1<?php
2
3namespace Drupal\Core\Theme;
4
5/**
6 * Defines a theme and its information needed at runtime.
7 *
8 * The theme manager will store the active theme object.
9 *
10 * @see \Drupal\Core\Theme\ThemeManager
11 * @see \Drupal\Core\Theme\ThemeInitialization
12 */
13class ActiveTheme {
14
15  /**
16   * The machine name of the active theme.
17   *
18   * @var string
19   */
20  protected $name;
21
22  /**
23   * The path to the logo.
24   *
25   * @var string
26   */
27  protected $logo;
28
29  /**
30   * The path to the theme.
31   *
32   * @var string
33   */
34  protected $path;
35
36  /**
37   * The engine of the theme.
38   *
39   * @var string
40   */
41  protected $engine;
42
43  /**
44   * The path to the theme engine for root themes.
45   *
46   * @var string
47   */
48  protected $owner;
49
50  /**
51   * An array of base theme active theme objects keyed by name.
52   *
53   * @var static[]
54   *
55   * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
56   *   $this->baseThemeExtensions instead.
57   *
58   * @see https://www.drupal.org/node/3019948
59   */
60  protected $baseThemes;
61
62  /**
63   * An array of base theme extension objects keyed by name.
64   *
65   * @var \Drupal\Core\Extension\Extension[]
66   */
67  protected $baseThemeExtensions = [];
68
69  /**
70   * The extension object.
71   *
72   * @var \Drupal\Core\Extension\Extension
73   */
74  protected $extension;
75
76  /**
77   * The stylesheets which are set to be removed by the theme.
78   *
79   * @var array
80   */
81  protected $styleSheetsRemove;
82
83  /**
84   * The libraries provided by the theme.
85   *
86   * @var array
87   */
88  protected $libraries;
89
90  /**
91   * The regions provided by the theme.
92   *
93   * @var array
94   */
95  protected $regions;
96
97  /**
98   * The libraries or library assets overridden by the theme.
99   *
100   * @var array
101   */
102  protected $librariesOverride;
103
104  /**
105   * The list of libraries-extend definitions.
106   *
107   * @var array
108   */
109  protected $librariesExtend;
110
111  /**
112   * Constructs an ActiveTheme object.
113   *
114   * @param array $values
115   *   The properties of the object, keyed by the names.
116   */
117  public function __construct(array $values) {
118    $values += [
119      'path' => '',
120      'engine' => 'twig',
121      'owner' => 'twig',
122      'logo' => '',
123      'stylesheets_remove' => [],
124      'libraries' => [],
125      'extension' => 'html.twig',
126      'base_theme_extensions' => [],
127      'regions' => [],
128      'libraries_override' => [],
129      'libraries_extend' => [],
130    ];
131
132    $this->name = $values['name'];
133    $this->logo = $values['logo'];
134    $this->path = $values['path'];
135    $this->engine = $values['engine'];
136    $this->owner = $values['owner'];
137    $this->styleSheetsRemove = $values['stylesheets_remove'];
138    $this->libraries = $values['libraries'];
139    $this->extension = $values['extension'];
140    $this->baseThemeExtensions = $values['base_theme_extensions'];
141    if (!empty($values['base_themes']) && empty($this->baseThemeExtensions)) {
142      @trigger_error("The 'base_themes' key is deprecated in Drupal 8.7.0  and support for it will be removed in Drupal 9.0.0. Use 'base_theme_extensions' instead. See https://www.drupal.org/node/3019948", E_USER_DEPRECATED);
143      foreach ($values['base_themes'] as $base_theme) {
144        $this->baseThemeExtensions[$base_theme->getName()] = $base_theme->getExtension();
145      }
146    }
147
148    $this->regions = $values['regions'];
149    $this->librariesOverride = $values['libraries_override'];
150    $this->librariesExtend = $values['libraries_extend'];
151  }
152
153  /**
154   * Returns the machine name of the theme.
155   *
156   * @return string
157   */
158  public function getName() {
159    return $this->name;
160  }
161
162  /**
163   * Returns the path to the theme directory.
164   *
165   * @return string
166   */
167  public function getPath() {
168    return $this->path;
169  }
170
171  /**
172   * Returns the theme engine.
173   *
174   * @return string
175   */
176  public function getEngine() {
177    return $this->engine;
178  }
179
180  /**
181   * Returns the path to the theme engine for root themes.
182   *
183   * @see \Drupal\Core\Extension\ThemeExtensionList::doList()
184   *
185   * @return mixed
186   */
187  public function getOwner() {
188    return $this->owner;
189  }
190
191  /**
192   * Returns the extension object.
193   *
194   * @return \Drupal\Core\Extension\Extension
195   */
196  public function getExtension() {
197    return $this->extension;
198  }
199
200  /**
201   * Returns the libraries provided by the theme.
202   *
203   * @return mixed
204   */
205  public function getLibraries() {
206    return $this->libraries;
207  }
208
209  /**
210   * Returns the removed stylesheets by the theme.
211   *
212   * This method is used as a BC layer to access the contents of the deprecated
213   * stylesheets-remove key in theme info.yml files. It will be removed once it
214   * is no longer needed in Drupal 10.
215   *
216   * @return mixed
217   *   The removed stylesheets.
218   *
219   * @see https://www.drupal.org/node/2497313
220   *
221   * @todo Remove in Drupal 10.0.x.
222   *
223   * @internal
224   */
225  public function getStyleSheetsRemove() {
226    return $this->styleSheetsRemove;
227  }
228
229  /**
230   * Returns an array of base theme active theme objects keyed by name.
231   *
232   * The order starts with the base theme of $this and ends with the root of
233   * the dependency chain.
234   *
235   * @return static[]
236   *
237   * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
238   *   \Drupal\Core\Theme\ActiveTheme::getBaseThemeExtensions() instead.
239   *
240   * @see https://www.drupal.org/node/3019948
241   */
242  public function getBaseThemes() {
243    @trigger_error('\Drupal\Core\Theme\ActiveTheme::getBaseThemes() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Theme\ActiveTheme::getBaseThemeExtensions() instead. See https://www.drupal.org/node/3019948', E_USER_DEPRECATED);
244    /** @var \Drupal\Core\Theme\ThemeInitialization $theme_initialisation */
245    $theme_initialisation = \Drupal::service('theme.initialization');
246    $base_themes = array_combine(array_keys($this->baseThemeExtensions), array_keys($this->baseThemeExtensions));
247    return array_map([$theme_initialisation, 'getActiveThemeByName'], $base_themes);
248  }
249
250  /**
251   * Returns an array of base theme extension objects keyed by name.
252   *
253   * The order starts with the base theme of $this and ends with the root of
254   * the dependency chain.
255   *
256   * @return \Drupal\Core\Extension\Extension[]
257   */
258  public function getBaseThemeExtensions() {
259    return $this->baseThemeExtensions;
260  }
261
262  /**
263   * Returns the logo provided by the theme.
264   *
265   * @return string
266   *   The logo path.
267   */
268  public function getLogo() {
269    return $this->logo;
270  }
271
272  /**
273   * The regions used by the theme.
274   *
275   * @return string[]
276   *   The list of region machine names supported by the theme.
277   *
278   * @see system_region_list()
279   */
280  public function getRegions() {
281    return array_keys($this->regions);
282  }
283
284  /**
285   * Returns the libraries or library assets overridden by the active theme.
286   *
287   * @return array
288   *   The list of libraries overrides.
289   */
290  public function getLibrariesOverride() {
291    return $this->librariesOverride;
292  }
293
294  /**
295   * Returns the libraries extended by the active theme.
296   *
297   * @return array
298   *   The list of libraries-extend definitions.
299   */
300  public function getLibrariesExtend() {
301    return $this->librariesExtend;
302  }
303
304}
305