|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector; |
|
6
|
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
|
8
|
|
|
use Yiisoft\Middleware\Dispatcher\Event\AfterMiddleware; |
|
9
|
|
|
use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware; |
|
10
|
|
|
|
|
11
|
|
|
final class MiddlewareCollector implements CollectorInterface, IndexCollectorInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use CollectorTrait; |
|
14
|
|
|
|
|
15
|
|
|
private array $beforeStack = []; |
|
16
|
|
|
private array $afterStack = []; |
|
17
|
|
|
|
|
18
|
1 |
|
#[ArrayShape(['beforeStack' => 'array', 'actionHandler' => 'array', 'afterStack' => 'array'])] |
|
19
|
|
|
public function getCollected(): array |
|
20
|
|
|
{ |
|
21
|
1 |
|
$beforeStack = $this->beforeStack; |
|
22
|
1 |
|
$afterStack = $this->afterStack; |
|
23
|
1 |
|
$beforeAction = array_pop($beforeStack); |
|
24
|
1 |
|
$afterAction = array_shift($afterStack); |
|
25
|
1 |
|
$actionHandler = []; |
|
26
|
|
|
|
|
27
|
1 |
|
if ($beforeAction !== null && $afterAction !== null) { |
|
28
|
1 |
|
$actionHandler = $this->getActionHandler($beforeAction, $afterAction); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return [ |
|
32
|
1 |
|
'beforeStack' => $beforeStack, |
|
33
|
1 |
|
'actionHandler' => $actionHandler, |
|
34
|
1 |
|
'afterStack' => $afterStack, |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function collect(BeforeMiddleware|AfterMiddleware $event): void |
|
39
|
|
|
{ |
|
40
|
1 |
|
if (!$this->isActive()) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ( |
|
45
|
1 |
|
method_exists($event->getMiddleware(), '__debugInfo') |
|
46
|
1 |
|
&& (new \ReflectionClass($event->getMiddleware()))->isAnonymous() |
|
47
|
|
|
) { |
|
48
|
|
|
$name = implode('::', $event->getMiddleware()->__debugInfo()['callback']); |
|
49
|
|
|
} else { |
|
50
|
1 |
|
$name = get_class($event->getMiddleware()); |
|
51
|
|
|
} |
|
52
|
1 |
|
if ($event instanceof BeforeMiddleware) { |
|
53
|
1 |
|
$this->beforeStack[] = [ |
|
54
|
1 |
|
'name' => $name, |
|
55
|
1 |
|
'time' => microtime(true), |
|
56
|
1 |
|
'memory' => memory_get_usage(), |
|
57
|
1 |
|
'request' => $event->getRequest(), |
|
58
|
|
|
]; |
|
59
|
|
|
} else { |
|
60
|
1 |
|
$this->afterStack[] = [ |
|
61
|
1 |
|
'name' => $name, |
|
62
|
1 |
|
'time' => microtime(true), |
|
63
|
1 |
|
'memory' => memory_get_usage(), |
|
64
|
1 |
|
'response' => $event->getResponse(), |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
private function reset(): void |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
1 |
|
$this->beforeStack = []; |
|
72
|
1 |
|
$this->afterStack = []; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
#[ArrayShape(['totalMiddlewares' => 'int'])] |
|
76
|
|
|
public function getIndexData(): array |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
1 |
|
'totalMiddlewares' => ($total = count($this->beforeStack)) > 0 ? $total - 1 : 0, // Remove action handler |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
#[ArrayShape([ |
|
84
|
|
|
'name' => 'string', |
|
85
|
|
|
'startTime' => 'float', |
|
86
|
|
|
'request' => 'object', |
|
87
|
|
|
'response' => 'object', |
|
88
|
|
|
'endTime' => 'float', |
|
89
|
|
|
'memory' => 'int', |
|
90
|
|
|
])] |
|
91
|
|
|
private function getActionHandler(array $beforeAction, array $afterAction): array |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
1 |
|
'name' => $beforeAction['name'], |
|
95
|
1 |
|
'startTime' => $beforeAction['time'], |
|
96
|
1 |
|
'request' => $beforeAction['request'], |
|
97
|
1 |
|
'response' => $afterAction['response'], |
|
98
|
1 |
|
'endTime' => $afterAction['time'], |
|
99
|
1 |
|
'memory' => $afterAction['memory'], |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.