1 | <?php |
||
26 | class Enumerator |
||
27 | { |
||
28 | /** |
||
29 | * Path to the root directory, where enumeration should start |
||
30 | */ |
||
31 | private $rootDirectory; |
||
32 | |||
33 | /** |
||
34 | * List of additional include paths, should be below rootDirectory |
||
35 | */ |
||
36 | private $includePaths; |
||
37 | |||
38 | /** |
||
39 | * List of additional exclude paths, should be below rootDirectory |
||
40 | */ |
||
41 | private $excludePaths; |
||
42 | |||
43 | /** |
||
44 | * Initializes an enumerator |
||
45 | * |
||
46 | * @param string $rootDirectory Path to the root directory |
||
47 | * @param array $includePaths List of additional include paths |
||
48 | * @param array $excludePaths List of additional exclude paths |
||
49 | */ |
||
50 | 8 | public function __construct(string $rootDirectory, array $includePaths = [], array $excludePaths = []) |
|
56 | |||
57 | /** |
||
58 | * Returns an enumerator for files |
||
59 | * |
||
60 | * @return Iterator|SplFileInfo[] |
||
61 | * @throws UnexpectedValueException |
||
62 | * @throws InvalidArgumentException |
||
63 | * @throws LogicException |
||
64 | */ |
||
65 | 7 | public function enumerate(): Iterator |
|
85 | |||
86 | /** |
||
87 | * Returns a filter callback for enumerating files |
||
88 | */ |
||
89 | 18 | public function getFilter(): Closure |
|
90 | { |
||
91 | 1 | $rootDirectory = $this->rootDirectory; |
|
92 | 1 | $includePaths = $this->includePaths; |
|
93 | 1 | $excludePaths = $this->excludePaths; |
|
94 | |||
95 | return function (SplFileInfo $file) use ($rootDirectory, $includePaths, $excludePaths) { |
||
96 | |||
97 | 18 | if ($file->getExtension() !== 'php') { |
|
98 | return false; |
||
99 | } |
||
100 | |||
101 | 18 | $fullPath = $this->getFileFullPath($file); |
|
102 | // Do not touch files that not under rootDirectory |
||
103 | 18 | if (strpos($fullPath, $rootDirectory) !== 0) { |
|
104 | 18 | return false; |
|
105 | } |
||
106 | |||
107 | 1 | if (!empty($includePaths)) { |
|
108 | 1 | $found = false; |
|
109 | 1 | foreach ($includePaths as $includePattern) { |
|
110 | 1 | if (fnmatch("{$includePattern}*", $fullPath, FNM_NOESCAPE)) { |
|
111 | 1 | $found = true; |
|
112 | 1 | break; |
|
113 | } |
||
114 | } |
||
115 | 1 | if (!$found) { |
|
116 | return false; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | 1 | foreach ($excludePaths as $excludePattern) { |
|
121 | 1 | if (fnmatch("{$excludePattern}*", $fullPath, FNM_NOESCAPE)) { |
|
122 | return false; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | 1 | return true; |
|
127 | 1 | }; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Return the real path of the given file |
||
132 | * |
||
133 | * This is used for testing purpose with virtual file system. |
||
134 | * In a vfs the 'realPath' methode will always return false. |
||
135 | * So we have a chance to mock this single function to return different path. |
||
136 | */ |
||
137 | 18 | protected function getFileFullPath(SplFileInfo $file): string |
|
141 | |||
142 | /** |
||
143 | * Returns collection of directories to look at |
||
144 | * |
||
145 | * @throws UnexpectedValueException if directory not under the root |
||
146 | */ |
||
147 | 7 | private function getInPaths(): array |
|
166 | |||
167 | /** |
||
168 | * Returns the list of excluded paths |
||
169 | */ |
||
170 | 7 | private function getExcludePaths(): array |
|
181 | } |
||
182 |