2 namespace Consolidation\AnnotatedCommand;
4 use Symfony\Component\Finder\Finder;
7 * Do discovery presuming that the namespace of the command will
8 * contain the last component of the path. This is the convention
9 * that should be used when searching for command files that are
10 * bundled with the modules of a framework. The convention used
11 * is that the namespace for a module in a framework should start with
12 * the framework name followed by the module name.
14 * For example, if base namespace is "Drupal", then a command file in
15 * modules/default_content/src/CliTools/ExampleCommands.cpp
16 * will be in the namespace Drupal\default_content\CliTools.
18 * For global locations, the middle component of the namespace is
19 * omitted. For example, if the base namespace is "Drupal", then
20 * a command file in __DRUPAL_ROOT__/CliTools/ExampleCommands.cpp
21 * will be in the namespace Drupal\CliTools.
23 * To discover namespaced commands in modules:
25 * $commandFiles = $discovery->discoverNamespaced($moduleList, '\Drupal');
27 * To discover global commands:
29 * $commandFiles = $discovery->discover($drupalRoot, '\Drupal');
31 class CommandFileDiscovery
34 protected $excludeList;
36 protected $searchLocations;
38 protected $searchPattern = '*Commands.php';
40 protected $includeFilesAtBase = true;
42 protected $searchDepth = 2;
44 public function __construct()
46 $this->excludeList = ['Exclude'];
47 $this->searchLocations = [
49 'CliTools', // TODO: Maybe remove
54 * Specify whether to search for files at the base directory
55 * ($directoryList parameter to discover and discoverNamespaced
56 * methods), or only in the directories listed in the search paths.
58 * @param boolean $includeFilesAtBase
60 public function setIncludeFilesAtBase($includeFilesAtBase)
62 $this->includeFilesAtBase = $includeFilesAtBase;
67 * Set the list of excludes to add to the finder, replacing
68 * whatever was there before.
70 * @param array $excludeList The list of directory names to skip when
71 * searching for command files.
73 public function setExcludeList($excludeList)
75 $this->excludeList = $excludeList;
80 * Add one more location to the exclude list.
82 * @param string $exclude One directory name to skip when searching
85 public function addExclude($exclude)
87 $this->excludeList[] = $exclude;
92 * Set the search depth. By default, fills immediately in the
93 * base directory are searched, plus all of the search locations
94 * to this specified depth. If the search locations is set to
95 * an empty array, then the base directory is searched to this
98 public function setSearchDepth($searchDepth)
100 $this->searchDepth = $searchDepth;
105 * Set the list of search locations to examine in each directory where
106 * command files may be found. This replaces whatever was there before.
108 * @param array $searchLocations The list of locations to search for command files.
110 public function setSearchLocations($searchLocations)
112 $this->searchLocations = $searchLocations;
117 * Add one more location to the search location list.
119 * @param string $location One more relative path to search
122 public function addSearchLocation($location)
124 $this->searchLocations[] = $location;
129 * Specify the pattern / regex used by the finder to search for
132 public function setSearchPattern($searchPattern)
134 $this->searchPattern = $searchPattern;
139 * Given a list of directories, e.g. Drupal modules like:
143 * modules/default_content
145 * Discover command files in any of these locations.
147 * @param string|string[] $directoryList Places to search for commands.
151 public function discoverNamespaced($directoryList, $baseNamespace = '')
153 return $this->discover($this->convertToNamespacedList((array)$directoryList), $baseNamespace);
157 * Given a simple list containing paths to directories, where
158 * the last component of the path should appear in the namespace,
159 * after the base namespace, this function will return an
160 * associative array mapping the path's basename (e.g. the module
161 * name) to the directory path.
163 * Module names must be unique.
165 * @param string[] $directoryList A list of module locations
169 public function convertToNamespacedList($directoryList)
171 $namespacedArray = [];
172 foreach ((array)$directoryList as $directory) {
173 $namespacedArray[basename($directory)] = $directory;
175 return $namespacedArray;
179 * Search for command files in the specified locations. This is the function that
180 * should be used for all locations that are NOT modules of a framework.
182 * @param string|string[] $directoryList Places to search for commands.
185 public function discover($directoryList, $baseNamespace = '')
188 foreach ((array)$directoryList as $key => $directory) {
189 $itemsNamespace = $this->joinNamespace([$baseNamespace, $key]);
190 $commandFiles = array_merge(
192 $this->discoverCommandFiles($directory, $itemsNamespace),
193 $this->discoverCommandFiles("$directory/src", $itemsNamespace)
196 return $commandFiles;
200 * Search for command files in specific locations within a single directory.
202 * In each location, we will accept only a few places where command files
203 * can be found. This will reduce the need to search through many unrelated
206 * The default search locations include:
212 * The pattern we will look for is any file whose name ends in 'Commands.php'.
213 * A list of paths to found files will be returned.
215 protected function discoverCommandFiles($directory, $baseNamespace)
218 // In the search location itself, we will search for command files
219 // immediately inside the directory only.
220 if ($this->includeFilesAtBase) {
221 $commandFiles = $this->discoverCommandFilesInLocation(
223 $this->getBaseDirectorySearchDepth(),
228 // In the other search locations,
229 foreach ($this->searchLocations as $location) {
230 $itemsNamespace = $this->joinNamespace([$baseNamespace, $location]);
231 $commandFiles = array_merge(
233 $this->discoverCommandFilesInLocation(
234 "$directory/$location",
235 $this->getSearchDepth(),
240 return $commandFiles;
244 * Return a Finder search depth appropriate for our selected search depth.
248 protected function getSearchDepth()
250 return $this->searchDepth <= 0 ? '== 0' : '<= ' . $this->searchDepth;
254 * Return a Finder search depth for the base directory. If the
255 * searchLocations array has been populated, then we will only search
256 * for files immediately inside the base directory; no traversal into
257 * deeper directories will be done, as that would conflict with the
258 * specification provided by the search locations. If there is no
259 * search location, then we will search to whatever depth was specified
264 protected function getBaseDirectorySearchDepth()
266 if (!empty($this->searchLocations)) {
269 return $this->getSearchDepth();
273 * Search for command files in just one particular location. Returns
274 * an associative array mapping from the pathname of the file to the
275 * classname that it contains. The pathname may be ignored if the search
276 * location is included in the autoloader.
278 * @param string $directory The location to search
279 * @param string $depth How deep to search (e.g. '== 0' or '< 2')
280 * @param string $baseNamespace Namespace to prepend to each classname
284 protected function discoverCommandFilesInLocation($directory, $depth, $baseNamespace)
286 if (!is_dir($directory)) {
289 $finder = $this->createFinder($directory, $depth);
292 foreach ($finder as $file) {
293 $relativePathName = $file->getRelativePathname();
294 $relativeNamespaceAndClassname = str_replace(
299 $classname = $this->joinNamespace([$baseNamespace, $relativeNamespaceAndClassname]);
300 $commandFilePath = $this->joinPaths([$directory, $relativePathName]);
301 $commands[$commandFilePath] = $classname;
308 * Create a Finder object for use in searching a particular directory
311 * @param string $directory The location to search
312 * @param string $depth The depth limitation
316 protected function createFinder($directory, $depth)
318 $finder = new Finder();
320 ->name($this->searchPattern)
324 foreach ($this->excludeList as $item) {
325 $finder->exclude($item);
332 * Combine the items of the provied array into a backslash-separated
333 * namespace string. Empty and numeric items are omitted.
335 * @param array $namespaceParts List of components of a namespace
339 protected function joinNamespace(array $namespaceParts)
341 return $this->joinParts(
345 return !is_numeric($item) && !empty($item);
351 * Combine the items of the provied array into a slash-separated
352 * pathname. Empty items are omitted.
354 * @param array $pathParts List of components of a path
358 protected function joinPaths(array $pathParts)
360 $path = $this->joinParts(
364 return !empty($item);
367 return str_replace(DIRECTORY_SEPARATOR, '/', $path);
371 * Simple wrapper around implode and array_filter.
373 * @param string $delimiter
374 * @param array $parts
375 * @param callable $filterFunction
377 protected function joinParts($delimiter, $parts, $filterFunction)
380 function ($item) use ($delimiter) {
381 return rtrim($item, $delimiter);
387 array_filter($parts, $filterFunction)