Commit | Line | Data |
---|---|---|
16072ce1 TO |
1 | <?php |
2 | namespace Civi\Angular; | |
3 | ||
4 | /** | |
5 | * Manage Angular resources. | |
6 | * | |
7 | * @package Civi\Angular | |
8 | */ | |
9 | class Manager { | |
10 | ||
11 | /** | |
12 | * @var \CRM_Core_Resources | |
13 | */ | |
14 | protected $res = NULL; | |
15 | ||
16 | /** | |
cc101011 | 17 | * Modules. |
18 | * | |
19 | * @var array|null | |
16072ce1 TO |
20 | * Each item has some combination of these keys: |
21 | * - ext: string | |
8671b4f2 | 22 | * The Civi extension which defines the Angular module. |
16072ce1 | 23 | * - js: array(string $relativeFilePath) |
8671b4f2 | 24 | * List of JS files (relative to the extension). |
16072ce1 | 25 | * - css: array(string $relativeFilePath) |
8671b4f2 | 26 | * List of CSS files (relative to the extension). |
16072ce1 | 27 | * - partials: array(string $relativeFilePath) |
8671b4f2 TO |
28 | * A list of partial-HTML folders (relative to the extension). |
29 | * This will be mapped to "~/moduleName" by crmResource. | |
1da632e0 TO |
30 | * - settings: array(string $key => mixed $value) |
31 | * List of settings to preload. | |
aa882f9b CW |
32 | * - settingsFactory: callable |
33 | * Callback function to fetch settings. | |
66c46618 CW |
34 | * - permissions: array |
35 | * List of permissions to make available client-side | |
aa882f9b CW |
36 | * - requires: array |
37 | * List of other modules required | |
16072ce1 TO |
38 | */ |
39 | protected $modules = NULL; | |
40 | ||
53d00841 TO |
41 | /** |
42 | * @var \CRM_Utils_Cache_Interface | |
43 | */ | |
44 | protected $cache; | |
45 | ||
6dc348de TO |
46 | /** |
47 | * @var array | |
48 | * Array(string $name => ChangeSet $change). | |
49 | */ | |
50 | protected $changeSets = NULL; | |
51 | ||
16072ce1 TO |
52 | /** |
53 | * @param \CRM_Core_Resources $res | |
54 | * The resource manager. | |
34f3bbd9 | 55 | * @param $cache |
16072ce1 | 56 | */ |
53d00841 | 57 | public function __construct($res, \CRM_Utils_Cache_Interface $cache = NULL) { |
16072ce1 | 58 | $this->res = $res; |
c33f1df1 | 59 | $this->cache = $cache ? $cache : new \CRM_Utils_Cache_ArrayCache([]); |
16072ce1 TO |
60 | } |
61 | ||
76b9562a TO |
62 | /** |
63 | * Clear out any runtime-cached metadata. | |
64 | * | |
65 | * This is useful if, eg, you have recently added or destroyed Angular modules. | |
66 | * | |
67 | * @return static | |
68 | */ | |
69 | public function clear() { | |
70 | $this->cache->clear(); | |
71 | $this->modules = NULL; | |
72 | $this->changeSets = NULL; | |
73 | return $this; | |
74 | } | |
75 | ||
16072ce1 | 76 | /** |
fe482240 | 77 | * Get a list of AngularJS modules which should be autoloaded. |
16072ce1 TO |
78 | * |
79 | * @return array | |
8671b4f2 TO |
80 | * Each item has some combination of these keys: |
81 | * - ext: string | |
82 | * The Civi extension which defines the Angular module. | |
83 | * - js: array(string $relativeFilePath) | |
84 | * List of JS files (relative to the extension). | |
85 | * - css: array(string $relativeFilePath) | |
86 | * List of CSS files (relative to the extension). | |
87 | * - partials: array(string $relativeFilePath) | |
88 | * A list of partial-HTML folders (relative to the extension). | |
89 | * This will be mapped to "~/moduleName" by crmResource. | |
1da632e0 TO |
90 | * - settings: array(string $key => mixed $value) |
91 | * List of settings to preload. | |
16072ce1 TO |
92 | */ |
93 | public function getModules() { | |
94 | if ($this->modules === NULL) { | |
1da632e0 | 95 | $config = \CRM_Core_Config::singleton(); |
8456e727 | 96 | global $civicrm_root; |
16072ce1 | 97 | |
8456e727 TO |
98 | // Note: It would be nice to just glob("$civicrm_root/ang/*.ang.php"), but at time |
99 | // of writing CiviMail and CiviCase have special conditionals. | |
16072ce1 | 100 | |
c64f69d9 | 101 | $angularModules = []; |
8456e727 | 102 | $angularModules['angularFileUpload'] = include "$civicrm_root/ang/angularFileUpload.ang.php"; |
b9a4dfbd | 103 | $angularModules['checklist-model'] = include "$civicrm_root/ang/checklist-model.ang.php"; |
8456e727 TO |
104 | $angularModules['crmApp'] = include "$civicrm_root/ang/crmApp.ang.php"; |
105 | $angularModules['crmAttachment'] = include "$civicrm_root/ang/crmAttachment.ang.php"; | |
106 | $angularModules['crmAutosave'] = include "$civicrm_root/ang/crmAutosave.ang.php"; | |
107 | $angularModules['crmCxn'] = include "$civicrm_root/ang/crmCxn.ang.php"; | |
9a78af90 | 108 | $angularModules['crmMonaco'] = include "$civicrm_root/ang/crmMonaco.ang.php"; |
8456e727 | 109 | $angularModules['crmResource'] = include "$civicrm_root/ang/crmResource.ang.php"; |
67341f3c | 110 | $angularModules['crmRouteBinder'] = include "$civicrm_root/ang/crmRouteBinder.ang.php"; |
8456e727 TO |
111 | $angularModules['crmUi'] = include "$civicrm_root/ang/crmUi.ang.php"; |
112 | $angularModules['crmUtil'] = include "$civicrm_root/ang/crmUtil.ang.php"; | |
113 | $angularModules['dialogService'] = include "$civicrm_root/ang/dialogService.ang.php"; | |
10d00207 | 114 | $angularModules['jsonFormatter'] = include "$civicrm_root/ang/jsonFormatter.ang.php"; |
8456e727 TO |
115 | $angularModules['ngRoute'] = include "$civicrm_root/ang/ngRoute.ang.php"; |
116 | $angularModules['ngSanitize'] = include "$civicrm_root/ang/ngSanitize.ang.php"; | |
117 | $angularModules['ui.utils'] = include "$civicrm_root/ang/ui.utils.ang.php"; | |
e88c001e | 118 | $angularModules['ui.bootstrap'] = include "$civicrm_root/ang/ui.bootstrap.ang.php"; |
8456e727 TO |
119 | $angularModules['ui.sortable'] = include "$civicrm_root/ang/ui.sortable.ang.php"; |
120 | $angularModules['unsavedChanges'] = include "$civicrm_root/ang/unsavedChanges.ang.php"; | |
cd6afc75 | 121 | $angularModules['crmStatusPage'] = include "$civicrm_root/ang/crmStatusPage.ang.php"; |
33044927 | 122 | $angularModules['exportui'] = include "$civicrm_root/ang/exportui.ang.php"; |
0b873c9a CW |
123 | $angularModules['api4Explorer'] = include "$civicrm_root/ang/api4Explorer.ang.php"; |
124 | $angularModules['api4'] = include "$civicrm_root/ang/api4.ang.php"; | |
f263929f | 125 | $angularModules['crmDashboard'] = include "$civicrm_root/ang/crmDashboard.ang.php"; |
bca4106f | 126 | $angularModules['crmD3'] = include "$civicrm_root/ang/crmD3.ang.php"; |
c0f7f681 | 127 | |
16072ce1 TO |
128 | foreach (\CRM_Core_Component::getEnabledComponents() as $component) { |
129 | $angularModules = array_merge($angularModules, $component->getAngularModules()); | |
130 | } | |
131 | \CRM_Utils_Hook::angularModules($angularModules); | |
aa882f9b CW |
132 | foreach ($angularModules as $module => $info) { |
133 | // Merge in defaults | |
134 | $angularModules[$module] += ['basePages' => ['civicrm/a']]; | |
135 | // Validate settingsFactory callables | |
136 | if (isset($info['settingsFactory'])) { | |
137 | // To keep the cache small, we want `settingsFactory` to contain the string names of class & function, not an object | |
138 | if (!is_array($info['settingsFactory']) && !is_string($info['settingsFactory'])) { | |
139 | throw new \CRM_Core_Exception($module . ' settingsFactory must be a callable array or string'); | |
140 | } | |
141 | // To keep the cache small, convert full object to just the class name | |
142 | if (is_array($info['settingsFactory']) && is_object($info['settingsFactory'][0])) { | |
143 | $angularModules[$module]['settingsFactory'][0] = get_class($info['settingsFactory'][0]); | |
144 | } | |
8da6c9b8 TO |
145 | } |
146 | } | |
16072ce1 TO |
147 | $this->modules = $this->resolvePatterns($angularModules); |
148 | } | |
149 | ||
150 | return $this->modules; | |
151 | } | |
152 | ||
153 | /** | |
154 | * Get the descriptor for an Angular module. | |
155 | * | |
156 | * @param string $name | |
157 | * Module name. | |
158 | * @return array | |
159 | * Details about the module: | |
160 | * - ext: string, the name of the Civi extension which defines the module | |
161 | * - js: array(string $relativeFilePath). | |
162 | * - css: array(string $relativeFilePath). | |
163 | * - partials: array(string $relativeFilePath). | |
164 | * @throws \Exception | |
165 | */ | |
166 | public function getModule($name) { | |
167 | $modules = $this->getModules(); | |
168 | if (!isset($modules[$name])) { | |
169 | throw new \Exception("Unrecognized Angular module"); | |
170 | } | |
171 | return $modules[$name]; | |
172 | } | |
173 | ||
8da6c9b8 TO |
174 | /** |
175 | * Resolve a full list of Angular dependencies. | |
176 | * | |
177 | * @param array $names | |
178 | * List of Angular modules. | |
179 | * Ex: array('crmMailing'). | |
180 | * @return array | |
181 | * List of Angular modules, include all dependencies. | |
182 | * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute'). | |
51feb18e | 183 | * @throws \CRM_Core_Exception |
8da6c9b8 TO |
184 | */ |
185 | public function resolveDependencies($names) { | |
186 | $allModules = $this->getModules(); | |
c64f69d9 | 187 | $visited = []; |
8da6c9b8 TO |
188 | $result = $names; |
189 | while (($missingModules = array_diff($result, array_keys($visited))) && !empty($missingModules)) { | |
190 | foreach ($missingModules as $module) { | |
191 | $visited[$module] = 1; | |
192 | if (!isset($allModules[$module])) { | |
51feb18e | 193 | throw new \CRM_Core_Exception("Unrecognized Angular module {$module}. Please ensure that all Angular modules are declared."); |
8da6c9b8 TO |
194 | } |
195 | elseif (isset($allModules[$module]['requires'])) { | |
196 | $result = array_unique(array_merge($result, $allModules[$module]['requires'])); | |
197 | } | |
198 | } | |
199 | } | |
200 | sort($result); | |
201 | return $result; | |
202 | } | |
203 | ||
204 | /** | |
205 | * Get a list of Angular modules that should be loaded on the given | |
206 | * base-page. | |
207 | * | |
208 | * @param string $basePage | |
209 | * The name of the base-page for which we want a list of moudles. | |
210 | * @return array | |
211 | * List of Angular modules. | |
212 | * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute'). | |
213 | */ | |
214 | public function resolveDefaultModules($basePage) { | |
215 | $modules = $this->getModules(); | |
c64f69d9 | 216 | $result = []; |
8da6c9b8 TO |
217 | foreach ($modules as $moduleName => $module) { |
218 | if (in_array($basePage, $module['basePages']) || in_array('*', $module['basePages'])) { | |
219 | $result[] = $moduleName; | |
220 | } | |
221 | } | |
222 | return $result; | |
223 | } | |
224 | ||
16072ce1 TO |
225 | /** |
226 | * Convert any globs in an Angular module to file names. | |
227 | * | |
228 | * @param array $modules | |
229 | * List of Angular modules. | |
230 | * @return array | |
231 | * Updated list of Angular modules | |
232 | */ | |
233 | protected function resolvePatterns($modules) { | |
c64f69d9 | 234 | $newModules = []; |
16072ce1 TO |
235 | |
236 | foreach ($modules as $moduleKey => $module) { | |
c64f69d9 | 237 | foreach (['js', 'css', 'partials'] as $fileset) { |
16072ce1 TO |
238 | if (!isset($module[$fileset])) { |
239 | continue; | |
240 | } | |
241 | $module[$fileset] = $this->res->glob($module['ext'], $module[$fileset]); | |
242 | } | |
243 | $newModules[$moduleKey] = $module; | |
244 | } | |
245 | ||
246 | return $newModules; | |
247 | } | |
248 | ||
249 | /** | |
6dc348de | 250 | * Get the partial HTML documents for a module (unfiltered). |
16072ce1 TO |
251 | * |
252 | * @param string $name | |
253 | * Angular module name. | |
254 | * @return array | |
255 | * Array(string $extFilePath => string $html) | |
a2dc0f82 TO |
256 | * @throws \Exception |
257 | * Invalid partials configuration. | |
16072ce1 | 258 | */ |
6dc348de | 259 | public function getRawPartials($name) { |
16072ce1 | 260 | $module = $this->getModule($name); |
90c62ad3 TO |
261 | $result = !empty($module['partialsCallback']) |
262 | ? \Civi\Core\Resolver::singleton()->call($module['partialsCallback'], [$name, $module]) | |
263 | : []; | |
16072ce1 | 264 | if (isset($module['partials'])) { |
a2dc0f82 TO |
265 | foreach ($module['partials'] as $partialDir) { |
266 | $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir; | |
267 | $files = \CRM_Utils_File::findFiles($partialDir, '*.html', TRUE); | |
268 | foreach ($files as $file) { | |
269 | $filename = '~/' . $name . '/' . $file; | |
270 | $result[$filename] = file_get_contents($partialDir . '/' . $file); | |
271 | } | |
16072ce1 | 272 | } |
6dc348de | 273 | return $result; |
16072ce1 TO |
274 | } |
275 | return $result; | |
276 | } | |
277 | ||
6dc348de TO |
278 | /** |
279 | * Get the partial HTML documents for a module. | |
280 | * | |
281 | * @param string $name | |
282 | * Angular module name. | |
283 | * @return array | |
284 | * Array(string $extFilePath => string $html) | |
285 | * @throws \Exception | |
286 | * Invalid partials configuration. | |
287 | */ | |
288 | public function getPartials($name) { | |
8b25b1fe | 289 | $cacheKey = "angular-partials_$name"; |
6dc348de TO |
290 | $cacheValue = $this->cache->get($cacheKey); |
291 | if ($cacheValue === NULL) { | |
292 | $cacheValue = ChangeSet::applyResourceFilters($this->getChangeSets(), 'partials', $this->getRawPartials($name)); | |
293 | $this->cache->set($cacheKey, $cacheValue); | |
294 | } | |
295 | return $cacheValue; | |
296 | } | |
297 | ||
16072ce1 TO |
298 | /** |
299 | * Get list of translated strings for a module. | |
300 | * | |
301 | * @param string $name | |
302 | * Angular module name. | |
303 | * @return array | |
304 | * Translated strings: array(string $orig => string $translated). | |
305 | */ | |
306 | public function getTranslatedStrings($name) { | |
e3d90d6c | 307 | $module = $this->getModule($name); |
c64f69d9 | 308 | $result = []; |
16072ce1 TO |
309 | $strings = $this->getStrings($name); |
310 | foreach ($strings as $string) { | |
311 | // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]? | |
312 | // It doesn't look like client side really supports the domain right now... | |
c64f69d9 CW |
313 | $translated = ts($string, [ |
314 | 'domain' => [$module['ext'], NULL], | |
315 | ]); | |
16072ce1 TO |
316 | if ($translated != $string) { |
317 | $result[$string] = $translated; | |
318 | } | |
319 | } | |
320 | return $result; | |
321 | } | |
322 | ||
323 | /** | |
324 | * Get list of translatable strings for a module. | |
325 | * | |
326 | * @param string $name | |
327 | * Angular module name. | |
328 | * @return array | |
329 | * Translatable strings. | |
330 | */ | |
331 | public function getStrings($name) { | |
332 | $module = $this->getModule($name); | |
c64f69d9 | 333 | $result = []; |
16072ce1 TO |
334 | if (isset($module['js'])) { |
335 | foreach ($module['js'] as $file) { | |
336 | $strings = $this->res->getStrings()->get( | |
337 | $module['ext'], | |
338 | $this->res->getPath($module['ext'], $file), | |
339 | 'text/javascript' | |
340 | ); | |
341 | $result = array_unique(array_merge($result, $strings)); | |
342 | } | |
343 | } | |
53d00841 TO |
344 | $partials = $this->getPartials($name); |
345 | foreach ($partials as $partial) { | |
346 | $result = array_unique(array_merge($result, \CRM_Utils_JS::parseStrings($partial))); | |
16072ce1 TO |
347 | } |
348 | return $result; | |
349 | } | |
350 | ||
351 | /** | |
27a90ef6 TO |
352 | * Get resources for one or more modules. |
353 | * | |
354 | * @param string|array $moduleNames | |
355 | * List of module names. | |
356 | * @param string $resType | |
1da632e0 | 357 | * Type of resource ('js', 'css', 'settings'). |
27a90ef6 | 358 | * @param string $refType |
1da632e0 | 359 | * Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings'). |
16072ce1 | 360 | * @return array |
27a90ef6 TO |
361 | * List of URLs or paths. |
362 | * @throws \CRM_Core_Exception | |
16072ce1 | 363 | */ |
27a90ef6 | 364 | public function getResources($moduleNames, $resType, $refType) { |
c64f69d9 | 365 | $result = []; |
27a90ef6 TO |
366 | $moduleNames = (array) $moduleNames; |
367 | foreach ($moduleNames as $moduleName) { | |
368 | $module = $this->getModule($moduleName); | |
369 | if (isset($module[$resType])) { | |
370 | foreach ($module[$resType] as $file) { | |
e5c376e7 TO |
371 | $refTypeSuffix = ''; |
372 | if (is_string($file) && preg_match(';^(assetBuilder|ext)://;', $file)) { | |
373 | $refTypeSuffix = '-' . parse_url($file, PHP_URL_SCHEME); | |
374 | } | |
375 | ||
376 | switch ($refType . $refTypeSuffix) { | |
27a90ef6 TO |
377 | case 'path': |
378 | $result[] = $this->res->getPath($module['ext'], $file); | |
379 | break; | |
16072ce1 | 380 | |
27a90ef6 TO |
381 | case 'rawUrl': |
382 | $result[] = $this->res->getUrl($module['ext'], $file); | |
383 | break; | |
384 | ||
385 | case 'cacheUrl': | |
386 | $result[] = $this->res->getUrl($module['ext'], $file, TRUE); | |
387 | break; | |
388 | ||
e5c376e7 TO |
389 | case 'path-assetBuilder': |
390 | $assetName = parse_url($file, PHP_URL_HOST) . parse_url($file, PHP_URL_PATH); | |
c64f69d9 | 391 | $assetParams = []; |
e5c376e7 TO |
392 | parse_str('' . parse_url($file, PHP_URL_QUERY), $assetParams); |
393 | $result[] = \Civi::service('asset_builder')->getPath($assetName, $assetParams); | |
394 | break; | |
395 | ||
396 | case 'rawUrl-assetBuilder': | |
397 | case 'cacheUrl-assetBuilder': | |
398 | $assetName = parse_url($file, PHP_URL_HOST) . parse_url($file, PHP_URL_PATH); | |
c64f69d9 | 399 | $assetParams = []; |
e5c376e7 TO |
400 | parse_str('' . parse_url($file, PHP_URL_QUERY), $assetParams); |
401 | $result[] = \Civi::service('asset_builder')->getUrl($assetName, $assetParams); | |
402 | break; | |
403 | ||
404 | case 'path-ext': | |
405 | $result[] = $this->res->getPath(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/')); | |
406 | break; | |
407 | ||
408 | case 'rawUrl-ext': | |
409 | $result[] = $this->res->getUrl(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/')); | |
410 | break; | |
411 | ||
412 | case 'cacheUrl-ext': | |
413 | $result[] = $this->res->getUrl(parse_url($file, PHP_URL_HOST), ltrim(parse_url($file, PHP_URL_PATH), '/'), TRUE); | |
414 | break; | |
415 | ||
1da632e0 | 416 | case 'settings': |
aa882f9b | 417 | case 'settingsFactory': |
5438399c | 418 | case 'requires': |
66c46618 | 419 | case 'permissions': |
d67ff852 | 420 | case 'bundles': |
1da632e0 TO |
421 | if (!empty($module[$resType])) { |
422 | $result[$moduleName] = $module[$resType]; | |
423 | } | |
424 | break; | |
425 | ||
27a90ef6 TO |
426 | default: |
427 | throw new \CRM_Core_Exception("Unrecognized resource format"); | |
428 | } | |
429 | } | |
16072ce1 TO |
430 | } |
431 | } | |
6dc348de TO |
432 | |
433 | return ChangeSet::applyResourceFilters($this->getChangeSets(), $resType, $result); | |
434 | } | |
435 | ||
436 | /** | |
437 | * @return array | |
438 | * Array(string $name => ChangeSet $changeSet). | |
439 | */ | |
440 | public function getChangeSets() { | |
441 | if ($this->changeSets === NULL) { | |
c64f69d9 | 442 | $this->changeSets = []; |
6dc348de TO |
443 | \CRM_Utils_Hook::alterAngular($this); |
444 | } | |
445 | return $this->changeSets; | |
446 | } | |
447 | ||
448 | /** | |
449 | * @param ChangeSet $changeSet | |
450 | * @return \Civi\Angular\Manager | |
451 | */ | |
452 | public function add($changeSet) { | |
453 | $this->changeSets[$changeSet->getName()] = $changeSet; | |
454 | return $this; | |
16072ce1 | 455 | } |
8671b4f2 | 456 | |
16072ce1 | 457 | } |