Passed
Pull Request — master (#195)
by Dmitriy
07:15 queued 04:46
created

RouterCollector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router\Debug;
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 1
    public function __construct(private ContainerInterface $container)
23
    {
24 1
    }
25
26 1
    public function collect(float $matchTime): void
27
    {
28 1
        if (!$this->isActive()) {
29
            return;
30
        }
31 1
        $this->matchTime = $matchTime;
32
    }
33
34 1
    public function getCollected(): array
35
    {
36 1
        $routeCollection = $this->container->has(RouteCollectionInterface::class)
37 1
            ? $this->container->get(RouteCollectionInterface::class)
38
            : null;
39
40 1
        $currentRoute = $this->getCurrentRoute();
41 1
        $route = $this->getRouteByCurrentRoute($currentRoute);
42 1
        [$middlewares, $action] = $this->getMiddlewaresAndAction($route);
43
44 1
        $result = [
45 1
            'currentRoute' => null,
46 1
        ];
47 1
        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 1
        if ($routeCollection !== null) {
60 1
            $result['routesTree'] = $routeCollection->getRouteTree();
61 1
            $result['routes'] = $routeCollection->getRoutes();
62 1
            $result['routeTime'] = $this->matchTime;
63
        }
64 1
        return $result;
65
    }
66
67 1
    public function getIndexData(): array
68
    {
69 1
        $currentRoute = $this->getCurrentRoute();
70 1
        $route = $this->getRouteByCurrentRoute($currentRoute);
71
72 1
        if ($currentRoute === null || $route === null) {
73 1
            return [
74 1
                'router' => null,
75 1
            ];
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 1
    private function getCurrentRoute(): ?CurrentRoute
95
    {
96 1
        return $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null;
97
    }
98
99 1
    private function getRouteByCurrentRoute(?CurrentRoute $currentRoute): ?Route
100
    {
101 1
        if ($currentRoute === null) {
102
            return null;
103
        }
104 1
        $reflection = new ReflectionObject($currentRoute);
105
106 1
        $reflectionProperty = $reflection->getProperty('route');
107 1
        $reflectionProperty->setAccessible(true);
108
109 1
        return $reflectionProperty->getValue($currentRoute);
110
    }
111
112 1
    private function getMiddlewaresAndAction(?Route $route): array
113
    {
114 1
        if ($route === null) {
115 1
            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