1 | <?php |
||
18 | class ControllerCompilerPass implements CompilerPassInterface |
||
19 | { |
||
20 | use FileCacheTrait; |
||
21 | |||
22 | const CONTROLLER_TAG = 'controller'; |
||
23 | const ROUTE_TAG = 'route'; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | 3 | public function process(ContainerBuilder $container) |
|
29 | { |
||
30 | 3 | $controllers = $container->findTaggedServiceIds(self::ROUTE_TAG); |
|
31 | |||
32 | 3 | $serialized = []; |
|
33 | 3 | foreach ($controllers as $controllerId => $tag) { |
|
34 | 3 | foreach ($tag as $routeRaw) { |
|
35 | /** @var RouteAnnotation $route */ |
||
36 | 3 | $route = $routeRaw[0]; |
|
37 | |||
38 | 3 | $name = $route->getName(); |
|
39 | 3 | if (empty($name)) { |
|
40 | throw new Exception(sprintf('"name" is missing for @Route(%s)', $controllerId)); |
||
41 | } |
||
42 | |||
43 | 3 | if (isset($serialized[$name])) { |
|
44 | throw new Exception(sprintf('Route name %s does already exits in %s', $name, $controllerId)); |
||
45 | } |
||
46 | |||
47 | 3 | $serialized[$name] = serialize($this->createRoute($route)); |
|
48 | } |
||
49 | |||
50 | 3 | $controller = $container->getDefinition($controllerId); |
|
51 | 3 | $controller->clearTag(self::ROUTE_TAG); |
|
52 | } |
||
53 | |||
54 | 3 | ksort($serialized); |
|
55 | |||
56 | 3 | $this->dumpMatcher($container, $serialized); |
|
57 | 3 | } |
|
58 | |||
59 | /** |
||
60 | * @param RouteAnnotation $route |
||
61 | * @return Route |
||
62 | */ |
||
63 | 3 | private function createRoute(RouteAnnotation $route) |
|
64 | { |
||
65 | 3 | if ($route->isCsrf()) { |
|
66 | 1 | $route->setOptions(['csrf' => true]); |
|
67 | } |
||
68 | |||
69 | 3 | return new Route( |
|
70 | 3 | $route->getPath(), |
|
71 | 3 | $route->getDefaults(), |
|
72 | 3 | $route->getRequirements(), |
|
73 | 3 | $route->getOptions(), |
|
74 | 3 | $route->getHost(), |
|
75 | 3 | $route->getSchemes(), |
|
76 | 3 | $route->getMethods(), |
|
77 | 3 | $route->getCondition() |
|
78 | ); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param ContainerBuilder $container |
||
83 | * @param array $routes |
||
84 | * @codeCoverageIgnore |
||
85 | */ |
||
86 | protected function dumpMatcher(ContainerBuilder $container, array $routes) |
||
98 | } |
||
99 |