|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector; |
|
6
|
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Yiisoft\Router\CurrentRoute; |
|
10
|
|
|
use Yiisoft\Router\RouteCollectionInterface; |
|
11
|
|
|
|
|
12
|
|
|
final class RouterCollector implements RouterCollectorInterface, IndexCollectorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use CollectorTrait; |
|
15
|
|
|
|
|
16
|
|
|
private ContainerInterface $container; |
|
17
|
|
|
private float $matchTime = 0; |
|
18
|
|
|
|
|
19
|
1 |
|
public function __construct(ContainerInterface $container) |
|
20
|
|
|
{ |
|
21
|
1 |
|
$this->container = $container; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
public function collect(float $matchTime): void |
|
25
|
|
|
{ |
|
26
|
1 |
|
if (!$this->isActive()) { |
|
27
|
|
|
return; |
|
28
|
|
|
} |
|
29
|
1 |
|
$this->matchTime = $matchTime; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function getCollected(): array |
|
33
|
|
|
{ |
|
34
|
1 |
|
$routeCollection = $this->container->has(RouteCollectionInterface::class) |
|
35
|
1 |
|
? $this->container->get(RouteCollectionInterface::class) |
|
36
|
|
|
: null; |
|
37
|
|
|
|
|
38
|
1 |
|
return $routeCollection === null ? [] : |
|
39
|
|
|
[ |
|
40
|
1 |
|
'routesTree' => $routeCollection->getRouteTree(), |
|
41
|
1 |
|
'routes' => $routeCollection->getRoutes(), |
|
42
|
1 |
|
'routeTime' => $this->matchTime, |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
#[ArrayShape(['routeMatchTime' => 'float|int', 'matchedRoute' => 'string'])] |
|
47
|
|
|
public function getIndexData(): array |
|
48
|
|
|
{ |
|
49
|
1 |
|
$currentRoute = $this->container->has(CurrentRoute::class) ? $this->container->get(CurrentRoute::class) : null; |
|
50
|
|
|
return [ |
|
51
|
1 |
|
'routeMatchTime' => $this->matchTime, |
|
52
|
1 |
|
'matchedRoute' => $currentRoute?->getName(), |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|