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