1 | <?php |
||
31 | class CommandFileDiscovery |
||
32 | { |
||
33 | /** @var string[] */ |
||
34 | protected $excludeList; |
||
35 | /** @var string[] */ |
||
36 | protected $searchLocations; |
||
37 | /** @var string */ |
||
38 | protected $searchPattern = '*Commands.php'; |
||
39 | /** @var boolean */ |
||
40 | protected $includeFilesAtBase = true; |
||
41 | /** @var integer */ |
||
42 | protected $searchDepth = 2; |
||
43 | /** @var bool */ |
||
44 | protected $followLinks = false; |
||
45 | |||
46 | public function __construct() |
||
54 | |||
55 | /** |
||
56 | * Specify whether to search for files at the base directory |
||
57 | * ($directoryList parameter to discover and discoverNamespaced |
||
58 | * methods), or only in the directories listed in the search paths. |
||
59 | * |
||
60 | * @param boolean $includeFilesAtBase |
||
61 | */ |
||
62 | public function setIncludeFilesAtBase($includeFilesAtBase) |
||
67 | |||
68 | /** |
||
69 | * Set the list of excludes to add to the finder, replacing |
||
70 | * whatever was there before. |
||
71 | * |
||
72 | * @param array $excludeList The list of directory names to skip when |
||
73 | * searching for command files. |
||
74 | */ |
||
75 | public function setExcludeList($excludeList) |
||
80 | |||
81 | /** |
||
82 | * Add one more location to the exclude list. |
||
83 | * |
||
84 | * @param string $exclude One directory name to skip when searching |
||
85 | * for command files. |
||
86 | */ |
||
87 | public function addExclude($exclude) |
||
92 | |||
93 | /** |
||
94 | * Set the search depth. By default, fills immediately in the |
||
95 | * base directory are searched, plus all of the search locations |
||
96 | * to this specified depth. If the search locations is set to |
||
97 | * an empty array, then the base directory is searched to this |
||
98 | * depth. |
||
99 | */ |
||
100 | public function setSearchDepth($searchDepth) |
||
105 | |||
106 | /** |
||
107 | * Specify that the discovery object should follow symlinks. By |
||
108 | * default, symlinks are not followed. |
||
109 | */ |
||
110 | public function followLinks($followLinks = true) |
||
115 | |||
116 | /** |
||
117 | * Set the list of search locations to examine in each directory where |
||
118 | * command files may be found. This replaces whatever was there before. |
||
119 | * |
||
120 | * @param array $searchLocations The list of locations to search for command files. |
||
121 | */ |
||
122 | public function setSearchLocations($searchLocations) |
||
127 | |||
128 | /** |
||
129 | * Add one more location to the search location list. |
||
130 | * |
||
131 | * @param string $location One more relative path to search |
||
132 | * for command files. |
||
133 | */ |
||
134 | public function addSearchLocation($location) |
||
139 | |||
140 | /** |
||
141 | * Specify the pattern / regex used by the finder to search for |
||
142 | * command files. |
||
143 | */ |
||
144 | public function setSearchPattern($searchPattern) |
||
149 | |||
150 | /** |
||
151 | * Given a list of directories, e.g. Drupal modules like: |
||
152 | * |
||
153 | * core/modules/block |
||
154 | * core/modules/dblog |
||
155 | * modules/default_content |
||
156 | * |
||
157 | * Discover command files in any of these locations. |
||
158 | * |
||
159 | * @param string|string[] $directoryList Places to search for commands. |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function discoverNamespaced($directoryList, $baseNamespace = '') |
||
167 | |||
168 | /** |
||
169 | * Given a simple list containing paths to directories, where |
||
170 | * the last component of the path should appear in the namespace, |
||
171 | * after the base namespace, this function will return an |
||
172 | * associative array mapping the path's basename (e.g. the module |
||
173 | * name) to the directory path. |
||
174 | * |
||
175 | * Module names must be unique. |
||
176 | * |
||
177 | * @param string[] $directoryList A list of module locations |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | public function convertToNamespacedList($directoryList) |
||
189 | |||
190 | /** |
||
191 | * Search for command files in the specified locations. This is the function that |
||
192 | * should be used for all locations that are NOT modules of a framework. |
||
193 | * |
||
194 | * @param string|string[] $directoryList Places to search for commands. |
||
195 | * @return array |
||
196 | */ |
||
197 | public function discover($directoryList, $baseNamespace = '') |
||
210 | |||
211 | /** |
||
212 | * Search for command files in specific locations within a single directory. |
||
213 | * |
||
214 | * In each location, we will accept only a few places where command files |
||
215 | * can be found. This will reduce the need to search through many unrelated |
||
216 | * files. |
||
217 | * |
||
218 | * The default search locations include: |
||
219 | * |
||
220 | * . |
||
221 | * CliTools |
||
222 | * src/CliTools |
||
223 | * |
||
224 | * The pattern we will look for is any file whose name ends in 'Commands.php'. |
||
225 | * A list of paths to found files will be returned. |
||
226 | */ |
||
227 | protected function discoverCommandFiles($directory, $baseNamespace) |
||
254 | |||
255 | /** |
||
256 | * Return a Finder search depth appropriate for our selected search depth. |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | protected function getSearchDepth() |
||
264 | |||
265 | /** |
||
266 | * Return a Finder search depth for the base directory. If the |
||
267 | * searchLocations array has been populated, then we will only search |
||
268 | * for files immediately inside the base directory; no traversal into |
||
269 | * deeper directories will be done, as that would conflict with the |
||
270 | * specification provided by the search locations. If there is no |
||
271 | * search location, then we will search to whatever depth was specified |
||
272 | * by the client. |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | protected function getBaseDirectorySearchDepth() |
||
283 | |||
284 | /** |
||
285 | * Search for command files in just one particular location. Returns |
||
286 | * an associative array mapping from the pathname of the file to the |
||
287 | * classname that it contains. The pathname may be ignored if the search |
||
288 | * location is included in the autoloader. |
||
289 | * |
||
290 | * @param string $directory The location to search |
||
291 | * @param string $depth How deep to search (e.g. '== 0' or '< 2') |
||
292 | * @param string $baseNamespace Namespace to prepend to each classname |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | protected function discoverCommandFilesInLocation($directory, $depth, $baseNamespace) |
||
318 | |||
319 | /** |
||
320 | * Create a Finder object for use in searching a particular directory |
||
321 | * location. |
||
322 | * |
||
323 | * @param string $directory The location to search |
||
324 | * @param string $depth The depth limitation |
||
325 | * |
||
326 | * @return Finder |
||
327 | */ |
||
328 | protected function createFinder($directory, $depth) |
||
346 | |||
347 | /** |
||
348 | * Combine the items of the provied array into a backslash-separated |
||
349 | * namespace string. Empty and numeric items are omitted. |
||
350 | * |
||
351 | * @param array $namespaceParts List of components of a namespace |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | protected function joinNamespace(array $namespaceParts) |
||
365 | |||
366 | /** |
||
367 | * Combine the items of the provied array into a slash-separated |
||
368 | * pathname. Empty items are omitted. |
||
369 | * |
||
370 | * @param array $pathParts List of components of a path |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | protected function joinPaths(array $pathParts) |
||
385 | |||
386 | /** |
||
387 | * Simple wrapper around implode and array_filter. |
||
388 | * |
||
389 | * @param string $delimiter |
||
390 | * @param array $parts |
||
391 | * @param callable $filterFunction |
||
392 | */ |
||
393 | protected function joinParts($delimiter, $parts, $filterFunction) |
||
406 | } |
||
407 |