LibraryDependencyResolver.php in Drupal 9
File
core/lib/Drupal/Core/Asset/LibraryDependencyResolver.php
View source
<?php
namespace Drupal\Core\Asset;
class LibraryDependencyResolver implements LibraryDependencyResolverInterface {
protected $libraryDiscovery;
protected $librariesDependencies = [];
public function __construct(LibraryDiscoveryInterface $library_discovery) {
$this->libraryDiscovery = $library_discovery;
}
public function getLibrariesWithDependencies(array $libraries) {
$return = [];
foreach ($libraries as $library) {
if (!isset($this->librariesDependencies[$library])) {
$this->librariesDependencies[$library] = $this
->doGetDependencies([
$library,
]);
}
$return += $this->librariesDependencies[$library];
}
return array_values($return);
}
protected function doGetDependencies(array $libraries_with_unresolved_dependencies, array $final_libraries = []) {
foreach ($libraries_with_unresolved_dependencies as $library) {
if (!isset($final_libraries[$library])) {
list($extension, $name) = explode('/', $library, 2);
$definition = $this->libraryDiscovery
->getLibraryByName($extension, $name);
if (!empty($definition['dependencies'])) {
$final_libraries = $this
->doGetDependencies($definition['dependencies'], $final_libraries);
}
$final_libraries[$library] = $library;
}
}
return $final_libraries;
}
public function getMinimalRepresentativeSubset(array $libraries) {
assert(count($libraries) === count(array_unique($libraries)), '$libraries can\'t contain duplicate items.');
$all_dependencies = [];
foreach ($libraries as $library) {
$with_deps = $this
->getLibrariesWithDependencies([
$library,
]);
$all_dependencies += array_diff($with_deps, [
$library,
]);
}
return array_values(array_diff($libraries, array_intersect($all_dependencies, $libraries)));
}
}