LibraryDependencyResolver.php in Drupal 8
File
core/lib/Drupal/Core/Asset/LibraryDependencyResolver.php
View source
<?php
namespace Drupal\Core\Asset;
class LibraryDependencyResolver implements LibraryDependencyResolverInterface {
protected $libraryDiscovery;
public function __construct(LibraryDiscoveryInterface $library_discovery) {
$this->libraryDiscovery = $library_discovery;
}
public function getLibrariesWithDependencies(array $libraries) {
return $this
->doGetDependencies($libraries);
}
protected function doGetDependencies(array $libraries_with_unresolved_dependencies, array $final_libraries = []) {
foreach ($libraries_with_unresolved_dependencies as $library) {
if (!in_array($library, $final_libraries)) {
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;
}
}
return $final_libraries;
}
public function getMinimalRepresentativeSubset(array $libraries) {
assert(count($libraries) === count(array_unique($libraries)), '$libraries can\'t contain duplicate items.');
$minimal = [];
$with_deps = [];
foreach ($libraries as $library) {
$with_deps[$library] = $this
->getLibrariesWithDependencies([
$library,
]);
}
foreach ($libraries as $library) {
$exists = FALSE;
foreach ($with_deps as $other_library => $dependencies) {
if ($library == $other_library) {
continue;
}
if (in_array($library, $dependencies)) {
$exists = TRUE;
break;
}
}
if (!$exists) {
$minimal[] = $library;
}
}
return $minimal;
}
}