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