AttachedAssets.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Drupal\Core\Asset;
  3. /**
  4. * The default attached assets collection.
  5. */
  6. class AttachedAssets implements AttachedAssetsInterface {
  7. /**
  8. * The (ordered) list of asset libraries attached to the current response.
  9. *
  10. * @var string[]
  11. */
  12. public $libraries = [];
  13. /**
  14. * The JavaScript settings attached to the current response.
  15. *
  16. * @var array
  17. */
  18. public $settings = [];
  19. /**
  20. * The set of asset libraries that the client has already loaded.
  21. *
  22. * @var string[]
  23. */
  24. protected $alreadyLoadedLibraries = [];
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function createFromRenderArray(array $render_array) {
  29. if (!isset($render_array['#attached'])) {
  30. throw new \LogicException('The render array has not yet been rendered, hence not all attachments have been collected yet.');
  31. }
  32. $assets = new static();
  33. if (isset($render_array['#attached']['library'])) {
  34. $assets->setLibraries($render_array['#attached']['library']);
  35. }
  36. if (isset($render_array['#attached']['drupalSettings'])) {
  37. $assets->setSettings($render_array['#attached']['drupalSettings']);
  38. }
  39. return $assets;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function setLibraries(array $libraries) {
  45. $this->libraries = array_unique($libraries);
  46. return $this;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getLibraries() {
  52. return $this->libraries;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function setSettings(array $settings) {
  58. $this->settings = $settings;
  59. return $this;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getSettings() {
  65. return $this->settings;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getAlreadyLoadedLibraries() {
  71. return $this->alreadyLoadedLibraries;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function setAlreadyLoadedLibraries(array $libraries) {
  77. $this->alreadyLoadedLibraries = $libraries;
  78. return $this;
  79. }
  80. }