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', 'afterStack' => 'array'])] |
19
|
|
|
public function getCollected(): array |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
1 |
|
'beforeStack' => $this->beforeStack, |
23
|
1 |
|
'afterStack' => $this->afterStack, |
24
|
|
|
]; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public function collect(...$payload): void |
28
|
|
|
{ |
29
|
1 |
|
$event = current($payload); |
30
|
1 |
|
if (!is_object($event) || !$this->isActive()) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
if ($event instanceof BeforeMiddleware) { |
35
|
1 |
|
$this->beforeStack[] = [ |
36
|
1 |
|
'name' => get_class($event->getMiddleware()), |
37
|
1 |
|
'time' => microtime(true), |
38
|
1 |
|
'memory' => memory_get_usage(), |
39
|
1 |
|
'request' => $event->getRequest(), |
40
|
|
|
]; |
41
|
1 |
|
} elseif ($event instanceof AfterMiddleware) { |
42
|
1 |
|
$this->afterStack[] = [ |
43
|
1 |
|
'name' => get_class($event->getMiddleware()), |
44
|
1 |
|
'time' => microtime(true), |
45
|
1 |
|
'memory' => memory_get_usage(), |
46
|
1 |
|
'response' => $event->getResponse(), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
1 |
|
} |
50
|
|
|
|
51
|
1 |
|
private function reset(): void |
|
|
|
|
52
|
|
|
{ |
53
|
1 |
|
$this->beforeStack = []; |
54
|
1 |
|
$this->afterStack = []; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
#[ArrayShape(['totalMiddlewares' => 'int'])] |
58
|
|
|
public function getIndexData(): array |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
1 |
|
'totalMiddlewares' => count($this->beforeStack), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.