FilesystemStreamCollector::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Stream;
6
7
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
8
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
9
10
final class FilesystemStreamCollector implements SummaryCollectorInterface
11
{
12
    use CollectorTrait;
13
14
    public function __construct(
15
        /**
16
         * Collection of regexps to ignore files sources to sniff.
17
         * Examples:
18
         * - '/' . preg_quote('yii-debug/src/Dumper', '/') . '/'
19
         * - '/ClosureExporter/'
20
         */
21
        private readonly array $ignoredPathPatterns = [],
22
        private readonly array $ignoredClasses = [],
23
    ) {
24
    }
25
26
    /**
27
     * @var array[]
28
     */
29
    private array $operations = [];
30
31
    public function getCollected(): array
32
    {
33
        if (!$this->isActive()) {
34
            return [];
35
        }
36
        return array_map('array_values', $this->operations);
37
    }
38
39
    public function startup(): void
40
    {
41
        $this->isActive = true;
42
        FilesystemStreamProxy::register();
43
        FilesystemStreamProxy::$collector = $this;
44
        FilesystemStreamProxy::$ignoredPathPatterns = $this->ignoredPathPatterns;
45
        FilesystemStreamProxy::$ignoredClasses = $this->ignoredClasses;
46
    }
47
48
    public function shutdown(): void
49
    {
50
        FilesystemStreamProxy::unregister();
51
        FilesystemStreamProxy::$collector = null;
52
        FilesystemStreamProxy::$ignoredPathPatterns = [];
53
        FilesystemStreamProxy::$ignoredClasses = [];
54
55
        $this->reset();
56
        $this->isActive = false;
57
    }
58
59
    public function collect(string $operation, string $path, array $args): void
60
    {
61
        if (!$this->isActive()) {
62
            return;
63
        }
64
65
        $this->operations[$operation][] = [
66
            'path' => $path,
67
            'args' => $args,
68
        ];
69
    }
70
71
    public function getSummary(): array
72
    {
73
        if (!$this->isActive()) {
74
            return [];
75
        }
76
        return [
77
            'fs_stream' => array_merge(
78
                ...array_map(
79
                    fn (string $operation) => [$operation => count($this->operations[$operation])],
80
                    array_keys($this->operations)
81
                )
82
            ),
83
        ];
84
    }
85
86
    private function reset(): void
87
    {
88
        $this->operations = [];
89
    }
90
}
91