Chris@0
|
1 <?php
|
Chris@0
|
2 namespace Consolidation\AnnotatedCommand;
|
Chris@0
|
3
|
Chris@0
|
4 use Symfony\Component\Finder\Finder;
|
Chris@0
|
5
|
Chris@0
|
6 /**
|
Chris@0
|
7 * Do discovery presuming that the namespace of the command will
|
Chris@0
|
8 * contain the last component of the path. This is the convention
|
Chris@0
|
9 * that should be used when searching for command files that are
|
Chris@0
|
10 * bundled with the modules of a framework. The convention used
|
Chris@0
|
11 * is that the namespace for a module in a framework should start with
|
Chris@0
|
12 * the framework name followed by the module name.
|
Chris@0
|
13 *
|
Chris@0
|
14 * For example, if base namespace is "Drupal", then a command file in
|
Chris@0
|
15 * modules/default_content/src/CliTools/ExampleCommands.cpp
|
Chris@0
|
16 * will be in the namespace Drupal\default_content\CliTools.
|
Chris@0
|
17 *
|
Chris@0
|
18 * For global locations, the middle component of the namespace is
|
Chris@0
|
19 * omitted. For example, if the base namespace is "Drupal", then
|
Chris@0
|
20 * a command file in __DRUPAL_ROOT__/CliTools/ExampleCommands.cpp
|
Chris@0
|
21 * will be in the namespace Drupal\CliTools.
|
Chris@0
|
22 *
|
Chris@0
|
23 * To discover namespaced commands in modules:
|
Chris@0
|
24 *
|
Chris@0
|
25 * $commandFiles = $discovery->discoverNamespaced($moduleList, '\Drupal');
|
Chris@0
|
26 *
|
Chris@0
|
27 * To discover global commands:
|
Chris@0
|
28 *
|
Chris@0
|
29 * $commandFiles = $discovery->discover($drupalRoot, '\Drupal');
|
Chris@0
|
30 */
|
Chris@0
|
31 class CommandFileDiscovery
|
Chris@0
|
32 {
|
Chris@0
|
33 /** @var string[] */
|
Chris@0
|
34 protected $excludeList;
|
Chris@0
|
35 /** @var string[] */
|
Chris@0
|
36 protected $searchLocations;
|
Chris@0
|
37 /** @var string */
|
Chris@0
|
38 protected $searchPattern = '*Commands.php';
|
Chris@0
|
39 /** @var boolean */
|
Chris@0
|
40 protected $includeFilesAtBase = true;
|
Chris@0
|
41 /** @var integer */
|
Chris@0
|
42 protected $searchDepth = 2;
|
Chris@0
|
43 /** @var bool */
|
Chris@0
|
44 protected $followLinks = false;
|
Chris@0
|
45
|
Chris@0
|
46 public function __construct()
|
Chris@0
|
47 {
|
Chris@0
|
48 $this->excludeList = ['Exclude'];
|
Chris@0
|
49 $this->searchLocations = [
|
Chris@0
|
50 'Command',
|
Chris@0
|
51 'CliTools', // TODO: Maybe remove
|
Chris@0
|
52 ];
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Specify whether to search for files at the base directory
|
Chris@0
|
57 * ($directoryList parameter to discover and discoverNamespaced
|
Chris@0
|
58 * methods), or only in the directories listed in the search paths.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param boolean $includeFilesAtBase
|
Chris@0
|
61 */
|
Chris@0
|
62 public function setIncludeFilesAtBase($includeFilesAtBase)
|
Chris@0
|
63 {
|
Chris@0
|
64 $this->includeFilesAtBase = $includeFilesAtBase;
|
Chris@0
|
65 return $this;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Set the list of excludes to add to the finder, replacing
|
Chris@0
|
70 * whatever was there before.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @param array $excludeList The list of directory names to skip when
|
Chris@0
|
73 * searching for command files.
|
Chris@0
|
74 */
|
Chris@0
|
75 public function setExcludeList($excludeList)
|
Chris@0
|
76 {
|
Chris@0
|
77 $this->excludeList = $excludeList;
|
Chris@0
|
78 return $this;
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Add one more location to the exclude list.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @param string $exclude One directory name to skip when searching
|
Chris@0
|
85 * for command files.
|
Chris@0
|
86 */
|
Chris@0
|
87 public function addExclude($exclude)
|
Chris@0
|
88 {
|
Chris@0
|
89 $this->excludeList[] = $exclude;
|
Chris@0
|
90 return $this;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Set the search depth. By default, fills immediately in the
|
Chris@0
|
95 * base directory are searched, plus all of the search locations
|
Chris@0
|
96 * to this specified depth. If the search locations is set to
|
Chris@0
|
97 * an empty array, then the base directory is searched to this
|
Chris@0
|
98 * depth.
|
Chris@0
|
99 */
|
Chris@0
|
100 public function setSearchDepth($searchDepth)
|
Chris@0
|
101 {
|
Chris@0
|
102 $this->searchDepth = $searchDepth;
|
Chris@0
|
103 return $this;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * Specify that the discovery object should follow symlinks. By
|
Chris@0
|
108 * default, symlinks are not followed.
|
Chris@0
|
109 */
|
Chris@0
|
110 public function followLinks($followLinks = true)
|
Chris@0
|
111 {
|
Chris@0
|
112 $this->followLinks = $followLinks;
|
Chris@0
|
113 return $this;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Set the list of search locations to examine in each directory where
|
Chris@0
|
118 * command files may be found. This replaces whatever was there before.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @param array $searchLocations The list of locations to search for command files.
|
Chris@0
|
121 */
|
Chris@0
|
122 public function setSearchLocations($searchLocations)
|
Chris@0
|
123 {
|
Chris@0
|
124 $this->searchLocations = $searchLocations;
|
Chris@0
|
125 return $this;
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Add one more location to the search location list.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @param string $location One more relative path to search
|
Chris@0
|
132 * for command files.
|
Chris@0
|
133 */
|
Chris@0
|
134 public function addSearchLocation($location)
|
Chris@0
|
135 {
|
Chris@0
|
136 $this->searchLocations[] = $location;
|
Chris@0
|
137 return $this;
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * Specify the pattern / regex used by the finder to search for
|
Chris@0
|
142 * command files.
|
Chris@0
|
143 */
|
Chris@0
|
144 public function setSearchPattern($searchPattern)
|
Chris@0
|
145 {
|
Chris@0
|
146 $this->searchPattern = $searchPattern;
|
Chris@0
|
147 return $this;
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * Given a list of directories, e.g. Drupal modules like:
|
Chris@0
|
152 *
|
Chris@0
|
153 * core/modules/block
|
Chris@0
|
154 * core/modules/dblog
|
Chris@0
|
155 * modules/default_content
|
Chris@0
|
156 *
|
Chris@0
|
157 * Discover command files in any of these locations.
|
Chris@0
|
158 *
|
Chris@0
|
159 * @param string|string[] $directoryList Places to search for commands.
|
Chris@0
|
160 *
|
Chris@0
|
161 * @return array
|
Chris@0
|
162 */
|
Chris@0
|
163 public function discoverNamespaced($directoryList, $baseNamespace = '')
|
Chris@0
|
164 {
|
Chris@0
|
165 return $this->discover($this->convertToNamespacedList((array)$directoryList), $baseNamespace);
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 /**
|
Chris@0
|
169 * Given a simple list containing paths to directories, where
|
Chris@0
|
170 * the last component of the path should appear in the namespace,
|
Chris@0
|
171 * after the base namespace, this function will return an
|
Chris@0
|
172 * associative array mapping the path's basename (e.g. the module
|
Chris@0
|
173 * name) to the directory path.
|
Chris@0
|
174 *
|
Chris@0
|
175 * Module names must be unique.
|
Chris@0
|
176 *
|
Chris@0
|
177 * @param string[] $directoryList A list of module locations
|
Chris@0
|
178 *
|
Chris@0
|
179 * @return array
|
Chris@0
|
180 */
|
Chris@0
|
181 public function convertToNamespacedList($directoryList)
|
Chris@0
|
182 {
|
Chris@0
|
183 $namespacedArray = [];
|
Chris@0
|
184 foreach ((array)$directoryList as $directory) {
|
Chris@0
|
185 $namespacedArray[basename($directory)] = $directory;
|
Chris@0
|
186 }
|
Chris@0
|
187 return $namespacedArray;
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 /**
|
Chris@0
|
191 * Search for command files in the specified locations. This is the function that
|
Chris@0
|
192 * should be used for all locations that are NOT modules of a framework.
|
Chris@0
|
193 *
|
Chris@0
|
194 * @param string|string[] $directoryList Places to search for commands.
|
Chris@0
|
195 * @return array
|
Chris@0
|
196 */
|
Chris@0
|
197 public function discover($directoryList, $baseNamespace = '')
|
Chris@0
|
198 {
|
Chris@0
|
199 $commandFiles = [];
|
Chris@0
|
200 foreach ((array)$directoryList as $key => $directory) {
|
Chris@0
|
201 $itemsNamespace = $this->joinNamespace([$baseNamespace, $key]);
|
Chris@0
|
202 $commandFiles = array_merge(
|
Chris@0
|
203 $commandFiles,
|
Chris@0
|
204 $this->discoverCommandFiles($directory, $itemsNamespace),
|
Chris@0
|
205 $this->discoverCommandFiles("$directory/src", $itemsNamespace)
|
Chris@0
|
206 );
|
Chris@0
|
207 }
|
Chris@0
|
208 return $commandFiles;
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 /**
|
Chris@0
|
212 * Search for command files in specific locations within a single directory.
|
Chris@0
|
213 *
|
Chris@0
|
214 * In each location, we will accept only a few places where command files
|
Chris@0
|
215 * can be found. This will reduce the need to search through many unrelated
|
Chris@0
|
216 * files.
|
Chris@0
|
217 *
|
Chris@0
|
218 * The default search locations include:
|
Chris@0
|
219 *
|
Chris@0
|
220 * .
|
Chris@0
|
221 * CliTools
|
Chris@0
|
222 * src/CliTools
|
Chris@0
|
223 *
|
Chris@0
|
224 * The pattern we will look for is any file whose name ends in 'Commands.php'.
|
Chris@0
|
225 * A list of paths to found files will be returned.
|
Chris@0
|
226 */
|
Chris@0
|
227 protected function discoverCommandFiles($directory, $baseNamespace)
|
Chris@0
|
228 {
|
Chris@0
|
229 $commandFiles = [];
|
Chris@0
|
230 // In the search location itself, we will search for command files
|
Chris@0
|
231 // immediately inside the directory only.
|
Chris@0
|
232 if ($this->includeFilesAtBase) {
|
Chris@0
|
233 $commandFiles = $this->discoverCommandFilesInLocation(
|
Chris@0
|
234 $directory,
|
Chris@0
|
235 $this->getBaseDirectorySearchDepth(),
|
Chris@0
|
236 $baseNamespace
|
Chris@0
|
237 );
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 // In the other search locations,
|
Chris@0
|
241 foreach ($this->searchLocations as $location) {
|
Chris@0
|
242 $itemsNamespace = $this->joinNamespace([$baseNamespace, $location]);
|
Chris@0
|
243 $commandFiles = array_merge(
|
Chris@0
|
244 $commandFiles,
|
Chris@0
|
245 $this->discoverCommandFilesInLocation(
|
Chris@0
|
246 "$directory/$location",
|
Chris@0
|
247 $this->getSearchDepth(),
|
Chris@0
|
248 $itemsNamespace
|
Chris@0
|
249 )
|
Chris@0
|
250 );
|
Chris@0
|
251 }
|
Chris@0
|
252 return $commandFiles;
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 /**
|
Chris@0
|
256 * Return a Finder search depth appropriate for our selected search depth.
|
Chris@0
|
257 *
|
Chris@0
|
258 * @return string
|
Chris@0
|
259 */
|
Chris@0
|
260 protected function getSearchDepth()
|
Chris@0
|
261 {
|
Chris@0
|
262 return $this->searchDepth <= 0 ? '== 0' : '<= ' . $this->searchDepth;
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * Return a Finder search depth for the base directory. If the
|
Chris@0
|
267 * searchLocations array has been populated, then we will only search
|
Chris@0
|
268 * for files immediately inside the base directory; no traversal into
|
Chris@0
|
269 * deeper directories will be done, as that would conflict with the
|
Chris@0
|
270 * specification provided by the search locations. If there is no
|
Chris@0
|
271 * search location, then we will search to whatever depth was specified
|
Chris@0
|
272 * by the client.
|
Chris@0
|
273 *
|
Chris@0
|
274 * @return string
|
Chris@0
|
275 */
|
Chris@0
|
276 protected function getBaseDirectorySearchDepth()
|
Chris@0
|
277 {
|
Chris@0
|
278 if (!empty($this->searchLocations)) {
|
Chris@0
|
279 return '== 0';
|
Chris@0
|
280 }
|
Chris@0
|
281 return $this->getSearchDepth();
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 /**
|
Chris@0
|
285 * Search for command files in just one particular location. Returns
|
Chris@0
|
286 * an associative array mapping from the pathname of the file to the
|
Chris@0
|
287 * classname that it contains. The pathname may be ignored if the search
|
Chris@0
|
288 * location is included in the autoloader.
|
Chris@0
|
289 *
|
Chris@0
|
290 * @param string $directory The location to search
|
Chris@0
|
291 * @param string $depth How deep to search (e.g. '== 0' or '< 2')
|
Chris@0
|
292 * @param string $baseNamespace Namespace to prepend to each classname
|
Chris@0
|
293 *
|
Chris@0
|
294 * @return array
|
Chris@0
|
295 */
|
Chris@0
|
296 protected function discoverCommandFilesInLocation($directory, $depth, $baseNamespace)
|
Chris@0
|
297 {
|
Chris@0
|
298 if (!is_dir($directory)) {
|
Chris@0
|
299 return [];
|
Chris@0
|
300 }
|
Chris@0
|
301 $finder = $this->createFinder($directory, $depth);
|
Chris@0
|
302
|
Chris@0
|
303 $commands = [];
|
Chris@0
|
304 foreach ($finder as $file) {
|
Chris@0
|
305 $relativePathName = $file->getRelativePathname();
|
Chris@0
|
306 $relativeNamespaceAndClassname = str_replace(
|
Chris@0
|
307 ['/', '.php'],
|
Chris@0
|
308 ['\\', ''],
|
Chris@0
|
309 $relativePathName
|
Chris@0
|
310 );
|
Chris@0
|
311 $classname = $this->joinNamespace([$baseNamespace, $relativeNamespaceAndClassname]);
|
Chris@0
|
312 $commandFilePath = $this->joinPaths([$directory, $relativePathName]);
|
Chris@0
|
313 $commands[$commandFilePath] = $classname;
|
Chris@0
|
314 }
|
Chris@0
|
315
|
Chris@0
|
316 return $commands;
|
Chris@0
|
317 }
|
Chris@0
|
318
|
Chris@0
|
319 /**
|
Chris@0
|
320 * Create a Finder object for use in searching a particular directory
|
Chris@0
|
321 * location.
|
Chris@0
|
322 *
|
Chris@0
|
323 * @param string $directory The location to search
|
Chris@0
|
324 * @param string $depth The depth limitation
|
Chris@0
|
325 *
|
Chris@0
|
326 * @return Finder
|
Chris@0
|
327 */
|
Chris@0
|
328 protected function createFinder($directory, $depth)
|
Chris@0
|
329 {
|
Chris@0
|
330 $finder = new Finder();
|
Chris@0
|
331 $finder->files()
|
Chris@0
|
332 ->name($this->searchPattern)
|
Chris@0
|
333 ->in($directory)
|
Chris@0
|
334 ->depth($depth);
|
Chris@0
|
335
|
Chris@0
|
336 foreach ($this->excludeList as $item) {
|
Chris@0
|
337 $finder->exclude($item);
|
Chris@0
|
338 }
|
Chris@0
|
339
|
Chris@0
|
340 if ($this->followLinks) {
|
Chris@0
|
341 $finder->followLinks();
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 return $finder;
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 /**
|
Chris@0
|
348 * Combine the items of the provied array into a backslash-separated
|
Chris@0
|
349 * namespace string. Empty and numeric items are omitted.
|
Chris@0
|
350 *
|
Chris@0
|
351 * @param array $namespaceParts List of components of a namespace
|
Chris@0
|
352 *
|
Chris@0
|
353 * @return string
|
Chris@0
|
354 */
|
Chris@0
|
355 protected function joinNamespace(array $namespaceParts)
|
Chris@0
|
356 {
|
Chris@0
|
357 return $this->joinParts(
|
Chris@0
|
358 '\\',
|
Chris@0
|
359 $namespaceParts,
|
Chris@0
|
360 function ($item) {
|
Chris@0
|
361 return !is_numeric($item) && !empty($item);
|
Chris@0
|
362 }
|
Chris@0
|
363 );
|
Chris@0
|
364 }
|
Chris@0
|
365
|
Chris@0
|
366 /**
|
Chris@0
|
367 * Combine the items of the provied array into a slash-separated
|
Chris@0
|
368 * pathname. Empty items are omitted.
|
Chris@0
|
369 *
|
Chris@0
|
370 * @param array $pathParts List of components of a path
|
Chris@0
|
371 *
|
Chris@0
|
372 * @return string
|
Chris@0
|
373 */
|
Chris@0
|
374 protected function joinPaths(array $pathParts)
|
Chris@0
|
375 {
|
Chris@0
|
376 $path = $this->joinParts(
|
Chris@0
|
377 '/',
|
Chris@0
|
378 $pathParts,
|
Chris@0
|
379 function ($item) {
|
Chris@0
|
380 return !empty($item);
|
Chris@0
|
381 }
|
Chris@0
|
382 );
|
Chris@0
|
383 return str_replace(DIRECTORY_SEPARATOR, '/', $path);
|
Chris@0
|
384 }
|
Chris@0
|
385
|
Chris@0
|
386 /**
|
Chris@0
|
387 * Simple wrapper around implode and array_filter.
|
Chris@0
|
388 *
|
Chris@0
|
389 * @param string $delimiter
|
Chris@0
|
390 * @param array $parts
|
Chris@0
|
391 * @param callable $filterFunction
|
Chris@0
|
392 */
|
Chris@0
|
393 protected function joinParts($delimiter, $parts, $filterFunction)
|
Chris@0
|
394 {
|
Chris@0
|
395 $parts = array_map(
|
Chris@0
|
396 function ($item) use ($delimiter) {
|
Chris@0
|
397 return rtrim($item, $delimiter);
|
Chris@0
|
398 },
|
Chris@0
|
399 $parts
|
Chris@0
|
400 );
|
Chris@0
|
401 return implode(
|
Chris@0
|
402 $delimiter,
|
Chris@0
|
403 array_filter($parts, $filterFunction)
|
Chris@0
|
404 );
|
Chris@0
|
405 }
|
Chris@0
|
406 }
|