class ExtensionDiscovery (View source)

Discovers available extensions in the filesystem.

To also discover test modules, add

Constants

ORIGIN_CORE

Origin directory weight: Core.

ORIGIN_PROFILE

Origin directory weight: Installation profile.

ORIGIN_SITES_ALL

Origin directory weight: sites/all.

ORIGIN_ROOT

Origin directory weight: Site-wide directory.

ORIGIN_PARENT_SITE

Origin directory weight: Parent site directory of a test site environment.

ORIGIN_SITE

Origin directory weight: Site-specific directory.

PHP_FUNCTION_PATTERN

Regular expression to match PHP function names.

Properties

static protected array $files

Previously discovered files keyed by origin directory and extension type.

protected array $profileDirectories

List of installation profile directories to additionally scan.

protected string $root

The app root for the current operation.

protected FileCacheInterface $fileCache

The file cache object.

protected string $sitePath

The site path.

Methods

__construct(string $root, bool $use_file_cache = TRUE, array $profile_directories = NULL, string $site_path = NULL)

Constructs a new ExtensionDiscovery object.

scan(string $type, bool $include_tests = NULL)

Discovers available extensions of a given type.

$this
setProfileDirectoriesFromSettings()

Sets installation profile directories based on current site settings.

array
getProfileDirectories()

Gets the installation profile directories to be scanned.

$this
setProfileDirectories(array $paths = NULL)

Sets explicit profile directories to scan.

filterByProfileDirectories(array $all_files)

Filters out extensions not belonging to the scanned installation profiles.

sort(array $all_files, array $weights)

Sorts the discovered extensions.

process(array $all_files)

Processes the filtered and sorted list of extensions.

array
scanDirectory(string $dir, bool $include_tests)

Recursively scans a base directory for the extensions it contains.

Details

__construct(string $root, bool $use_file_cache = TRUE, array $profile_directories = NULL, string $site_path = NULL)

Constructs a new ExtensionDiscovery object.

Parameters

string $root

The app root.

bool $use_file_cache

Whether file cache should be used.

array $profile_directories

The available profile directories

string $site_path

The path to the site.

Extension[] scan(string $type, bool $include_tests = NULL)

Discovers available extensions of a given type.

Finds all extensions (modules, themes, etc) that exist on the site. It searches in several locations. For instance, to discover all available modules:

Parameters

string $type

The extension type to search for. One of 'profile', 'module', 'theme', or 'theme_engine'.

bool $include_tests

(optional) Whether to explicitly include or exclude test extensions. By default, test extensions are only discovered when in a test environment.

Return Value

Extension[]

An associative array of Extension objects, keyed by extension name.

$this setProfileDirectoriesFromSettings()

Sets installation profile directories based on current site settings.

Return Value

$this

array getProfileDirectories()

Gets the installation profile directories to be scanned.

Return Value

array

A list of installation profile directory paths relative to the system root directory.

$this setProfileDirectories(array $paths = NULL)

Sets explicit profile directories to scan.

Parameters

array $paths

A list of installation profile directory paths relative to the system root directory (without trailing slash) to search for extensions.

Return Value

$this

protected Extension[] filterByProfileDirectories(array $all_files)

Filters out extensions not belonging to the scanned installation profiles.

Parameters

array $all_files

The list of all extensions.

Return Value

Extension[]

The filtered list of extensions.

protected Extension[] sort(array $all_files, array $weights)

Sorts the discovered extensions.

Parameters

array $all_files

The list of all extensions.

array $weights

An array of weights, keyed by originating directory.

Return Value

Extension[]

The sorted list of extensions.

protected Extension[] process(array $all_files)

Processes the filtered and sorted list of extensions.

Extensions discovered in later search paths override earlier, unless they are not compatible with the current version of Drupal core.

Parameters

array $all_files

The sorted list of all extensions that were found.

Return Value

Extension[]

The filtered list of extensions, keyed by extension name.

protected array scanDirectory(string $dir, bool $include_tests)

Recursively scans a base directory for the extensions it contains.

Parameters

string $dir

A relative base directory path to scan, without trailing slash.

bool $include_tests

Whether to include test extensions. If FALSE, all 'tests' directories are excluded in the search.

Return Value

array

An associative array whose keys are extension type names and whose values are associative arrays of \Drupal\Core\Extension\Extension objects, keyed by absolute path name.

See also

RecursiveExtensionFilterIterator