Commit | Line | Data |
---|---|---|
060617e9 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_CollectionInterface | |
14 | * | |
c8cbd3ba TO |
15 | * A resource collection is a mix of *resources* (or *snippets* or *assets*) that can be |
16 | * added to a page. A fully-formed resource might look like this: | |
17 | * | |
18 | * ``` | |
19 | * array( | |
20 | * 'name' => 'jQuery', | |
21 | * 'region' => 'html-header', | |
22 | * 'weight' => 100, | |
23 | * 'type' => 'scriptUrl', | |
24 | * 'scriptUrl' => 'https://example.com/js/jquery.min.js' | |
25 | * ) | |
26 | * ``` | |
27 | * | |
28 | * Typically, a resource is created with just one option, eg | |
29 | * | |
30 | * ``` | |
31 | * // Add resources in array notation | |
32 | * $c->add(['script' => 'alert("Hello");']); | |
33 | * $c->add(['scriptFile' => ['civicrm', 'js/crm.ckeditor.js']]); | |
34 | * $c->add(['scriptUrl' => 'https://example.com/js/jquery.min.js']); | |
35 | * $c->add(['style' => 'p { font-size: 4em; }']); | |
36 | * $c->add(['styleFile' => ['civicrm', 'css/dashboard.css']]); | |
37 | * $c->add(['styleUrl' => 'https://example.com/css/foobar.css']); | |
38 | * | |
39 | * // Add resources with helper methods | |
40 | * $c->addScript('alert("Hello");'); | |
41 | * $c->addScriptFile('civicrm', 'js/crm.ckeditor.js'); | |
42 | * $c->addScriptUrl('https://example.com/js/jquery.min.js'); | |
43 | * $c->addStyle('p { font-size: 4em; }'); | |
44 | * $c->addStyleFile('civicrm', 'css/dashboard.css'); | |
45 | * $c->addStyleUrl('https://example.com/css/foobar.css'); | |
46 | * ``` | |
47 | * | |
48 | * The other properties are automatically computed (dependent upon context), | |
49 | * but they may be set explicitly. These options include: | |
50 | * | |
51 | * - type: string (markup, template, callback, script, scriptFile, scriptUrl, jquery, style, styleFile, styleUrl) | |
52 | * - name: string, symbolic identifier for this resource | |
53 | * - weight: int, default=1. Lower weights come before higher weights. | |
54 | * (If two resources have the same weight, then a secondary ordering will be | |
55 | * used to ensure reproducibility. However, the secondary ordering is | |
56 | * not guaranteed among versions/implementations.) | |
57 | * - disabled: int, default=0 | |
58 | * - region: string | |
59 | * - translate: bool|string, Autoload translations. (Only applies to 'scriptFile') | |
60 | * - FALSE: Do not load translated strings. | |
61 | * - TRUE: Load translated strings. Use the $ext's default domain. | |
62 | * - string: Load translated strings. Use a specific domain. | |
63 | * | |
64 | * For example, the following are equivalent ways to set the 'weight' option: | |
65 | * | |
66 | * ```php | |
67 | * $c->add([ | |
68 | * 'script' => 'alert("Hello");', | |
69 | * 'weight' => 100, | |
70 | * ]); | |
71 | * $c->addScript('alert("Hello");', ['weight' => 100]); | |
72 | * ``` | |
73 | * | |
74 | * Passing options in array (key-value) notation is clearest. For backward | |
75 | * compatibility, some methods (eg `addScript()`) accept options in positional form. | |
76 | * Where applicable, the docblock of each `addFoo()` will include a comment about positional form. | |
950538ac TO |
77 | * |
78 | * @see CRM_Core_Resources_CollectionTrait | |
060617e9 TO |
79 | */ |
80 | interface CRM_Core_Resources_CollectionInterface { | |
81 | ||
82 | /** | |
83 | * Add an item to the collection. For example, when working with 'page-header' collection: | |
84 | * | |
060617e9 TO |
85 | * Note: This function does not perform any extra encoding of markup, script code, or etc. If |
86 | * you're passing in user-data, you must clean it yourself. | |
87 | * | |
88 | * @param array $snippet | |
c8cbd3ba | 89 | * The resource to add. For a full list of properties, see CRM_Core_Resources_CollectionInterface. |
060617e9 TO |
90 | * @return array |
91 | * The full/computed snippet (with defaults applied). | |
c8cbd3ba | 92 | * @see CRM_Core_Resources_CollectionInterface |
060617e9 TO |
93 | */ |
94 | public function add($snippet); | |
95 | ||
96 | /** | |
97 | * Update specific properties of a snippet. | |
98 | * | |
99 | * Ex: $region->update('default', ['disabled' => TRUE]); | |
100 | * | |
101 | * @param string $name | |
c8cbd3ba TO |
102 | * Symbolic of the resource/snippet to update. |
103 | * @param array $snippet | |
104 | * Resource options. See CollectionInterface docs. | |
060617e9 TO |
105 | * @return static |
106 | */ | |
107 | public function update($name, $snippet); | |
108 | ||
109 | /** | |
110 | * Remove all snippets. | |
111 | * | |
112 | * @return static | |
113 | */ | |
114 | public function clear(); | |
115 | ||
116 | /** | |
117 | * Get snippet. | |
118 | * | |
119 | * @param string $name | |
120 | * @return array|NULL | |
121 | */ | |
122 | public function &get($name); | |
123 | ||
124 | /** | |
125 | * Get a list of all snippets in this collection. | |
126 | * | |
127 | * @return iterable | |
128 | */ | |
129 | public function getAll(): iterable; | |
130 | ||
131 | /** | |
132 | * Alter the contents of the collection. | |
133 | * | |
134 | * @param callable $callback | |
135 | * The callback is invoked once for each member in the collection. | |
136 | * The callback may return one of three values: | |
137 | * - TRUE: The item is OK and belongs in the collection. | |
138 | * - FALSE: The item is not OK and should be omitted from the collection. | |
139 | * - Array: The item should be revised (using the returned value). | |
140 | * @return static | |
141 | */ | |
142 | public function filter($callback); | |
143 | ||
144 | /** | |
145 | * Find all snippets which match the given criterion. | |
146 | * | |
147 | * @param callable $callback | |
148 | * The callback is invoked once for each member in the collection. | |
149 | * The callback may return one of two values: | |
150 | * - TRUE: The item is OK and belongs in the collection. | |
151 | * - FALSE: The item is not OK and should be omitted from the collection. | |
152 | * @return iterable | |
153 | * List of matching snippets. | |
154 | */ | |
155 | public function find($callback): iterable; | |
156 | ||
157 | /** | |
158 | * Assimilate a list of resources into this list. | |
159 | * | |
160 | * @param iterable $others | |
161 | * List of snippets to add. | |
162 | * @return static | |
163 | * @see CRM_Core_Resources_CollectionInterface::merge() | |
164 | */ | |
165 | public function merge(iterable $others); | |
166 | ||
167 | } |