1 | <?php |
||
24 | class Enumerator |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * Path to the root directory, where enumeration should start |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $rootDirectory; |
||
33 | |||
34 | /** |
||
35 | * List of additional include paths, should be below rootDirectory |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | private $includePaths; |
||
40 | |||
41 | /** |
||
42 | * List of additional exclude paths, should be below rootDirectory |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | private $excludePaths; |
||
47 | |||
48 | /** |
||
49 | * Initializes an enumerator |
||
50 | * |
||
51 | * @param string $rootDirectory Path to the root directory |
||
52 | * @param array $includePaths List of additional include paths |
||
53 | * @param array $excludePaths List of additional exclude paths |
||
54 | */ |
||
55 | 7 | public function __construct($rootDirectory, array $includePaths = [], array $excludePaths = []) |
|
61 | |||
62 | /** |
||
63 | * Returns an enumerator for files |
||
64 | * |
||
65 | * @return CallbackFilterIterator|RecursiveIteratorIterator|\IteratorIterator|SplFileInfo[] |
||
66 | * @throws UnexpectedValueException |
||
67 | * @throws InvalidArgumentException |
||
68 | * @throws LogicException |
||
69 | */ |
||
70 | 7 | public function enumerate() |
|
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | * @throws UnexpectedValueException |
||
93 | */ |
||
94 | 7 | private function getInPaths() |
|
113 | |||
114 | /** |
||
115 | * @return array |
||
116 | */ |
||
117 | 7 | private function getExcludePaths() |
|
128 | |||
129 | /** |
||
130 | * Returns a filter callback for enumerating files |
||
131 | * |
||
132 | * @return \Closure |
||
133 | */ |
||
134 | public function getFilter() |
||
174 | |||
175 | /** |
||
176 | * Return the real path of the given file |
||
177 | * |
||
178 | * This is used for testing purpose with virtual file system. |
||
179 | * In a vfs the 'realPath' methode will always return false. |
||
180 | * So we have a chance to mock this single function to return different path. |
||
181 | * |
||
182 | * @param SplFileInfo $file |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | protected function getFileFullPath(SplFileInfo $file) |
||
190 | |||
191 | } |
||
192 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: