forked from FriendsOfSymfony/FOSJsRoutingBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutesResponse.php
101 lines (83 loc) · 2.83 KB
/
RoutesResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
/*
* This file is part of the FOSJsRoutingBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\JsRoutingBundle\Response;
use Symfony\Component\Routing\RouteCollection;
class RoutesResponse
{
private $routes;
public function __construct(private string $baseUrl, RouteCollection $routes = null,
private ?string $prefix = null, private ?string $host = null,
private ?string $port = null, private ?string $scheme = null,
private ?string $locale = null, private array $domains = [])
{
$this->routes = $routes ?: new RouteCollection();
}
public function getBaseUrl(): string
{
return $this->baseUrl;
}
public function getRoutes(): array
{
$exposedRoutes = [];
foreach ($this->routes->all() as $name => $route) {
if (!$route->hasOption('expose')) {
$domain = 'default';
} else {
$domain = $route->getOption('expose');
$domain = is_string($domain) ? ('true' === $domain ? 'default' : $domain) : 'default';
}
if (0 === count($this->domains)) {
if ('default' !== $domain) {
continue;
}
} elseif (!in_array($domain, $this->domains, true)) {
continue;
}
$compiledRoute = $route->compile();
$defaults = array_intersect_key(
$route->getDefaults(),
array_fill_keys($compiledRoute->getVariables(), null)
);
if (!isset($defaults['_locale']) && in_array('_locale', $compiledRoute->getVariables())) {
$defaults['_locale'] = $this->locale;
}
$exposedRoutes[$name] = [
'tokens' => $compiledRoute->getTokens(),
'defaults' => $defaults,
'requirements' => $route->getRequirements(),
'hosttokens' => method_exists($compiledRoute, 'getHostTokens') ? $compiledRoute->getHostTokens() : [],
'methods' => $route->getMethods(),
'schemes' => $route->getSchemes(),
];
}
return $exposedRoutes;
}
public function getPrefix(): ?string
{
return $this->prefix;
}
public function getHost(): ?string
{
return $this->host;
}
public function getPort(): ?string
{
return $this->port;
}
public function getScheme(): ?string
{
return $this->scheme;
}
public function getLocale(): ?string
{
return $this->locale;
}
}