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