95fca186614fc944976b059e7083913acafb73ec
[yaffs-website] / web / core / lib / Drupal / Core / Asset / LibraryDependencyResolver.php
1 <?php
2
3 namespace Drupal\Core\Asset;
4
5 /**
6  * Resolves the dependencies of asset (CSS/JavaScript) libraries.
7  */
8 class LibraryDependencyResolver implements LibraryDependencyResolverInterface {
9
10   /**
11    * The library discovery service.
12    *
13    * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
14    */
15   protected $libraryDiscovery;
16
17   /**
18    * Constructs a new LibraryDependencyResolver instance.
19    *
20    * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
21    *   The library discovery service.
22    */
23   public function __construct(LibraryDiscoveryInterface $library_discovery) {
24     $this->libraryDiscovery = $library_discovery;
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function getLibrariesWithDependencies(array $libraries) {
31     return $this->doGetDependencies($libraries);
32   }
33
34   /**
35    * Gets the given libraries with its dependencies.
36    *
37    * Helper method for ::getLibrariesWithDependencies().
38    *
39    * @param string[] $libraries_with_unresolved_dependencies
40    *   A list of libraries, with unresolved dependencies, in the order they
41    *   should be loaded.
42    * @param string[] $final_libraries
43    *   The final list of libraries (the return value) that is being built
44    *   recursively.
45    *
46    * @return string[]
47    *   A list of libraries, in the order they should be loaded, including their
48    *   dependencies.
49    */
50   protected function doGetDependencies(array $libraries_with_unresolved_dependencies, array $final_libraries = []) {
51     foreach ($libraries_with_unresolved_dependencies as $library) {
52       if (!in_array($library, $final_libraries)) {
53         list($extension, $name) = explode('/', $library, 2);
54         $definition = $this->libraryDiscovery->getLibraryByName($extension, $name);
55         if (!empty($definition['dependencies'])) {
56           $final_libraries = $this->doGetDependencies($definition['dependencies'], $final_libraries);
57         }
58         $final_libraries[] = $library;
59       }
60     }
61     return $final_libraries;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getMinimalRepresentativeSubset(array $libraries) {
68     $minimal = [];
69
70     // Determine each library's dependencies.
71     $with_deps = [];
72     foreach ($libraries as $library) {
73       $with_deps[$library] = $this->getLibrariesWithDependencies([$library]);
74     }
75
76     foreach ($libraries as $library) {
77       $exists = FALSE;
78       foreach ($with_deps as $other_library => $dependencies) {
79         if ($library == $other_library) {
80           continue;
81         }
82         if (in_array($library, $dependencies)) {
83           $exists = TRUE;
84           break;
85         }
86       }
87       if (!$exists) {
88         $minimal[] = $library;
89       }
90     }
91
92     return $minimal;
93   }
94
95 }