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