| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Routing\Generator\Dumper;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Routing\Exception\RouteCircularReferenceException;
|
|
|
15 |
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
|
|
16 |
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* CompiledUrlGeneratorDumper creates a PHP array to be used with CompiledUrlGenerator.
|
|
|
20 |
*
|
|
|
21 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
22 |
* @author Tobias Schultze <http://tobion.de>
|
|
|
23 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
24 |
*/
|
|
|
25 |
class CompiledUrlGeneratorDumper extends GeneratorDumper
|
|
|
26 |
{
|
|
|
27 |
public function getCompiledRoutes(): array
|
|
|
28 |
{
|
|
|
29 |
$compiledRoutes = [];
|
|
|
30 |
foreach ($this->getRoutes()->all() as $name => $route) {
|
|
|
31 |
$compiledRoute = $route->compile();
|
|
|
32 |
|
|
|
33 |
$compiledRoutes[$name] = [
|
|
|
34 |
$compiledRoute->getVariables(),
|
|
|
35 |
$route->getDefaults(),
|
|
|
36 |
$route->getRequirements(),
|
|
|
37 |
$compiledRoute->getTokens(),
|
|
|
38 |
$compiledRoute->getHostTokens(),
|
|
|
39 |
$route->getSchemes(),
|
|
|
40 |
[],
|
|
|
41 |
];
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
return $compiledRoutes;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public function getCompiledAliases(): array
|
|
|
48 |
{
|
|
|
49 |
$routes = $this->getRoutes();
|
|
|
50 |
$compiledAliases = [];
|
|
|
51 |
foreach ($routes->getAliases() as $name => $alias) {
|
|
|
52 |
$deprecations = $alias->isDeprecated() ? [$alias->getDeprecation($name)] : [];
|
|
|
53 |
$currentId = $alias->getId();
|
|
|
54 |
$visited = [];
|
|
|
55 |
while (null !== $alias = $routes->getAlias($currentId) ?? null) {
|
|
|
56 |
if (false !== $searchKey = array_search($currentId, $visited)) {
|
|
|
57 |
$visited[] = $currentId;
|
|
|
58 |
|
|
|
59 |
throw new RouteCircularReferenceException($currentId, \array_slice($visited, $searchKey));
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
if ($alias->isDeprecated()) {
|
|
|
63 |
$deprecations[] = $deprecation = $alias->getDeprecation($currentId);
|
|
|
64 |
trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$visited[] = $currentId;
|
|
|
68 |
$currentId = $alias->getId();
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if (null === $target = $routes->get($currentId)) {
|
|
|
72 |
throw new RouteNotFoundException(sprintf('Target route "%s" for alias "%s" does not exist.', $currentId, $name));
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$compiledTarget = $target->compile();
|
|
|
76 |
|
|
|
77 |
$compiledAliases[$name] = [
|
|
|
78 |
$compiledTarget->getVariables(),
|
|
|
79 |
$target->getDefaults(),
|
|
|
80 |
$target->getRequirements(),
|
|
|
81 |
$compiledTarget->getTokens(),
|
|
|
82 |
$compiledTarget->getHostTokens(),
|
|
|
83 |
$target->getSchemes(),
|
|
|
84 |
$deprecations,
|
|
|
85 |
];
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return $compiledAliases;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function dump(array $options = []): string
|
|
|
92 |
{
|
|
|
93 |
return <<<EOF
|
|
|
94 |
<?php
|
|
|
95 |
|
|
|
96 |
// This file has been auto-generated by the Symfony Routing Component.
|
|
|
97 |
|
|
|
98 |
return [{$this->generateDeclaredRoutes()}
|
|
|
99 |
];
|
|
|
100 |
|
|
|
101 |
EOF;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Generates PHP code representing an array of defined routes
|
|
|
106 |
* together with the routes properties (e.g. requirements).
|
|
|
107 |
*/
|
|
|
108 |
private function generateDeclaredRoutes(): string
|
|
|
109 |
{
|
|
|
110 |
$routes = '';
|
|
|
111 |
foreach ($this->getCompiledRoutes() as $name => $properties) {
|
|
|
112 |
$routes .= sprintf("\n '%s' => %s,", $name, CompiledUrlMatcherDumper::export($properties));
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
foreach ($this->getCompiledAliases() as $alias => $properties) {
|
|
|
116 |
$routes .= sprintf("\n '%s' => %s,", $alias, CompiledUrlMatcherDumper::export($properties));
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
return $routes;
|
|
|
120 |
}
|
|
|
121 |
}
|