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 | 3 | } elseif (isset($serialized[$name])) { |
|
42 | throw new Exception(sprintf('Route name %s does already exits in %s', $name, $controllerId)); |
||
43 | } |
||
44 | |||
45 | 3 | $serialized[$name] = serialize($this->createRoute($route)); |
|
46 | } |
||
47 | |||
48 | 3 | $controller = $container->getDefinition($controllerId); |
|
49 | 3 | $controller->clearTag(self::ROUTE_TAG); |
|
50 | } |
||
51 | |||
52 | 3 | $this->dumpMatcher($container, $serialized); |
|
53 | 3 | } |
|
54 | |||
55 | /** |
||
56 | * @param RouteAnnotation $route |
||
57 | * @return Route |
||
58 | */ |
||
59 | 3 | private function createRoute(RouteAnnotation $route) |
|
60 | { |
||
61 | 3 | if ($route->isCsrf()) { |
|
62 | 1 | $route->setOptions(['csrf' => true]); |
|
63 | } |
||
64 | |||
65 | 3 | return new Route( |
|
66 | 3 | $route->getPath(), |
|
67 | 3 | $route->getDefaults(), |
|
68 | 3 | $route->getRequirements(), |
|
69 | 3 | $route->getOptions(), |
|
70 | 3 | $route->getHost(), |
|
71 | 3 | $route->getSchemes(), |
|
72 | 3 | $route->getMethods(), |
|
73 | 3 | $route->getCondition() |
|
74 | ); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param ContainerBuilder $container |
||
79 | * @param array $routes |
||
80 | * @codeCoverageIgnore |
||
81 | */ |
||
82 | protected function dumpMatcher(ContainerBuilder $container, array $routes) |
||
97 | } |
||
98 |
If you suppress an error, we recommend checking for the error condition explicitly: