LibraryDependencyResolver.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. $minimal = [];
  62. // Determine each library's dependencies.
  63. $with_deps = [];
  64. foreach ($libraries as $library) {
  65. $with_deps[$library] = $this->getLibrariesWithDependencies([$library]);
  66. }
  67. foreach ($libraries as $library) {
  68. $exists = FALSE;
  69. foreach ($with_deps as $other_library => $dependencies) {
  70. if ($library == $other_library) {
  71. continue;
  72. }
  73. if (in_array($library, $dependencies)) {
  74. $exists = TRUE;
  75. break;
  76. }
  77. }
  78. if (!$exists) {
  79. $minimal[] = $library;
  80. }
  81. }
  82. return $minimal;
  83. }
  84. }