|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Stream package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Serafim\Stream\Filter; |
|
13
|
|
|
|
|
14
|
|
|
abstract class BaseFilter implements FilterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array<callable> |
|
18
|
|
|
*/ |
|
19
|
|
|
protected array $filters = []; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $class |
|
23
|
|
|
* @param string $file |
|
24
|
|
|
* @return bool |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __invoke(string $class, string $file): bool |
|
27
|
1 |
|
{ |
|
28
|
|
|
return $this->match($class, $file); |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param callable $filter |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
public function not(callable $filter): self |
|
36
|
1 |
|
{ |
|
37
|
|
|
return $this->where(function (string $class, string $file) use ($filter): bool { |
|
38
|
|
|
return ! $filter($class, $file); |
|
39
|
1 |
|
}); |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param callable $filter |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
|
|
public function where(callable $filter): self |
|
47
|
14 |
|
{ |
|
48
|
|
|
$this->filters[] = $filter; |
|
49
|
14 |
|
|
|
50
|
|
|
return $this; |
|
51
|
14 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param \Closure $then |
|
55
|
|
|
* @return BaseFilter|$this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function every(\Closure $then): self |
|
58
|
1 |
|
{ |
|
59
|
|
|
return $this->where(function (string $class, string $file) use ($then): bool { |
|
60
|
|
|
$then($conjunction = new Conjunction()); |
|
61
|
1 |
|
|
|
62
|
|
|
return $conjunction->match($class, $file); |
|
63
|
1 |
|
}); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \Closure $then |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function any(\Closure $then): self |
|
71
|
1 |
|
{ |
|
72
|
|
|
return $this->where(function (string $class, string $file) use ($then): bool { |
|
73
|
|
|
$then($disjunction = new Disjunction()); |
|
74
|
1 |
|
|
|
75
|
|
|
return $disjunction->match($class, $file); |
|
76
|
1 |
|
}); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $fqn |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
|
|
public function fqn(string $fqn): self |
|
84
|
1 |
|
{ |
|
85
|
|
|
return $this->where(function (string $class) use ($fqn): bool { |
|
86
|
|
|
return \trim($class, '\\') === \trim($fqn, '\\'); |
|
87
|
1 |
|
}); |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $name |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
|
|
public function className(string $name): self |
|
95
|
2 |
|
{ |
|
96
|
|
|
$name = \trim($name, '\\'); |
|
97
|
2 |
|
|
|
98
|
|
|
return $this->where(function (string $fqn) use ($name): bool { |
|
99
|
|
|
return \substr($fqn, -\strlen($name)) === $name; |
|
100
|
2 |
|
}); |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $prefix |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
public function namespace(string $prefix): self |
|
108
|
1 |
|
{ |
|
109
|
|
|
$prefix = \trim($prefix, '\\'); |
|
110
|
1 |
|
|
|
111
|
|
|
return $this->where(function (string $class) use ($prefix): bool { |
|
112
|
|
|
return \strpos(\trim($class, '\\'), $prefix) === 0; |
|
113
|
1 |
|
}); |
|
114
|
1 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $name |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
|
|
public function fileName(string $name): self |
|
121
|
1 |
|
{ |
|
122
|
|
|
return $this->where(function (string $_, string $file) use ($name): bool { |
|
123
|
|
|
$file = \str_replace('\\', '/', $file); |
|
124
|
1 |
|
|
|
125
|
|
|
return \pathinfo($file, \PATHINFO_FILENAME) === $name; |
|
126
|
1 |
|
}); |
|
127
|
1 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $regex |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
|
|
public function pathNameMatches(string $regex): self |
|
134
|
1 |
|
{ |
|
135
|
|
|
$regex = $this->regex($regex, false); |
|
136
|
1 |
|
|
|
137
|
|
|
return $this->where(function (string $_, string $file) use ($regex): bool { |
|
138
|
|
|
return \preg_match($regex, $file) !== 0; |
|
139
|
1 |
|
}); |
|
140
|
1 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $regex |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
|
|
public function fileNameMatches(string $regex): self |
|
147
|
1 |
|
{ |
|
148
|
|
|
$regex = $this->regex($regex); |
|
149
|
1 |
|
|
|
150
|
|
|
return $this->where(function (string $_, string $file) use ($regex): bool { |
|
151
|
|
|
return \preg_match($regex, \pathinfo($file, \PATHINFO_FILENAME)) !== 0; |
|
152
|
1 |
|
}); |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $regex |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
|
|
public function classNameMatches(string $regex): self |
|
160
|
1 |
|
{ |
|
161
|
|
|
$regex = $this->regex($regex, true); |
|
162
|
1 |
|
|
|
163
|
|
|
return $this->where(function (string $fqn) use ($regex): bool { |
|
164
|
|
|
$class = \basename(\str_replace('\\', '/', $fqn)); |
|
165
|
1 |
|
|
|
166
|
|
|
return \preg_match($regex, $class) !== 0; |
|
167
|
1 |
|
}); |
|
168
|
1 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $regex |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
|
|
public function fqnMatches(string $regex): self |
|
175
|
1 |
|
{ |
|
176
|
|
|
$regex = $this->regex($regex); |
|
177
|
1 |
|
|
|
178
|
|
|
return $this->where(function (string $fqn) use ($regex): bool { |
|
179
|
|
|
return \preg_match($regex, $fqn) !== 0; |
|
180
|
1 |
|
}); |
|
181
|
1 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param string $regex |
|
185
|
|
|
* @param bool $strict |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
|
private function regex(string $regex, bool $strict = false): string |
|
189
|
4 |
|
{ |
|
190
|
|
|
$regex = $strict ? '^' . $regex . '$' : $regex; |
|
191
|
4 |
|
|
|
192
|
|
|
return \sprintf('/%s/isuS', \addcslashes($regex, '/')); |
|
193
|
4 |
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|