1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* |
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Cache\DataCollector; |
12
|
|
|
|
13
|
|
|
use Psr\Log\LoggerAwareInterface; |
14
|
|
|
use Psr\Log\LoggerAwareTrait; |
15
|
|
|
use Symfony\Component\Routing\RouteCollection; |
16
|
|
|
use Yarhon\RouteGuardBundle\Controller\ControllerNameResolverInterface; |
17
|
|
|
use Yarhon\RouteGuardBundle\Exception\ExceptionInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class RouteCollectionDataCollector implements LoggerAwareInterface |
23
|
|
|
{ |
24
|
|
|
use LoggerAwareTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RouteDataCollector |
28
|
|
|
*/ |
29
|
|
|
private $routeDataCollector; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ControllerNameResolverInterface |
33
|
|
|
*/ |
34
|
|
|
private $controllerNameResolver; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $options; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param RouteDataCollector $routeDataCollector |
43
|
|
|
* @param ControllerNameResolverInterface $controllerNameResolver |
44
|
|
|
* @param array $options |
45
|
|
|
*/ |
46
|
24 |
|
public function __construct(RouteDataCollector $routeDataCollector, ControllerNameResolverInterface $controllerNameResolver, $options = []) |
47
|
|
|
{ |
48
|
24 |
|
$this->routeDataCollector = $routeDataCollector; |
49
|
24 |
|
$this->controllerNameResolver = $controllerNameResolver; |
50
|
|
|
|
51
|
24 |
|
$this->options = array_merge([ |
52
|
24 |
|
'ignore_controllers' => [], |
53
|
|
|
'ignore_exceptions' => false, |
54
|
24 |
|
], $options); |
55
|
24 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param RouteCollection $routeCollection |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
* |
62
|
|
|
* @throws ExceptionInterface |
63
|
|
|
*/ |
64
|
24 |
|
public function collect(RouteCollection $routeCollection) |
65
|
|
|
{ |
66
|
24 |
|
if ($this->logger) { |
67
|
20 |
|
$this->logger->info('Collect data for route collection', ['count' => count($routeCollection)]); |
68
|
|
|
} |
69
|
|
|
|
70
|
24 |
|
$ignoredRoutes = []; |
71
|
24 |
|
$catchExceptions = $this->options['ignore_exceptions'] && $this->logger; |
72
|
|
|
|
73
|
24 |
|
$data = []; |
74
|
|
|
|
75
|
24 |
|
foreach ($routeCollection as $routeName => $route) { |
76
|
|
|
try { |
77
|
24 |
|
$controller = $route->getDefault('_controller'); |
78
|
24 |
|
$controllerName = $this->controllerNameResolver->resolve($controller); |
79
|
|
|
|
80
|
23 |
|
if (null !== $controllerName && $this->isControllerIgnored($controllerName)) { |
81
|
1 |
|
$ignoredRoutes[] = $routeName; |
82
|
1 |
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
23 |
|
$routeData = $this->routeDataCollector->collect($routeName, $route, $controllerName); |
86
|
|
|
|
87
|
|
|
// We don't need any data for routes without authorization tests |
88
|
22 |
|
if (count($routeData[0])) { |
89
|
22 |
|
$data[$routeName] = $routeData; |
90
|
|
|
} |
91
|
4 |
|
} catch (ExceptionInterface $e) { |
92
|
4 |
|
if (!$catchExceptions) { |
93
|
2 |
|
$this->addRouteNameToException($e, $routeName); |
94
|
2 |
|
throw $e; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
$this->logger->error(sprintf('Route "%s" would be ignored because of exception caught: %s', $routeName, $e->getMessage()), ['exception' => $e]); |
|
|
|
|
98
|
2 |
|
continue; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
22 |
|
if ($this->logger && count($ignoredRoutes)) { |
103
|
|
|
$this->logger->info('Ignored routes', ['count' => count($ignoredRoutes), 'list' => $ignoredRoutes]); |
104
|
|
|
} |
105
|
|
|
|
106
|
22 |
|
return $data; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $controllerName |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
23 |
|
private function isControllerIgnored($controllerName) |
115
|
|
|
{ |
116
|
23 |
|
foreach ($this->options['ignore_controllers'] as $ignored) { |
117
|
19 |
|
if (0 === strpos($controllerName, $ignored)) { |
118
|
1 |
|
return true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
23 |
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
private function addRouteNameToException(\Exception $e, $routeName) |
126
|
|
|
{ |
127
|
2 |
|
$message = sprintf('Route "%s": %s', $routeName, $e->getMessage()); |
128
|
|
|
|
129
|
2 |
|
$r = new \ReflectionProperty($e, 'message'); |
130
|
2 |
|
$r->setAccessible(true); |
131
|
2 |
|
$r->setValue($e, $message); |
132
|
2 |
|
} |
133
|
|
|
} |
134
|
|
|
|