Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Asset / LibraryDiscovery.php
1 <?php
2
3 namespace Drupal\Core\Asset;
4
5 use Drupal\Core\Cache\CacheCollectorInterface;
6
7 /**
8  * Discovers available asset libraries in Drupal.
9  */
10 class LibraryDiscovery implements LibraryDiscoveryInterface {
11
12   /**
13    * The library discovery cache collector.
14    *
15    * @var \Drupal\Core\Cache\CacheCollectorInterface
16    */
17   protected $collector;
18
19   /**
20    * The final library definitions, statically cached.
21    *
22    * hook_library_info_alter() and hook_js_settings_alter() allows modules
23    * and themes to dynamically alter a library definition (once per request).
24    *
25    * @var array
26    */
27   protected $libraryDefinitions = [];
28
29   /**
30    * Constructs a new LibraryDiscovery instance.
31    *
32    * @param \Drupal\Core\Cache\CacheCollectorInterface $library_discovery_collector
33    *   The library discovery cache collector.
34    */
35   public function __construct(CacheCollectorInterface $library_discovery_collector) {
36     $this->collector = $library_discovery_collector;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
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;
48       }
49     }
50
51     return $this->libraryDefinitions[$extension];
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getLibraryByName($extension, $name) {
58     $extension = $this->getLibrariesByExtension($extension);
59     return isset($extension[$name]) ? $extension[$name] : FALSE;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function clearCachedDefinitions() {
66     $this->libraryDefinitions = [];
67     $this->collector->clear();
68   }
69
70 }