Commit | Line | Data |
---|---|---|
fcf926ad TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | /** | |
13 | * Class CRM_Core_Resources_Bundle | |
14 | * | |
15 | * A bundle is a collection of web resources with the following details: | |
16 | * - Only scripts, styles, and settings are allowed. Free-form markup is not. | |
17 | * - Resources *may* have a 'region'. Hopefully, this is not necessary for most bundles. | |
18 | * - If no 'region' is given, then CRM_Core_Resources will pick a default at activation time. | |
19 | */ | |
20 | class CRM_Core_Resources_Bundle implements CRM_Core_Resources_CollectionInterface { | |
21 | ||
22 | use CRM_Core_Resources_CollectionTrait; | |
23 | ||
24 | /** | |
25 | * Symbolic name for this bundle. | |
26 | * | |
27 | * @var string|null | |
28 | */ | |
29 | public $name; | |
30 | ||
31 | /** | |
aec7c57d EM |
32 | * @param string|null $name |
33 | * @param string[]|null $types | |
0aded46e | 34 | * List of resource-types to permit in this bundle. NULL for a default list. |
8eb00bd1 TO |
35 | * Ex: ['styleFile', 'styleUrl'] |
36 | * The following aliases are allowed: '*all*', '*default*', '*script*', '*style*' | |
fcf926ad | 37 | */ |
0aded46e | 38 | public function __construct($name = NULL, $types = NULL) { |
fcf926ad | 39 | $this->name = $name; |
8eb00bd1 TO |
40 | |
41 | $typeAliases = [ | |
42 | '*all*' => ['script', 'scriptFile', 'scriptUrl', 'settings', 'style', 'styleFile', 'styleUrl', 'markup', 'template', 'callback'], | |
43 | '*default*' => ['script', 'scriptFile', 'scriptUrl', 'settings', 'style', 'styleFile', 'styleUrl'], | |
44 | '*style*' => ['style', 'styleFile', 'styleUrl'], | |
45 | '*script*' => ['script', 'scriptFile', 'scriptUrl'], | |
46 | ]; | |
47 | $mapType = function ($t) use ($typeAliases) { | |
48 | return $typeAliases[$t] ?? [$t]; | |
49 | }; | |
50 | $types = $types ?: ['*default*']; | |
51 | $this->types = array_unique(array_merge(...array_map($mapType, (array) $types))); | |
fcf926ad TO |
52 | } |
53 | ||
924d1dc8 TO |
54 | /** |
55 | * Fill in default values for the 'region' property. | |
56 | * | |
57 | * @return static | |
58 | */ | |
59 | public function fillDefaults() { | |
60 | $this->filter(function ($s) { | |
61 | if (!isset($s['region'])) { | |
62 | if ($s['type'] === 'settings') { | |
63 | $s['region'] = NULL; | |
64 | } | |
65 | elseif (preg_match(';^(markup|template|callback);', $s['type'])) { | |
66 | $s['region'] = 'page-header'; | |
67 | } | |
68 | else { | |
69 | $s['region'] = CRM_Core_Resources_Common::REGION; | |
70 | } | |
71 | } | |
72 | return $s; | |
73 | }); | |
74 | return $this; | |
75 | } | |
76 | ||
fcf926ad | 77 | } |