|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrainExe\Core\DependencyInjection\CompilerPass; |
|
4
|
|
|
|
|
5
|
|
|
use BrainExe\Core\Annotations\CompilerPass; |
|
6
|
|
|
use BrainExe\Core\Annotations\Route as RouteAnnotation; |
|
7
|
|
|
use BrainExe\Core\Application\SerializedRouteCollection; |
|
8
|
|
|
use BrainExe\Core\Traits\FileCacheTrait; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
12
|
|
|
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; |
|
13
|
|
|
use Symfony\Component\Routing\Route; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @CompilerPass(priority=5) |
|
17
|
|
|
*/ |
|
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) |
|
83
|
|
|
{ |
|
84
|
|
|
ksort($routes); |
|
85
|
|
|
|
|
86
|
|
|
$this->dumpVariableToCache(SerializedRouteCollection::CACHE_FILE, $routes); |
|
87
|
|
|
|
|
88
|
|
|
/** @var SerializedRouteCollection $routerCollection */ |
|
89
|
|
|
$routerCollection = $container->get('Core.RouteCollection'); |
|
90
|
|
|
|
|
91
|
|
|
$routerFile = sprintf('%scache/router_matcher.php', ROOT); |
|
92
|
|
|
$routeDumper = new PhpMatcherDumper($routerCollection); |
|
93
|
|
|
$content = $routeDumper->dump(); |
|
94
|
|
|
file_put_contents($routerFile, $content); |
|
95
|
|
|
@chmod($routerFile, 0777); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: