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
$followLinks
protected string[] $strippedNamespaces

Methods

__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.

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:

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.

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.

fixNamespaces($commandFiles)

fixNamespaces will alter the namespaces in the commandFiles result to remove the Composer placement directory, if any.

discoverCommandFiles($directory, $baseNamespace)

Search for command files in specific locations within a single directory.

string
getSearchDepth()

Return a Finder search depth appropriate for our selected search depth.

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.

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.

Finder
createFinder(string $directory, string $depth)

Create a Finder object for use in searching a particular directory location.

string
joinNamespace(array $namespaceParts)

Combine the items of the provied array into a backslash-separated namespace string. Empty and numeric items are omitted.

string
joinPaths(array $pathParts)

Combine the items of the provied array into a slash-separated pathname. Empty items are omitted.

joinParts(string $delimiter, array $parts, callable $filterFunction)

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.

Parameters

bool $includeFilesAtBase

setExcludeList(array $excludeList)

Set the list of excludes to add to the finder, replacing whatever was there before.

Parameters

array $excludeList

The list of directory names to skip when searching for command files.

addExclude(string $exclude)

Add one more location to the exclude list.

Parameters

string $exclude

One directory name to skip when searching for command files.

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.

Parameters

$searchDepth

Specify that the discovery object should follow symlinks. By default, symlinks are not followed.

Parameters

$followLinks

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.

Parameters

array $searchLocations

The list of locations to search for command files.

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.

Parameters

$ignore
$base

addSearchLocation(string $location)

Add one more location to the search location list.

Parameters

string $location

One more relative path to search for command files.

setSearchPattern($searchPattern)

Specify the pattern / regex used by the finder to search for command files.

Parameters

$searchPattern

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.

Parameters

string|string[] $directoryList

Places to search for commands.

$baseNamespace

Return Value

array

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.

Parameters

string[] $directoryList

A list of module locations

Return Value

array

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.

Parameters

string|string[] $directoryList

Places to search for commands.

$baseNamespace

Return Value

array

protected fixNamespaces($commandFiles)

fixNamespaces will alter the namespaces in the commandFiles result to remove the Composer placement directory, if any.

Parameters

$commandFiles

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.

Parameters

$directory
$baseNamespace

protected string getSearchDepth()

Return a Finder search depth appropriate for our selected search depth.

Return Value

string

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.

Return Value

string

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.

Parameters

string $directory

The location to search

string $depth

How deep to search (e.g. '== 0' or '< 2')

string $baseNamespace

Namespace to prepend to each classname

Return Value

array

protected Finder createFinder(string $directory, string $depth)

Create a Finder object for use in searching a particular directory location.

Parameters

string $directory

The location to search

string $depth

The depth limitation

Return Value

Finder

protected string joinNamespace(array $namespaceParts)

Combine the items of the provied array into a backslash-separated namespace string. Empty and numeric items are omitted.

Parameters

array $namespaceParts

List of components of a namespace

Return Value

string

protected string joinPaths(array $pathParts)

Combine the items of the provied array into a slash-separated pathname. Empty items are omitted.

Parameters

array $pathParts

List of components of a path

Return Value

string

protected joinParts(string $delimiter, array $parts, callable $filterFunction)

Simple wrapper around implode and array_filter.

Parameters

string $delimiter
array $parts
callable $filterFunction