class LibraryDiscoveryParser (View source)

Parses library files to get extension data.

Properties

protected ModuleHandlerInterface $moduleHandler

The module handler.

protected ThemeManagerInterface $themeManager

The theme manager.

protected string $root

The app root.

protected StreamWrapperManagerInterface $streamWrapperManager

The stream wrapper manager.

protected LibrariesDirectoryFileFinder $librariesDirectoryFileFinder

The libraries directory file finder.

protected ExtensionPathResolver $extensionPathResolver

The extension path resolver.

Methods

__construct(string $root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, StreamWrapperManagerInterface $stream_wrapper_manager, LibrariesDirectoryFileFinder $libraries_directory_file_finder = NULL, ExtensionPathResolver $extension_path_resolver = NULL)

Constructs a new LibraryDiscoveryParser instance.

array
buildByExtension(string $extension)

Parses and builds up all the libraries information of an extension.

array
parseLibraryInfo(string $extension, string $path)

Parses a given library file and allows modules and themes to alter it.

array
applyLibrariesOverride(array $libraries, string $extension)

Apply libraries overrides specified for the current active theme.

drupalGetPath($type, $name) deprecated

Wraps drupal_get_path().

isValidUri($string)

Determines if the supplied string is a valid URI.

setOverrideValue(array $library, array $sub_key, array $overrides, string $theme_path)

Overrides the specified library asset.

string
resolveThemeAssetPath(string $theme_path, string $overriding_asset)

Ensures that a full path is returned for an overriding theme asset.

static int
validateCssLibrary(array $library)

Validates CSS library structure.

Details

__construct(string $root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, StreamWrapperManagerInterface $stream_wrapper_manager, LibrariesDirectoryFileFinder $libraries_directory_file_finder = NULL, ExtensionPathResolver $extension_path_resolver = NULL)

Constructs a new LibraryDiscoveryParser instance.

Parameters

string $root

The app root.

ModuleHandlerInterface $module_handler

The module handler.

ThemeManagerInterface $theme_manager

The theme manager.

StreamWrapperManagerInterface $stream_wrapper_manager

The stream wrapper manager.

LibrariesDirectoryFileFinder $libraries_directory_file_finder

The libraries directory file finder.

ExtensionPathResolver $extension_path_resolver

The extension path resolver.

array buildByExtension(string $extension)

Parses and builds up all the libraries information of an extension.

Parameters

string $extension

The name of the extension that registered a library.

Return Value

array

All library definitions of the passed extension.

Exceptions

IncompleteLibraryDefinitionException
UnexpectedValueException

protected array parseLibraryInfo(string $extension, string $path)

Parses a given library file and allows modules and themes to alter it.

This method sets the parsed information onto the library property.

Library information is parsed from *.libraries.yml files; see editor.libraries.yml for an example. Every library must have at least one js or css entry. Each entry starts with a machine name and defines the following elements:

  • js: A list of JavaScript files to include. Each file is keyed by the file path. An item can have several attributes (like HTML attributes). For example:

Parameters

string $extension

The name of the extension that registered a library.

string $path

The relative path to the extension.

Return Value

array

An array of parsed library data.

Exceptions

InvalidLibraryFileException

protected array applyLibrariesOverride(array $libraries, string $extension)

Apply libraries overrides specified for the current active theme.

Parameters

array $libraries

The libraries definitions.

string $extension

The extension in which these libraries are defined.

Return Value

array

The modified libraries definitions.

protected drupalGetPath($type, $name) deprecated

deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\Core\Extension\ExtensionList::getPath() instead.

Wraps drupal_get_path().

Parameters

$type
$name

See also

https://www.drupal.org/node/2940438

protected isValidUri($string)

Determines if the supplied string is a valid URI.

Parameters

$string

protected setOverrideValue(array $library, array $sub_key, array $overrides, string $theme_path)

Overrides the specified library asset.

Parameters

array $library

The containing library definition.

array $sub_key

An array containing the sub-keys specifying the library asset, e.g. ['js'] or ['css', 'component'].

array $overrides

Specifies the overrides, this is an array where the key is the asset to be overridden while the value is overriding asset.

string $theme_path

The theme or base theme.

protected string resolveThemeAssetPath(string $theme_path, string $overriding_asset)

Ensures that a full path is returned for an overriding theme asset.

Parameters

string $theme_path

The theme or base theme.

string $overriding_asset

The overriding library asset.

Return Value

string

A fully resolved theme asset path relative to the Drupal directory.

static int validateCssLibrary(array $library)

Validates CSS library structure.

Parameters

array $library

The library definition array.

Return Value

int

Returns based on validity:

  • 0 if the library definition is valid
  • 1 if the library definition has improper nesting
  • 2 if the library definition specifies files as an array