|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Classifier; |
|
6
|
|
|
|
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use Symfony\Component\Finder\Finder; |
|
9
|
|
|
use Yiisoft\Classifier\Filter\FilterInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Base implementation for {@see ClassifierInterface} with common filters. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractClassifier implements ClassifierInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array<class-string|trait-string, ReflectionClass> |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
|
|
protected static array $reflectionsCache = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var FilterInterface[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private array $filters = []; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string[] |
|
27
|
|
|
*/ |
|
28
|
|
|
protected array $directories; |
|
29
|
|
|
|
|
30
|
34 |
|
public function __construct(string $directory, string ...$directories) |
|
31
|
|
|
{ |
|
32
|
34 |
|
$this->directories = [$directory, ...array_values($directories)]; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
34 |
|
public function withFilter(FilterInterface ...$filter): static |
|
36
|
|
|
{ |
|
37
|
34 |
|
$new = clone $this; |
|
38
|
34 |
|
array_push($new->filters, ...array_values($filter)); |
|
39
|
|
|
|
|
40
|
34 |
|
return $new; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return iterable<class-string> |
|
45
|
|
|
*/ |
|
46
|
34 |
|
public function find(): iterable |
|
47
|
|
|
{ |
|
48
|
34 |
|
foreach ($this->getAvailableDeclarations() as $declaration) { |
|
49
|
34 |
|
if ($this->skipDeclaration($declaration)) { |
|
50
|
26 |
|
continue; |
|
51
|
|
|
} |
|
52
|
26 |
|
yield $declaration; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
34 |
|
protected function getFiles(): Finder |
|
57
|
|
|
{ |
|
58
|
34 |
|
return (new Finder()) |
|
59
|
34 |
|
->in($this->directories) |
|
60
|
34 |
|
->name('*.php') |
|
61
|
34 |
|
->sortByName() |
|
62
|
34 |
|
->files(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param class-string|trait-string $declaration |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
34 |
|
private function skipDeclaration(string $declaration): bool |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
34 |
|
$reflectionClass = self::$reflectionsCache[$declaration] ??= new ReflectionClass($declaration); |
|
72
|
13 |
|
} catch (\Throwable) { |
|
73
|
13 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
34 |
|
if ($reflectionClass->isInternal() || $reflectionClass->isAnonymous()) { |
|
77
|
13 |
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
34 |
|
foreach ($this->filters as $filter) { |
|
81
|
34 |
|
if (!$filter->match($reflectionClass)) { |
|
82
|
26 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
26 |
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return iterable<class-string|trait-string> |
|
91
|
|
|
*/ |
|
92
|
|
|
abstract protected function getAvailableDeclarations(): iterable; |
|
93
|
|
|
} |
|
94
|
|
|
|