Passed
Pull Request — master (#190)
by Dmitriy
04:57 queued 02:31
created

RouterCollector::getRouteByCurrentRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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