1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector\Web; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use ReflectionObject; |
9
|
|
|
use Yiisoft\Router\CurrentRoute; |
10
|
|
|
use Yiisoft\Router\Route; |
11
|
|
|
use Yiisoft\Router\RouteCollectionInterface; |
12
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorTrait; |
13
|
|
|
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; |
14
|
|
|
|
15
|
|
|
class RouterCollector implements SummaryCollectorInterface |
16
|
|
|
{ |
17
|
|
|
use CollectorTrait; |
18
|
|
|
|
19
|
|
|
private float $matchTime = 0; |
20
|
|
|
|
21
|
|
|
public function __construct(private ContainerInterface $container) |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function collect(float $matchTime): void |
26
|
|
|
{ |
27
|
|
|
if (!$this->isActive()) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
$this->matchTime = $matchTime; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getCollected(): array |
34
|
|
|
{ |
35
|
|
|
$routeCollection = $this->container->has(RouteCollectionInterface::class) |
36
|
|
|
? $this->container->get(RouteCollectionInterface::class) |
37
|
|
|
: null; |
38
|
|
|
|
39
|
|
|
$currentRoute = $this->getCurrentRoute(); |
40
|
|
|
$route = $this->getRouteByCurrentRoute($currentRoute); |
41
|
|
|
[$middlewares, $action] = $this->getMiddlewaresAndAction($route); |
42
|
|
|
|
43
|
|
|
$result = [ |
44
|
|
|
'currentRoute' => null, |
45
|
|
|
]; |
46
|
|
|
if ($currentRoute !== null && $route !== null) { |
47
|
|
|
$result['currentRoute'] = [ |
48
|
|
|
'matchTime' => $this->matchTime, |
49
|
|
|
'name' => $route->getData('name'), |
50
|
|
|
'pattern' => $route->getData('pattern'), |
51
|
|
|
'arguments' => $currentRoute->getArguments(), |
52
|
|
|
'host' => $route->getData('host'), |
53
|
|
|
'uri' => (string) $currentRoute->getUri(), |
54
|
|
|
'action' => $action, |
55
|
|
|
'middlewares' => $middlewares, |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
if ($routeCollection !== null) { |
59
|
|
|
$result['routesTree'] = $routeCollection->getRouteTree(); |
60
|
|
|
$result['routes'] = $routeCollection->getRoutes(); |
61
|
|
|
$result['routeTime'] = $this->matchTime; |
62
|
|
|
} |
63
|
|
|
return $result; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getSummary(): array |
67
|
|
|
{ |
68
|
|
|
$currentRoute = $this->getCurrentRoute(); |
69
|
|
|
$route = $this->getRouteByCurrentRoute($currentRoute); |
70
|
|
|
|
71
|
|
|
if ($currentRoute === null || $route === null) { |
72
|
|
|
return [ |
73
|
|
|
'router' => null, |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
[$middlewares, $action] = $this->getMiddlewaresAndAction($route); |
78
|
|
|
|
79
|
|
|
return [ |
80
|
|
|
'router' => [ |
81
|
|
|
'matchTime' => $this->matchTime, |
82
|
|
|
'name' => $route->getData('name'), |
83
|
|
|
'pattern' => $route->getData('pattern'), |
84
|
|
|
'arguments' => $currentRoute->getArguments(), |
85
|
|
|
'host' => $route->getData('host'), |
86
|
|
|
'uri' => (string) $currentRoute->getUri(), |
87
|
|
|
'action' => $action, |
88
|
|
|
'middlewares' => $middlewares, |
89
|
|
|
], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getCurrentRoute(): ?CurrentRoute |
94
|
|
|
{ |
95
|
|
|
return $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getRouteByCurrentRoute(?CurrentRoute $currentRoute): ?Route |
99
|
|
|
{ |
100
|
|
|
if ($currentRoute === null) { |
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
$reflection = new ReflectionObject($currentRoute); |
104
|
|
|
|
105
|
|
|
$reflectionProperty = $reflection->getProperty('route'); |
106
|
|
|
$reflectionProperty->setAccessible(true); |
107
|
|
|
|
108
|
|
|
return $reflectionProperty->getValue($currentRoute); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function getMiddlewaresAndAction(?Route $route): array |
112
|
|
|
{ |
113
|
|
|
if ($route === null) { |
114
|
|
|
return [[], null]; |
115
|
|
|
} |
116
|
|
|
$reflection = new ReflectionObject($route); |
117
|
|
|
$reflectionProperty = $reflection->getProperty('middlewareDefinitions'); |
118
|
|
|
$reflectionProperty->setAccessible(true); |
119
|
|
|
$middlewareDefinitions = $reflectionProperty->getValue($route); |
120
|
|
|
$action = array_pop($middlewareDefinitions); |
121
|
|
|
return [$middlewareDefinitions, $action]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|