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