LibraryDiscoveryCollector.php in Drupal 9
File
core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php
View source
<?php
namespace Drupal\Core\Asset;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Asset\Exception\InvalidLibrariesExtendSpecificationException;
use Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException;
use Drupal\Core\Cache\CacheCollector;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
class LibraryDiscoveryCollector extends CacheCollector {
protected $discoveryParser;
protected $themeManager;
public function __construct(CacheBackendInterface $cache, LockBackendInterface $lock, LibraryDiscoveryParser $discovery_parser, ThemeManagerInterface $theme_manager) {
$this->themeManager = $theme_manager;
parent::__construct(NULL, $cache, $lock, [
'library_info',
]);
$this->discoveryParser = $discovery_parser;
}
protected function getCid() {
if (!isset($this->cid)) {
$this->cid = 'library_info:' . $this->themeManager
->getActiveTheme()
->getName();
}
return $this->cid;
}
protected function resolveCacheMiss($key) {
$this->storage[$key] = $this
->getLibraryDefinitions($key);
$this
->persist($key);
return $this->storage[$key];
}
protected function getLibraryDefinitions($extension) {
$libraries = $this->discoveryParser
->buildByExtension($extension);
foreach ($libraries as $name => $definition) {
if (isset($definition['override'])) {
if ($definition['override'] === FALSE) {
unset($libraries[$name]);
}
else {
list($replacement_extension, $replacement_name) = explode('/', $definition['override']);
$replacement_definition = $this
->get($replacement_extension);
if (isset($replacement_definition[$replacement_name])) {
$libraries[$name] = $replacement_definition[$replacement_name];
}
else {
throw new InvalidLibrariesOverrideSpecificationException(sprintf('The specified library %s does not exist.', $definition['override']));
}
}
}
else {
$libraries[$name] = $this
->applyLibrariesExtend($extension, $name, $definition);
}
}
return $libraries;
}
protected function applyLibrariesExtend($extension, $library_name, $library_definition) {
$libraries_extend = $this->themeManager
->getActiveTheme()
->getLibrariesExtend();
if (!empty($libraries_extend["{$extension}/{$library_name}"])) {
foreach ($libraries_extend["{$extension}/{$library_name}"] as $library_extend_name) {
if (isset($library_definition['deprecated'])) {
$extend_message = sprintf('Theme "%s" is extending a deprecated library.', $extension);
$library_deprecation = str_replace('%library_id%', "{$extension}/{$library_name}", $library_definition['deprecated']);
@trigger_error("{$extend_message} {$library_deprecation}", E_USER_DEPRECATED);
}
if (!is_string($library_extend_name)) {
throw new InvalidLibrariesExtendSpecificationException('The libraries-extend specification for each library must be a list of strings.');
}
list($new_extension, $new_library_name) = explode('/', $library_extend_name, 2);
$new_libraries = $this
->get($new_extension);
if (isset($new_libraries[$new_library_name])) {
$library_definition = NestedArray::mergeDeep($library_definition, $new_libraries[$new_library_name]);
}
else {
throw new InvalidLibrariesExtendSpecificationException(sprintf('The specified library "%s" does not exist.', $library_extend_name));
}
}
}
return $library_definition;
}
public function reset() {
parent::reset();
$this->cid = NULL;
}
}