CommandFileDiscovery
class CommandFileDiscovery (View source)
Do discovery presuming that the namespace of the command will contain the last component of the path. This is the convention that should be used when searching for command files that are bundled with the modules of a framework. The convention used is that the namespace for a module in a framework should start with the framework name followed by the module name.
For example, if base namespace is "Drupal", then a command file in modules/default_content/src/CliTools/ExampleCommands.cpp will be in the namespace Drupal\default_content\CliTools.
For global locations, the middle component of the namespace is omitted. For example, if the base namespace is "Drupal", then a command file in __DRUPAL_ROOT__/CliTools/ExampleCommands.cpp will be in the namespace Drupal\CliTools.
To discover namespaced commands in modules:
$commandFiles = $discovery->discoverNamespaced($moduleList, '\Drupal');
To discover global commands:
$commandFiles = $discovery->discover($drupalRoot, '\Drupal');
WARNING:
This class is deprecated. Commandfile discovery is complicated, and does not work from within phar files. It is recommended to instead use a static list of command classes as shown in https://github.com/g1a/starter/blob/master/example
For a better alternative when implementing a plugin mechanism, see https://robo.li/extending/#register-command-files-via-psr-4-autoloading
Properties
protected string[] | $excludeList | ||
protected string[] | $searchLocations | ||
protected string | $searchPattern | ||
protected bool | $includeFilesAtBase | ||
protected int | $searchDepth | ||
protected bool | $followLinks | ||
protected string[] | $strippedNamespaces |
Methods
No description
Specify whether to search for files at the base directory ($directoryList parameter to discover and discoverNamespaced methods), or only in the directories listed in the search paths.
Set the list of excludes to add to the finder, replacing whatever was there before.
Add one more location to the exclude list.
Set the search depth. By default, fills immediately in the base directory are searched, plus all of the search locations to this specified depth. If the search locations is set to an empty array, then the base directory is searched to this depth.
Specify that the discovery object should follow symlinks. By default, symlinks are not followed.
Set the list of search locations to examine in each directory where command files may be found. This replaces whatever was there before.
Set a particular namespace part to ignore. This is useful in plugin mechanisms where the plugin is placed by Composer.
Add one more location to the search location list.
Specify the pattern / regex used by the finder to search for command files.
Given a list of directories, e.g. Drupal modules like:
Given a simple list containing paths to directories, where the last component of the path should appear in the namespace, after the base namespace, this function will return an associative array mapping the path's basename (e.g. the module name) to the directory path.
Search for command files in the specified locations. This is the function that should be used for all locations that are NOT modules of a framework.
fixNamespaces will alter the namespaces in the commandFiles result to remove the Composer placement directory, if any.
Search for command files in specific locations within a single directory.
Return a Finder search depth appropriate for our selected search depth.
Return a Finder search depth for the base directory. If the searchLocations array has been populated, then we will only search for files immediately inside the base directory; no traversal into deeper directories will be done, as that would conflict with the specification provided by the search locations. If there is no search location, then we will search to whatever depth was specified by the client.
Search for command files in just one particular location. Returns an associative array mapping from the pathname of the file to the classname that it contains. The pathname may be ignored if the search location is included in the autoloader.
Create a Finder object for use in searching a particular directory location.
Combine the items of the provied array into a backslash-separated namespace string. Empty and numeric items are omitted.
Combine the items of the provied array into a slash-separated pathname. Empty items are omitted.
Simple wrapper around implode and array_filter.
Details
__construct()
No description
setIncludeFilesAtBase(bool $includeFilesAtBase)
Specify whether to search for files at the base directory ($directoryList parameter to discover and discoverNamespaced methods), or only in the directories listed in the search paths.
setExcludeList(array $excludeList)
Set the list of excludes to add to the finder, replacing whatever was there before.
addExclude(string $exclude)
Add one more location to the exclude list.
setSearchDepth($searchDepth)
Set the search depth. By default, fills immediately in the base directory are searched, plus all of the search locations to this specified depth. If the search locations is set to an empty array, then the base directory is searched to this depth.
followLinks($followLinks = true)
Specify that the discovery object should follow symlinks. By default, symlinks are not followed.
setSearchLocations(array $searchLocations)
Set the list of search locations to examine in each directory where command files may be found. This replaces whatever was there before.
ignoreNamespacePart($ignore, $base = '')
Set a particular namespace part to ignore. This is useful in plugin mechanisms where the plugin is placed by Composer.
For example, Drush extensions are placed in ./drush/Commands
.
If the Composer installer path is "drush/Commands/contrib/{$name}": ["type:drupal-drush"]
,
then Composer will place the command files in drush/Commands/contrib
.
The namespace should not be any different in this instance than if
the extension were placed in drush/Commands
, though, so Drush therefore
calls ignoreNamespacePart('contrib', 'Commands')
. This causes the
contrib
component to be removed from the namespace if it follows
the namespace Commands
. If the '$base' parameter is not specified, then
the ignored portion of the namespace may appear anywhere in the path.
addSearchLocation(string $location)
Add one more location to the search location list.
setSearchPattern($searchPattern)
Specify the pattern / regex used by the finder to search for command files.
array
discoverNamespaced(string|string[] $directoryList, $baseNamespace = '')
Given a list of directories, e.g. Drupal modules like:
core/modules/block core/modules/dblog modules/default_content
Discover command files in any of these locations.
array
convertToNamespacedList(string[] $directoryList)
Given a simple list containing paths to directories, where the last component of the path should appear in the namespace, after the base namespace, this function will return an associative array mapping the path's basename (e.g. the module name) to the directory path.
Module names must be unique.
array
discover(string|string[] $directoryList, $baseNamespace = '')
Search for command files in the specified locations. This is the function that should be used for all locations that are NOT modules of a framework.
protected
fixNamespaces($commandFiles)
fixNamespaces will alter the namespaces in the commandFiles result to remove the Composer placement directory, if any.
protected
discoverCommandFiles($directory, $baseNamespace)
Search for command files in specific locations within a single directory.
In each location, we will accept only a few places where command files can be found. This will reduce the need to search through many unrelated files.
The default search locations include:
. CliTools src/CliTools
The pattern we will look for is any file whose name ends in 'Commands.php'. A list of paths to found files will be returned.
protected string
getSearchDepth()
Return a Finder search depth appropriate for our selected search depth.
protected string
getBaseDirectorySearchDepth()
Return a Finder search depth for the base directory. If the searchLocations array has been populated, then we will only search for files immediately inside the base directory; no traversal into deeper directories will be done, as that would conflict with the specification provided by the search locations. If there is no search location, then we will search to whatever depth was specified by the client.
protected array
discoverCommandFilesInLocation(string $directory, string $depth, string $baseNamespace)
Search for command files in just one particular location. Returns an associative array mapping from the pathname of the file to the classname that it contains. The pathname may be ignored if the search location is included in the autoloader.
protected Finder
createFinder(string $directory, string $depth)
Create a Finder object for use in searching a particular directory location.
protected string
joinNamespace(array $namespaceParts)
Combine the items of the provied array into a backslash-separated namespace string. Empty and numeric items are omitted.
protected string
joinPaths(array $pathParts)
Combine the items of the provied array into a slash-separated pathname. Empty items are omitted.
protected
joinParts(string $delimiter, array $parts, callable $filterFunction)
Simple wrapper around implode and array_filter.