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