Passed
Pull Request — master (#186)
by Alexander
02:41
created

HttpStreamCollector::getCollected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
        // TODO: add cURL support, maybe through proxy?
27
        // https://github.com/php/php-src/issues/10509
28
        //stream_context_set_default([
29
        //    'http' => [
30
        //        'proxy' => 'yii-debug-http://127.0.0.1',
31
        //    ],
32
        //]);
33
        HttpStreamProxy::$collector = $this;
34
    }
35
36
    public function shutdown(): void
37
    {
38
        HttpStreamProxy::unregister();
39
        HttpStreamProxy::$collector = null;
40
41
        $this->reset();
42
        $this->isActive = false;
43
    }
44
45
    public function collect(string $operation, string $path, array $args): void
46
    {
47
        if (!$this->isActive()) {
48
            return;
49
        }
50
51
        $this->requests[$operation][] = [
52
            'uri' => $path,
53
            'args' => $args,
54
        ];
55
    }
56
57
    public function getIndexData(): array
58
    {
59
        return [
60
            'http_stream' => array_merge(
61
                ...array_map(
62
                    fn (string $operation) => [
63
                        $operation => is_countable($this->requests[$operation]) ? count(
64
                            $this->requests[$operation]
65
                        ) : 0,
66
                    ],
67
                    array_keys($this->requests)
68
                )
69
            ),
70
        ];
71
    }
72
73
    private function reset(): void
74
    {
75
        $this->requests = [];
76
    }
77
}
78