3 namespace Drupal\Core\Asset;
5 use Drupal\Core\Cache\CacheCollectorInterface;
8 * Discovers available asset libraries in Drupal.
10 class LibraryDiscovery implements LibraryDiscoveryInterface {
13 * The library discovery cache collector.
15 * @var \Drupal\Core\Cache\CacheCollectorInterface
20 * The final library definitions, statically cached.
22 * hook_library_info_alter() and hook_js_settings_alter() allows modules
23 * and themes to dynamically alter a library definition (once per request).
27 protected $libraryDefinitions = [];
30 * Constructs a new LibraryDiscovery instance.
32 * @param \Drupal\Core\Cache\CacheCollectorInterface $library_discovery_collector
33 * The library discovery cache collector.
35 public function __construct(CacheCollectorInterface $library_discovery_collector) {
36 $this->collector = $library_discovery_collector;
42 public function getLibrariesByExtension($extension) {
43 if (!isset($this->libraryDefinitions[$extension])) {
44 $libraries = $this->collector->get($extension);
45 $this->libraryDefinitions[$extension] = [];
46 foreach ($libraries as $name => $definition) {
47 $this->libraryDefinitions[$extension][$name] = $definition;
51 return $this->libraryDefinitions[$extension];
57 public function getLibraryByName($extension, $name) {
58 $extension = $this->getLibrariesByExtension($extension);
59 return isset($extension[$name]) ? $extension[$name] : FALSE;
65 public function clearCachedDefinitions() {
66 $this->libraryDefinitions = [];
67 $this->collector->clear();