Passed
Pull Request — master (#160)
by Dmitriy
02:31
created

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