2 namespace Civi\Angular
;
5 * Manage Angular resources.
7 * @package Civi\Angular
12 * @var \CRM_Core_Resources
14 protected $res = NULL;
20 * Each item has some combination of these keys:
22 * The Civi extension which defines the Angular module.
23 * - js: array(string $relativeFilePath)
24 * List of JS files (relative to the extension).
25 * - css: array(string $relativeFilePath)
26 * List of CSS files (relative to the extension).
27 * - partials: array(string $relativeFilePath)
28 * A list of partial-HTML folders (relative to the extension).
29 * This will be mapped to "~/moduleName" by crmResource.
30 * - settings: array(string $key => mixed $value)
31 * List of settings to preload.
32 * - settingsFactory: callable
33 * Callback function to fetch settings.
34 * - permissions: array
35 * List of permissions to make available client-side
37 * List of other modules required
39 protected $modules = NULL;
42 * @var \CRM_Utils_Cache_Interface
48 * Array(string $name => ChangeSet $change).
50 protected $changeSets = NULL;
53 * @param \CRM_Core_Resources $res
54 * The resource manager.
57 public function __construct($res, \CRM_Utils_Cache_Interface
$cache = NULL) {
59 $this->cache
= $cache ?
$cache : new \
CRM_Utils_Cache_ArrayCache([]);
63 * Clear out any runtime-cached metadata.
65 * This is useful if, eg, you have recently added or destroyed Angular modules.
69 public function clear() {
70 $this->cache
->clear();
71 $this->modules
= NULL;
72 $this->changeSets
= NULL;
77 * Get a list of AngularJS modules which should be autoloaded.
80 * Each item has some combination of these keys:
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.
90 * - settings: array(string $key => mixed $value)
91 * List of settings to preload.
93 public function getModules() {
94 if ($this->modules
=== NULL) {
95 $config = \CRM_Core_Config
::singleton();
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.
101 $angularModules = [];
102 $angularModules['angularFileUpload'] = include "$civicrm_root/ang/angularFileUpload.ang.php";
103 $angularModules['checklist-model'] = include "$civicrm_root/ang/checklist-model.ang.php";
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";
108 // $angularModules['crmExample'] = include "$civicrm_root/ang/crmExample.ang.php";
109 $angularModules['crmResource'] = include "$civicrm_root/ang/crmResource.ang.php";
110 $angularModules['crmRouteBinder'] = include "$civicrm_root/ang/crmRouteBinder.ang.php";
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";
114 $angularModules['ngRoute'] = include "$civicrm_root/ang/ngRoute.ang.php";
115 $angularModules['ngSanitize'] = include "$civicrm_root/ang/ngSanitize.ang.php";
116 $angularModules['ui.utils'] = include "$civicrm_root/ang/ui.utils.ang.php";
117 $angularModules['ui.bootstrap'] = include "$civicrm_root/ang/ui.bootstrap.ang.php";
118 $angularModules['ui.sortable'] = include "$civicrm_root/ang/ui.sortable.ang.php";
119 $angularModules['unsavedChanges'] = include "$civicrm_root/ang/unsavedChanges.ang.php";
120 $angularModules['statuspage'] = include "$civicrm_root/ang/crmStatusPage.ang.php";
121 $angularModules['exportui'] = include "$civicrm_root/ang/exportui.ang.php";
122 $angularModules['api4Explorer'] = include "$civicrm_root/ang/api4Explorer.ang.php";
123 $angularModules['api4'] = include "$civicrm_root/ang/api4.ang.php";
125 foreach (\CRM_Core_Component
::getEnabledComponents() as $component) {
126 $angularModules = array_merge($angularModules, $component->getAngularModules());
128 \CRM_Utils_Hook
::angularModules($angularModules);
129 foreach ($angularModules as $module => $info) {
131 $angularModules[$module] +
= ['basePages' => ['civicrm/a']];
132 // Validate settingsFactory callables
133 if (isset($info['settingsFactory'])) {
134 // To keep the cache small, we want `settingsFactory` to contain the string names of class & function, not an object
135 if (!is_array($info['settingsFactory']) && !is_string($info['settingsFactory'])) {
136 throw new \
CRM_Core_Exception($module . ' settingsFactory must be a callable array or string');
138 // To keep the cache small, convert full object to just the class name
139 if (is_array($info['settingsFactory']) && is_object($info['settingsFactory'][0])) {
140 $angularModules[$module]['settingsFactory'][0] = get_class($info['settingsFactory'][0]);
144 $this->modules
= $this->resolvePatterns($angularModules);
147 return $this->modules
;
151 * Get the descriptor for an Angular module.
153 * @param string $name
156 * Details about the module:
157 * - ext: string, the name of the Civi extension which defines the module
158 * - js: array(string $relativeFilePath).
159 * - css: array(string $relativeFilePath).
160 * - partials: array(string $relativeFilePath).
163 public function getModule($name) {
164 $modules = $this->getModules();
165 if (!isset($modules[$name])) {
166 throw new \
Exception("Unrecognized Angular module");
168 return $modules[$name];
172 * Resolve a full list of Angular dependencies.
174 * @param array $names
175 * List of Angular modules.
176 * Ex: array('crmMailing').
178 * List of Angular modules, include all dependencies.
179 * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute').
181 public function resolveDependencies($names) {
182 $allModules = $this->getModules();
185 while (($missingModules = array_diff($result, array_keys($visited))) && !empty($missingModules)) {
186 foreach ($missingModules as $module) {
187 $visited[$module] = 1;
188 if (!isset($allModules[$module])) {
189 \Civi
::log()->warning('Unrecognized Angular module {name}. Please ensure that all Angular modules are declared.', [
191 'civi.tag' => 'deprecated',
194 elseif (isset($allModules[$module]['requires'])) {
195 $result = array_unique(array_merge($result, $allModules[$module]['requires']));
204 * Get a list of Angular modules that should be loaded on the given
207 * @param string $basePage
208 * The name of the base-page for which we want a list of moudles.
210 * List of Angular modules.
211 * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute').
213 public function resolveDefaultModules($basePage) {
214 $modules = $this->getModules();
216 foreach ($modules as $moduleName => $module) {
217 if (in_array($basePage, $module['basePages']) ||
in_array('*', $module['basePages'])) {
218 $result[] = $moduleName;
225 * Convert any globs in an Angular module to file names.
227 * @param array $modules
228 * List of Angular modules.
230 * Updated list of Angular modules
232 protected function resolvePatterns($modules) {
235 foreach ($modules as $moduleKey => $module) {
236 foreach (['js', 'css', 'partials'] as $fileset) {
237 if (!isset($module[$fileset])) {
240 $module[$fileset] = $this->res
->glob($module['ext'], $module[$fileset]);
242 $newModules[$moduleKey] = $module;
249 * Get the partial HTML documents for a module (unfiltered).
251 * @param string $name
252 * Angular module name.
254 * Array(string $extFilePath => string $html)
256 * Invalid partials configuration.
258 public function getRawPartials($name) {
259 $module = $this->getModule($name);
260 $result = !empty($module['partialsCallback'])
261 ? \Civi\Core\Resolver
::singleton()->call($module['partialsCallback'], [$name, $module])
263 if (isset($module['partials'])) {
264 foreach ($module['partials'] as $partialDir) {
265 $partialDir = $this->res
->getPath($module['ext']) . '/' . $partialDir;
266 $files = \CRM_Utils_File
::findFiles($partialDir, '*.html', TRUE);
267 foreach ($files as $file) {
268 $filename = '~/' . $name . '/' . $file;
269 $result[$filename] = file_get_contents($partialDir . '/' . $file);
278 * Get the partial HTML documents for a module.
280 * @param string $name
281 * Angular module name.
283 * Array(string $extFilePath => string $html)
285 * Invalid partials configuration.
287 public function getPartials($name) {
288 $cacheKey = "angular-partials_$name";
289 $cacheValue = $this->cache
->get($cacheKey);
290 if ($cacheValue === NULL) {
291 $cacheValue = ChangeSet
::applyResourceFilters($this->getChangeSets(), 'partials', $this->getRawPartials($name));
292 $this->cache
->set($cacheKey, $cacheValue);
298 * Get list of translated strings for a module.
300 * @param string $name
301 * Angular module name.
303 * Translated strings: array(string $orig => string $translated).
305 public function getTranslatedStrings($name) {
306 $module = $this->getModule($name);
308 $strings = $this->getStrings($name);
309 foreach ($strings as $string) {
310 // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]?
311 // It doesn't look like client side really supports the domain right now...
312 $translated = ts($string, [
313 'domain' => [$module['ext'], NULL],
315 if ($translated != $string) {
316 $result[$string] = $translated;
323 * Get list of translatable strings for a module.
325 * @param string $name
326 * Angular module name.
328 * Translatable strings.
330 public function getStrings($name) {
331 $module = $this->getModule($name);
333 if (isset($module['js'])) {
334 foreach ($module['js'] as $file) {
335 $strings = $this->res
->getStrings()->get(
337 $this->res
->getPath($module['ext'], $file),
340 $result = array_unique(array_merge($result, $strings));
343 $partials = $this->getPartials($name);
344 foreach ($partials as $partial) {
345 $result = array_unique(array_merge($result, \CRM_Utils_JS
::parseStrings($partial)));
351 * Get resources for one or more modules.
353 * @param string|array $moduleNames
354 * List of module names.
355 * @param string $resType
356 * Type of resource ('js', 'css', 'settings').
357 * @param string $refType
358 * Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings').
360 * List of URLs or paths.
361 * @throws \CRM_Core_Exception
363 public function getResources($moduleNames, $resType, $refType) {
365 $moduleNames = (array) $moduleNames;
366 foreach ($moduleNames as $moduleName) {
367 $module = $this->getModule($moduleName);
368 if (isset($module[$resType])) {
369 foreach ($module[$resType] as $file) {
371 if (is_string($file) && preg_match(';^(assetBuilder|ext)://;', $file)) {
372 $refTypeSuffix = '-' . parse_url($file, PHP_URL_SCHEME
);
375 switch ($refType . $refTypeSuffix) {
377 $result[] = $this->res
->getPath($module['ext'], $file);
381 $result[] = $this->res
->getUrl($module['ext'], $file);
385 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);
388 case 'path-assetBuilder':
389 $assetName = parse_url($file, PHP_URL_HOST
) . parse_url($file, PHP_URL_PATH
);
391 parse_str('' . parse_url($file, PHP_URL_QUERY
), $assetParams);
392 $result[] = \Civi
::service('asset_builder')->getPath($assetName, $assetParams);
395 case 'rawUrl-assetBuilder':
396 case 'cacheUrl-assetBuilder':
397 $assetName = parse_url($file, PHP_URL_HOST
) . parse_url($file, PHP_URL_PATH
);
399 parse_str('' . parse_url($file, PHP_URL_QUERY
), $assetParams);
400 $result[] = \Civi
::service('asset_builder')->getUrl($assetName, $assetParams);
404 $result[] = $this->res
->getPath(parse_url($file, PHP_URL_HOST
), ltrim(parse_url($file, PHP_URL_PATH
), '/'));
408 $result[] = $this->res
->getUrl(parse_url($file, PHP_URL_HOST
), ltrim(parse_url($file, PHP_URL_PATH
), '/'));
412 $result[] = $this->res
->getUrl(parse_url($file, PHP_URL_HOST
), ltrim(parse_url($file, PHP_URL_PATH
), '/'), TRUE);
416 case 'settingsFactory':
419 if (!empty($module[$resType])) {
420 $result[$moduleName] = $module[$resType];
425 throw new \
CRM_Core_Exception("Unrecognized resource format");
431 return ChangeSet
::applyResourceFilters($this->getChangeSets(), $resType, $result);
436 * Array(string $name => ChangeSet $changeSet).
438 public function getChangeSets() {
439 if ($this->changeSets
=== NULL) {
440 $this->changeSets
= [];
441 \CRM_Utils_Hook
::alterAngular($this);
443 return $this->changeSets
;
447 * @param ChangeSet $changeSet
448 * @return \Civi\Angular\Manager
450 public function add($changeSet) {
451 $this->changeSets
[$changeSet->getName()] = $changeSet;