-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFilteredDirectory.php
More file actions
139 lines (126 loc) · 3.17 KB
/
Copy pathFilteredDirectory.php
File metadata and controls
139 lines (126 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Aternos\IO\System\Directory;
use Aternos\IO\Exception\IOException;
use Aternos\IO\Exception\PathOutsideElementException;
use Aternos\IO\System\Directory\Filter\PathFilter;
use Aternos\IO\System\FilesystemElement;
use Generator;
/**
* Class FilteredDirectory
*
* Directory with filters for children
*
* @package Aternos\IO\System\Directory
*/
class FilteredDirectory extends Directory
{
/**
* @var PathFilter[]
*/
protected array $filters = [];
protected bool $mode = false;
protected bool $skipFilter = false;
/**
* @param string $path
* @param PathFilter[] $filters
*/
public function __construct(string $path, array $filters = [])
{
parent::__construct($path);
$this->filters = $filters;
}
/**
* Add a filter to the directory
*
* @param PathFilter $filter
* @return $this
*/
public function addFilter(PathFilter $filter): static
{
$this->filters[] = $filter;
return $this;
}
/**
* Get current filters
*
* @return PathFilter[]
*/
public function getFilters(): array
{
return $this->filters;
}
/**
* Set/replace all filters
*
* @param PathFilter[] $filters
* @return $this
*/
public function setFilters(array $filters): static
{
$this->filters = $filters;
return $this;
}
/**
* Set the filter mode to include only filtered files
*
* @return $this
*/
public function includeOnlyFiltered(): static
{
$this->mode = true;
return $this;
}
/**
* Set the filter mode to exclude filtered files
*
* @return $this
*/
public function excludeFiltered(): static
{
$this->mode = false;
return $this;
}
/**
* @inheritDoc
* @throws PathOutsideElementException
*/
public function getChildren(bool $allowOutsideLinks = false): Generator
{
foreach (parent::getChildren($allowOutsideLinks) as $child) {
if ($this->shouldInclude($child) || $this->skipFilter) {
yield $child;
}
}
}
/**
* @inheritDoc
* @throws PathOutsideElementException
*/
public function getChildrenRecursive(bool $allowOutsideLinks = false, bool $followLinks = true, int $currentDepth = 0): Generator
{
$this->skipFilter = true;
foreach (parent::getChildrenRecursive($allowOutsideLinks, $followLinks, $currentDepth) as $child) {
if ($this->shouldInclude($child)) {
yield $child;
}
}
$this->skipFilter = false;
}
/**
* Check if a filesystem element should be included based on the filters
*
* @param FilesystemElement $element
* @return bool
* @throws PathOutsideElementException
* @throws IOException
*/
protected function shouldInclude(FilesystemElement $element): bool
{
foreach ($this->filters as $filter) {
if ($filter->matches($element->getRelativePathTo($this))) {
return $this->mode;
}
}
return !$this->mode;
}
}