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

FilesystemStreamCollector::getCollected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
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 FilesystemStreamCollector implements CollectorInterface, IndexCollectorInterface
8
{
9
    use CollectorTrait;
10
11
    public function __construct(private array $ignoredPathPatterns = [], private array $ignoredClasses = [])
12
    {
13
    }
14
15
    /**
16
     * @var array[]
17
     */
18
    private array $operations = [];
19
20
    public function getCollected(): array
21
    {
22
        return array_map('array_values', $this->operations);
23
    }
24
25
    public function startup(): void
26
    {
27
        $this->isActive = true;
28
        FilesystemStreamProxy::register();
29
        FilesystemStreamProxy::$collector = $this;
30
        FilesystemStreamProxy::$ignoredPathPatterns = $this->ignoredPathPatterns;
31
        FilesystemStreamProxy::$ignoredClasses = $this->ignoredClasses;
32
    }
33
34
    public function shutdown(): void
35
    {
36
        FilesystemStreamProxy::unregister();
37
        FilesystemStreamProxy::$collector = null;
38
        FilesystemStreamProxy::$ignoredPathPatterns = [];
39
        FilesystemStreamProxy::$ignoredClasses = [];
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->operations[$operation][] = [
52
            'path' => $path,
53
            'args' => $args,
54
        ];
55
    }
56
57
    public function getIndexData(): array
58
    {
59
        return [
60
            'fs_stream' => array_merge(
61
                ...array_map(
62
                    fn (string $operation) => [$operation => count($this->operations[$operation])],
63
                    array_keys($this->operations)
64
                )
65
            ),
66
        ];
67
    }
68
69
    private function reset(): void
70
    {
71
        $this->operations = [];
72
    }
73
}
74