1 | <?php |
||
16 | class Enumerator |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * Path to the root directory, where enumeration should start |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | private $rootDirectory; |
||
25 | |||
26 | /** |
||
27 | * List of additional include paths, should be below rootDirectory |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $includePaths; |
||
32 | |||
33 | /** |
||
34 | * List of additional exclude paths, should be below rootDirectory |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $excludePaths; |
||
39 | |||
40 | /** |
||
41 | * Initializes an enumerator |
||
42 | * |
||
43 | * @param string $rootDirectory Path to the root directory |
||
44 | * @param array $includePaths List of additional include paths |
||
45 | * @param array $excludePaths List of additional exclude paths |
||
46 | */ |
||
47 | 6 | public function __construct($rootDirectory, array $includePaths = [], array $excludePaths = []) |
|
53 | |||
54 | /** |
||
55 | * Returns an enumerator for files |
||
56 | * |
||
57 | * @return \CallbackFilterIterator|\RecursiveIteratorIterator|\SplFileInfo[] |
||
58 | */ |
||
59 | 6 | public function enumerate() |
|
73 | |||
74 | /** |
||
75 | * Returns a filter callback for enumerating files |
||
76 | * |
||
77 | * @return \Closure |
||
78 | */ |
||
79 | 6 | public function getFilter() |
|
80 | { |
||
81 | 6 | $rootDirectory = $this->rootDirectory; |
|
82 | 6 | $includePaths = $this->includePaths; |
|
83 | 6 | $excludePaths = $this->excludePaths; |
|
84 | |||
85 | 6 | return function (\SplFileInfo $file) use ($rootDirectory, $includePaths, $excludePaths) { |
|
86 | |||
87 | 6 | if ($file->getExtension() !== 'php') { |
|
88 | return false; |
||
89 | }; |
||
90 | |||
91 | 6 | $fullPath = $this->getFileFullPath($file); |
|
92 | // Do not touch files that not under rootDirectory |
||
93 | 6 | if (strpos($fullPath, $rootDirectory) !== 0) { |
|
94 | return false; |
||
95 | } |
||
96 | |||
97 | 6 | if (!empty($includePaths)) { |
|
98 | 1 | $found = false; |
|
99 | 1 | foreach ($includePaths as $includePattern) { |
|
100 | 1 | if (fnmatch("{$includePattern}*", $fullPath)) { |
|
101 | 1 | $found = true; |
|
102 | 1 | break; |
|
103 | } |
||
104 | } |
||
105 | 1 | if (!$found) { |
|
106 | return false; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | 6 | foreach ($excludePaths as $excludePattern) { |
|
111 | 4 | if (fnmatch("{$excludePattern}*", $fullPath)) { |
|
112 | 4 | return false; |
|
113 | } |
||
114 | } |
||
115 | |||
116 | 4 | return true; |
|
117 | 6 | }; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Return the real path of the given file |
||
122 | * |
||
123 | * This is used for testing purpose with virtual file system. |
||
124 | * In a vfs the 'realPath' methode will always return false. |
||
125 | * So we have a chance to mock this single function to return different path. |
||
126 | * |
||
127 | * @param \SplFileInfo $file |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | protected function getFileFullPath(\SplFileInfo $file) |
||
135 | |||
136 | } |
||
137 |