Passed
Pull Request — master (#186)
by Dmitriy
03:58 queued 01:17
created

HttpStreamCollector::collect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
final class HttpStreamCollector implements CollectorInterface, IndexCollectorInterface
8
{
9
    use CollectorTrait;
10
11
    public function __construct(private array $ignoredPathPatterns = [], private array $ignoredClasses = [])
12
    {
13
    }
14
15
    private array $requests = [];
16
17
    public function getCollected(): array
18
    {
19
        return $this->requests;
20
    }
21
22
    public function startup(): void
23
    {
24
        $this->isActive = true;
25
        HttpStreamProxy::register();
26
        //stream_context_set_default([
27
        //    'http' => [
28
        //        'proxy' => 'yii-debug-http://127.0.0.1',
29
        //    ],
30
        //]);
31
        //stream_context_set_default([
32
        //    'http' => [
33
        //        'proxy' => 'yii-debug-http://127.0.0.1',
34
        //    ],
35
        //]);
36
        HttpStreamProxy::$collector = $this;
37
    }
38
39
    public function shutdown(): void
40
    {
41
        HttpStreamProxy::unregister();
42
        HttpStreamProxy::$collector = null;
43
44
        $this->reset();
45
        $this->isActive = false;
46
    }
47
48
    public function collect(string $operation, string $path, array $args): void
49
    {
50
        if (!$this->isActive()) {
51
            return;
52
        }
53
54
        $this->requests[$operation][] = [
55
            'uri' => $path,
56
            'args' => $args,
57
        ];
58
    }
59
60
    public function getIndexData(): array
61
    {
62
        return [
63
            'http_stream' => array_merge(
64
                ...array_map(
65
                    fn (string $operation) => [
66
                        $operation => is_countable($this->requests[$operation]) ? count(
67
                            $this->requests[$operation]
68
                        ) : 0,
69
                    ],
70
                    array_keys($this->requests)
71
                )
72
            ),
73
        ];
74
    }
75
76
    private function reset(): void
77
    {
78
        $this->requests = [];
79
    }
80
}
81