Passed
Pull Request — master (#195)
by Dmitriy
20:26 queued 07:33
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\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 1
    public function __construct(private ContainerInterface $container)
22
    {
23 1
    }
24
25 1
    public function collect(float $matchTime): void
26
    {
27 1
        if (!$this->isActive()) {
28
            return;
29
        }
30 1
        $this->matchTime = $matchTime;
31
    }
32
33 1
    public function getCollected(): array
34
    {
35 1
        $routeCollection = $this->container->has(RouteCollectionInterface::class)
36 1
            ? $this->container->get(RouteCollectionInterface::class)
37
            : null;
38
39 1
        $currentRoute = $this->getCurrentRoute();
40 1
        $route = $this->getRouteByCurrentRoute($currentRoute);
41 1
        [$middlewares, $action] = $this->getMiddlewaresAndAction($route);
42
43 1
        $result = [
44 1
            'currentRoute' => null,
45 1
        ];
46 1
        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 1
        if ($routeCollection !== null) {
59 1
            $result['routesTree'] = $routeCollection->getRouteTree();
60 1
            $result['routes'] = $routeCollection->getRoutes();
61 1
            $result['routeTime'] = $this->matchTime;
62
        }
63 1
        return $result;
64
    }
65
66 1
    public function getSummary(): array
67
    {
68 1
        $currentRoute = $this->getCurrentRoute();
69 1
        $route = $this->getRouteByCurrentRoute($currentRoute);
70
71 1
        if ($currentRoute === null || $route === null) {
72 1
            return [
73 1
                'router' => null,
74 1
            ];
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 1
    private function getCurrentRoute(): ?CurrentRoute
94
    {
95 1
        return $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null;
96
    }
97
98 1
    private function getRouteByCurrentRoute(?CurrentRoute $currentRoute): ?Route
99
    {
100 1
        if ($currentRoute === null) {
101
            return null;
102
        }
103 1
        $reflection = new ReflectionObject($currentRoute);
104
105 1
        $reflectionProperty = $reflection->getProperty('route');
106 1
        $reflectionProperty->setAccessible(true);
107
108 1
        return $reflectionProperty->getValue($currentRoute);
109
    }
110
111 1
    private function getMiddlewaresAndAction(?Route $route): array
112
    {
113 1
        if ($route === null) {
114 1
            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