Passed
Push — master ( 755731...71ac2a )
by Dmitriy
02:36
created

MiddlewareCollector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Web;
6
7
use ReflectionClass;
8
use Yiisoft\Middleware\Dispatcher\Event\AfterMiddleware;
9
use Yiisoft\Middleware\Dispatcher\Event\BeforeMiddleware;
10
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
11
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
12
use Yiisoft\Yii\Debug\Collector\TimelineCollector;
13
14
final class MiddlewareCollector implements SummaryCollectorInterface
15
{
16
    use CollectorTrait;
17
18
    private array $beforeStack = [];
19
    private array $afterStack = [];
20
21
    public function __construct(private TimelineCollector $timelineCollector)
22
    {
23
    }
24
25
    public function getCollected(): array
26
    {
27
        if (!$this->isActive()) {
28
            return [];
29
        }
30
        $beforeStack = $this->beforeStack;
31
        $afterStack = $this->afterStack;
32
        $beforeAction = array_pop($beforeStack);
33
        $afterAction = array_shift($afterStack);
34
        $actionHandler = [];
35
36
        if ($beforeAction !== null && $afterAction !== null) {
37
            $actionHandler = $this->getActionHandler($beforeAction, $afterAction);
38
        }
39
40
        return [
41
            'beforeStack' => $beforeStack,
42
            'actionHandler' => $actionHandler,
43
            'afterStack' => $afterStack,
44
        ];
45
    }
46
47
    public function collect(BeforeMiddleware|AfterMiddleware $event): void
48
    {
49
        if (!$this->isActive()) {
50
            return;
51
        }
52
53
        if (
54
            method_exists($event->getMiddleware(), '__debugInfo')
55
            && (new ReflectionClass($event->getMiddleware()))->isAnonymous()
56
        ) {
57
            $callback = $event->getMiddleware()->__debugInfo()['callback'];
58
            if (is_array($callback)) {
59
                $name = implode('::', $callback);
60
            } else {
61
                $name = 'object(Closure)#' . spl_object_id($callback);
62
            }
63
        } else {
64
            $name = $event->getMiddleware()::class;
65
        }
66
        if ($event instanceof BeforeMiddleware) {
67
            $this->beforeStack[] = [
68
                'name' => $name,
69
                'time' => microtime(true),
70
                'memory' => memory_get_usage(),
71
                'request' => $event->getRequest(),
72
            ];
73
        } else {
74
            $this->afterStack[] = [
75
                'name' => $name,
76
                'time' => microtime(true),
77
                'memory' => memory_get_usage(),
78
                'response' => $event->getResponse(),
79
            ];
80
        }
81
        $this->timelineCollector->collect($this, spl_object_id($event));
82
    }
83
84
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
85
    {
86
        $this->beforeStack = [];
87
        $this->afterStack = [];
88
    }
89
90
    public function getSummary(): array
91
    {
92
        if (!$this->isActive()) {
93
            return [];
94
        }
95
        return [
96
            'middleware' => [
97
                'total' => ($total = count($this->beforeStack)) > 0 ? $total - 1 : 0, // Remove action handler
98
            ],
99
        ];
100
    }
101
102
    private function getActionHandler(array $beforeAction, array $afterAction): array
103
    {
104
        return [
105
            'name' => $beforeAction['name'],
106
            'startTime' => $beforeAction['time'],
107
            'request' => $beforeAction['request'],
108
            'response' => $afterAction['response'],
109
            'endTime' => $afterAction['time'],
110
            'memory' => $afterAction['memory'],
111
        ];
112
    }
113
}
114