Passed
Pull Request — master (#160)
by Dmitriy
02:29
created

RouterCollector   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 60
c 0
b 0
f 0
dl 0
loc 107
rs 10

7 Methods

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