LibraryDependencyResolver.php 2.8 KB

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