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

FilesystemStreamCollector::collect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
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 FilesystemStreamCollector implements CollectorInterface, IndexCollectorInterface
8
{
9
    use CollectorTrait;
10
11
    public function __construct(
12
        /**
13
         * Collection of regexps to ignore files sources to sniff.
14
         * Examples:
15
         * - '/' . preg_quote('yii-debug/src/Dumper', '/') . '/'
16
         * - '/ClosureExporter/'
17
         */
18
        private array $ignoredPathPatterns = [],
19
        private array $ignoredClasses = [],
20
    ) {
21
    }
22
23
    /**
24
     * @var array[]
25
     */
26
    private array $operations = [];
27
28
    public function getCollected(): array
29
    {
30
        return array_map('array_values', $this->operations);
31
    }
32
33
    public function startup(): void
34
    {
35
        $this->isActive = true;
36
        FilesystemStreamProxy::register();
37
        FilesystemStreamProxy::$collector = $this;
38
        FilesystemStreamProxy::$ignoredPathPatterns = $this->ignoredPathPatterns;
39
        FilesystemStreamProxy::$ignoredClasses = $this->ignoredClasses;
40
    }
41
42
    public function shutdown(): void
43
    {
44
        FilesystemStreamProxy::unregister();
45
        FilesystemStreamProxy::$collector = null;
46
        FilesystemStreamProxy::$ignoredPathPatterns = [];
47
        FilesystemStreamProxy::$ignoredClasses = [];
48
49
        $this->reset();
50
        $this->isActive = false;
51
    }
52
53
    public function collect(string $operation, string $path, array $args): void
54
    {
55
        if (!$this->isActive()) {
56
            return;
57
        }
58
59
        $this->operations[$operation][] = [
60
            'path' => $path,
61
            'args' => $args,
62
        ];
63
    }
64
65
    public function getIndexData(): array
66
    {
67
        return [
68
            'fs_stream' => array_merge(
69
                ...array_map(
70
                    fn (string $operation) => [$operation => count($this->operations[$operation])],
71
                    array_keys($this->operations)
72
                )
73
            ),
74
        ];
75
    }
76
77
    private function reset(): void
78
    {
79
        $this->operations = [];
80
    }
81
}
82