1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\Instrument\FileSystem; |
12
|
|
|
|
13
|
|
|
use CallbackFilterIterator; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use LogicException; |
16
|
|
|
use RecursiveIteratorIterator; |
17
|
|
|
use SplFileInfo; |
|
|
|
|
18
|
|
|
use Symfony\Component\Finder\Finder; |
19
|
|
|
use UnexpectedValueException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Enumerates files in the concrete directory, applying filtration logic |
23
|
|
|
*/ |
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 = []) |
56
|
|
|
{ |
57
|
7 |
|
$this->rootDirectory = $rootDirectory; |
58
|
7 |
|
$this->includePaths = $includePaths; |
59
|
7 |
|
$this->excludePaths = $excludePaths; |
60
|
7 |
|
} |
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() |
71
|
|
|
{ |
72
|
7 |
|
$finder = new Finder(); |
73
|
7 |
|
$finder->files() |
74
|
7 |
|
->name('*.php') |
75
|
7 |
|
->in($this->getInPaths()); |
76
|
|
|
|
77
|
7 |
|
foreach ($this->getExcludePaths() as $path) { |
78
|
4 |
|
$finder->notPath($path); |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
$iterator = $finder->getIterator(); |
82
|
|
|
|
83
|
7 |
|
if (strpos(PHP_OS, 'WIN') === 0) { |
84
|
|
|
$iterator = new NormalizeIterator($iterator); |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
return $iterator; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
* @throws UnexpectedValueException |
93
|
|
|
*/ |
94
|
7 |
|
private function getInPaths() |
95
|
|
|
{ |
96
|
7 |
|
$inPaths = []; |
97
|
|
|
|
98
|
7 |
|
foreach ($this->includePaths as $path) { |
99
|
1 |
|
if (strpos($path, $this->rootDirectory, 0) === false) { |
100
|
|
|
throw new UnexpectedValueException(sprintf('Path %s is not in %s', $path, $this->rootDirectory)); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$path = str_replace('*', '', $path); |
104
|
1 |
|
$inPaths[] = $path; |
105
|
|
|
} |
106
|
|
|
|
107
|
7 |
|
if (empty($inPaths)) { |
108
|
6 |
|
$inPaths[] = $this->rootDirectory; |
109
|
|
|
} |
110
|
|
|
|
111
|
7 |
|
return $inPaths; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
7 |
|
private function getExcludePaths() |
118
|
|
|
{ |
119
|
7 |
|
$excludePaths = []; |
120
|
|
|
|
121
|
7 |
|
foreach ($this->excludePaths as $path) { |
122
|
4 |
|
$path = str_replace('*', '.*', $path); |
123
|
4 |
|
$excludePaths[] = '#' . str_replace($this->rootDirectory . '/', '', $path) . '#'; |
124
|
|
|
} |
125
|
|
|
|
126
|
7 |
|
return $excludePaths; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns a filter callback for enumerating files |
131
|
|
|
* |
132
|
|
|
* @return \Closure |
133
|
|
|
*/ |
134
|
|
|
public function getFilter() |
135
|
|
|
{ |
136
|
|
|
$rootDirectory = $this->rootDirectory; |
137
|
|
|
$includePaths = $this->includePaths; |
138
|
|
|
$excludePaths = $this->excludePaths; |
139
|
|
|
|
140
|
|
|
return function (SplFileInfo $file) use ($rootDirectory, $includePaths, $excludePaths) { |
141
|
|
|
|
142
|
|
|
if ($file->getExtension() !== 'php') { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$fullPath = $this->getFileFullPath($file); |
147
|
|
|
// Do not touch files that not under rootDirectory |
148
|
|
|
if (strpos($fullPath, $rootDirectory) !== 0) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!empty($includePaths)) { |
153
|
|
|
$found = false; |
154
|
|
|
foreach ($includePaths as $includePattern) { |
155
|
|
|
if (fnmatch("{$includePattern}*", $fullPath, FNM_NOESCAPE)) { |
156
|
|
|
$found = true; |
157
|
|
|
break; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
if (!$found) { |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
foreach ($excludePaths as $excludePattern) { |
166
|
|
|
if (fnmatch("{$excludePattern}*", $fullPath, FNM_NOESCAPE)) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
}; |
173
|
|
|
} |
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) |
187
|
|
|
{ |
188
|
|
|
return $file->getRealPath(); |
189
|
|
|
} |
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: