Passed
Push — master ( f7ef7a...65c26d )
by Dmitriy
02:46
created

FilesystemStreamCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 26
dl 0
loc 73
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A shutdown() 0 9 1
A getCollected() 0 3 1
A reset() 0 3 1
A __construct() 0 10 1
A collect() 0 9 2
A startup() 0 7 1
A getSummary() 0 7 1
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 array $ignoredPathPatterns = [],
22
        private array $ignoredClasses = [],
23
    ) {
24
    }
25
26
    /**
27
     * @var array[]
28
     */
29
    private array $operations = [];
30
31
    public function getCollected(): array
32
    {
33
        return array_map('array_values', $this->operations);
34
    }
35
36
    public function startup(): void
37
    {
38
        $this->isActive = true;
39
        FilesystemStreamProxy::register();
40
        FilesystemStreamProxy::$collector = $this;
41
        FilesystemStreamProxy::$ignoredPathPatterns = $this->ignoredPathPatterns;
42
        FilesystemStreamProxy::$ignoredClasses = $this->ignoredClasses;
43
    }
44
45
    public function shutdown(): void
46
    {
47
        FilesystemStreamProxy::unregister();
48
        FilesystemStreamProxy::$collector = null;
49
        FilesystemStreamProxy::$ignoredPathPatterns = [];
50
        FilesystemStreamProxy::$ignoredClasses = [];
51
52
        $this->reset();
53
        $this->isActive = false;
54
    }
55
56
    public function collect(string $operation, string $path, array $args): void
57
    {
58
        if (!$this->isActive()) {
59
            return;
60
        }
61
62
        $this->operations[$operation][] = [
63
            'path' => $path,
64
            'args' => $args,
65
        ];
66
    }
67
68
    public function getSummary(): array
69
    {
70
        return [
71
            'fs_stream' => array_merge(
72
                ...array_map(
73
                    fn (string $operation) => [$operation => count($this->operations[$operation])],
74
                    array_keys($this->operations)
75
                )
76
            ),
77
        ];
78
    }
79
80
    private function reset(): void
81
    {
82
        $this->operations = [];
83
    }
84
}
85