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;
73 // Force-refresh assetBuilder files
74 \Civi
::container()->get('asset_builder')->clear(FALSE);
79 * Get a list of AngularJS modules which should be autoloaded.
82 * Each item has some combination of these keys:
84 * The Civi extension which defines the Angular module.
85 * - js: array(string $relativeFilePath)
86 * List of JS files (relative to the extension).
87 * - css: array(string $relativeFilePath)
88 * List of CSS files (relative to the extension).
89 * - partials: array(string $relativeFilePath)
90 * A list of partial-HTML folders (relative to the extension).
91 * This will be mapped to "~/moduleName" by crmResource.
92 * - settings: array(string $key => mixed $value)
93 * List of settings to preload.
95 public function getModules() {
96 if ($this->modules
=== NULL) {
97 $config = \CRM_Core_Config
::singleton();
100 // Note: It would be nice to just glob("$civicrm_root/ang/*.ang.php"), but at time
101 // of writing CiviMail and CiviCase have special conditionals.
103 $angularModules = [];
104 $angularModules['angularFileUpload'] = include "$civicrm_root/ang/angularFileUpload.ang.php";
105 $angularModules['checklist-model'] = include "$civicrm_root/ang/checklist-model.ang.php";
106 $angularModules['crmApp'] = include "$civicrm_root/ang/crmApp.ang.php";
107 $angularModules['crmAttachment'] = include "$civicrm_root/ang/crmAttachment.ang.php";
108 $angularModules['crmAutosave'] = include "$civicrm_root/ang/crmAutosave.ang.php";
109 $angularModules['crmCxn'] = include "$civicrm_root/ang/crmCxn.ang.php";
110 $angularModules['crmMonaco'] = include "$civicrm_root/ang/crmMonaco.ang.php";
111 $angularModules['crmResource'] = include "$civicrm_root/ang/crmResource.ang.php";
112 $angularModules['crmRouteBinder'] = include "$civicrm_root/ang/crmRouteBinder.ang.php";
113 $angularModules['crmUi'] = include "$civicrm_root/ang/crmUi.ang.php";
114 $angularModules['crmUtil'] = include "$civicrm_root/ang/crmUtil.ang.php";
115 $angularModules['dialogService'] = include "$civicrm_root/ang/dialogService.ang.php";
116 $angularModules['jsonFormatter'] = include "$civicrm_root/ang/jsonFormatter.ang.php";
117 $angularModules['ngRoute'] = include "$civicrm_root/ang/ngRoute.ang.php";
118 $angularModules['ngSanitize'] = include "$civicrm_root/ang/ngSanitize.ang.php";
119 $angularModules['ui.utils'] = include "$civicrm_root/ang/ui.utils.ang.php";
120 $angularModules['ui.bootstrap'] = include "$civicrm_root/ang/ui.bootstrap.ang.php";
121 $angularModules['ui.sortable'] = include "$civicrm_root/ang/ui.sortable.ang.php";
122 $angularModules['unsavedChanges'] = include "$civicrm_root/ang/unsavedChanges.ang.php";
123 $angularModules['crmStatusPage'] = include "$civicrm_root/ang/crmStatusPage.ang.php";
124 $angularModules['exportui'] = include "$civicrm_root/ang/exportui.ang.php";
125 $angularModules['api4Explorer'] = include "$civicrm_root/ang/api4Explorer.ang.php";
126 $angularModules['api4'] = include "$civicrm_root/ang/api4.ang.php";
127 $angularModules['crmDashboard'] = include "$civicrm_root/ang/crmDashboard.ang.php";
128 $angularModules['crmD3'] = include "$civicrm_root/ang/crmD3.ang.php";
130 foreach (\CRM_Core_Component
::getEnabledComponents() as $component) {
131 $angularModules = array_merge($angularModules, $component->getAngularModules());
133 \CRM_Utils_Hook
::angularModules($angularModules);
134 foreach ($angularModules as $module => $info) {
136 $angularModules[$module] +
= ['basePages' => ['civicrm/a']];
137 // Validate settingsFactory callables
138 if (isset($info['settingsFactory'])) {
139 // To keep the cache small, we want `settingsFactory` to contain the string names of class & function, not an object
140 if (!is_array($info['settingsFactory']) && !is_string($info['settingsFactory'])) {
141 throw new \
CRM_Core_Exception($module . ' settingsFactory must be a callable array or string');
143 // To keep the cache small, convert full object to just the class name
144 if (is_array($info['settingsFactory']) && is_object($info['settingsFactory'][0])) {
145 $angularModules[$module]['settingsFactory'][0] = get_class($info['settingsFactory'][0]);
149 $this->modules
= $this->resolvePatterns($angularModules);
152 return $this->modules
;
156 * Get the descriptor for an Angular module.
158 * @param string $name
161 * Details about the module:
162 * - ext: string, the name of the Civi extension which defines the module
163 * - js: array(string $relativeFilePath).
164 * - css: array(string $relativeFilePath).
165 * - partials: array(string $relativeFilePath).
168 public function getModule($name) {
169 $modules = $this->getModules();
170 if (!isset($modules[$name])) {
171 throw new \
Exception("Unrecognized Angular module");
173 return $modules[$name];
177 * Resolve a full list of Angular dependencies.
179 * @param array $names
180 * List of Angular modules.
181 * Ex: array('crmMailing').
183 * List of Angular modules, include all dependencies.
184 * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute').
185 * @throws \CRM_Core_Exception
187 public function resolveDependencies($names) {
188 $allModules = $this->getModules();
191 while (($missingModules = array_diff($result, array_keys($visited))) && !empty($missingModules)) {
192 foreach ($missingModules as $module) {
193 $visited[$module] = 1;
194 if (!isset($allModules[$module])) {
195 throw new \
CRM_Core_Exception("Unrecognized Angular module {$module}. Please ensure that all Angular modules are declared.");
197 elseif (isset($allModules[$module]['requires'])) {
198 $result = array_unique(array_merge($result, $allModules[$module]['requires']));
207 * Get a list of Angular modules that should be loaded on the given
210 * @param string $basePage
211 * The name of the base-page for which we want a list of moudles.
213 * List of Angular modules.
214 * Ex: array('crmMailing', 'crmUi', 'crmUtil', 'ngRoute').
216 public function resolveDefaultModules($basePage) {
217 $modules = $this->getModules();
219 foreach ($modules as $moduleName => $module) {
220 if (in_array($basePage, $module['basePages']) ||
in_array('*', $module['basePages'])) {
221 $result[] = $moduleName;
228 * Convert any globs in an Angular module to file names.
230 * @param array $modules
231 * List of Angular modules.
233 * Updated list of Angular modules
235 protected function resolvePatterns($modules) {
238 foreach ($modules as $moduleKey => $module) {
239 foreach (['js', 'css', 'partials'] as $fileset) {
240 if (!isset($module[$fileset])) {
243 $module[$fileset] = $this->res
->glob($module['ext'], $module[$fileset]);
245 $newModules[$moduleKey] = $module;
252 * Get the partial HTML documents for a module (unfiltered).
254 * @param string $name
255 * Angular module name.
257 * Array(string $extFilePath => string $html)
259 * Invalid partials configuration.
261 public function getRawPartials($name) {
262 $module = $this->getModule($name);
263 $result = !empty($module['partialsCallback'])
264 ? \Civi\Core\Resolver
::singleton()->call($module['partialsCallback'], [$name, $module])
266 if (isset($module['partials'])) {
267 foreach ($module['partials'] as $partialDir) {
268 $partialDir = $this->res
->getPath($module['ext']) . '/' . $partialDir;
269 $files = \CRM_Utils_File
::findFiles($partialDir, '*.html', TRUE);
270 foreach ($files as $file) {
271 $filename = '~/' . $name . '/' . $file;
272 $result[$filename] = file_get_contents($partialDir . '/' . $file);
281 * Get the partial HTML documents for a module.
283 * @param string $name
284 * Angular module name.
286 * Array(string $extFilePath => string $html)
288 * Invalid partials configuration.
290 public function getPartials($name) {
291 $cacheKey = "angular-partials_$name";
292 $cacheValue = $this->cache
->get($cacheKey);
293 if ($cacheValue === NULL) {
294 $cacheValue = ChangeSet
::applyResourceFilters($this->getChangeSets(), 'partials', $this->getRawPartials($name));
295 $this->cache
->set($cacheKey, $cacheValue);
301 * Get list of translated strings for a module.
303 * @param string $name
304 * Angular module name.
306 * Translated strings: array(string $orig => string $translated).
308 public function getTranslatedStrings($name) {
309 $module = $this->getModule($name);
311 $strings = $this->getStrings($name);
312 foreach ($strings as $string) {
313 // TODO: should we pass translation domain based on $module[ext] or $module[tsDomain]?
314 // It doesn't look like client side really supports the domain right now...
315 $translated = ts($string, [
316 'domain' => [$module['ext'], NULL],
318 if ($translated != $string) {
319 $result[$string] = $translated;
326 * Get list of translatable strings for a module.
328 * @param string $name
329 * Angular module name.
331 * Translatable strings.
333 public function getStrings($name) {
334 $module = $this->getModule($name);
336 if (isset($module['js'])) {
337 foreach ($module['js'] as $file) {
338 $strings = $this->res
->getStrings()->get(
340 $this->res
->getPath($module['ext'], $file),
343 $result = array_unique(array_merge($result, $strings));
346 $partials = $this->getPartials($name);
347 foreach ($partials as $partial) {
348 $result = array_unique(array_merge($result, \CRM_Utils_JS
::parseStrings($partial)));
354 * Get resources for one or more modules.
356 * @param string|array $moduleNames
357 * List of module names.
358 * @param string $resType
359 * Type of resource ('js', 'css', 'settings').
360 * @param string $refType
361 * Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings').
363 * List of URLs or paths.
364 * @throws \CRM_Core_Exception
366 public function getResources($moduleNames, $resType, $refType) {
368 $moduleNames = (array) $moduleNames;
369 foreach ($moduleNames as $moduleName) {
370 $module = $this->getModule($moduleName);
371 if (isset($module[$resType])) {
372 foreach ($module[$resType] as $file) {
374 if (is_string($file) && preg_match(';^(assetBuilder|ext)://;', $file)) {
375 $refTypeSuffix = '-' . parse_url($file, PHP_URL_SCHEME
);
378 switch ($refType . $refTypeSuffix) {
380 $result[] = $this->res
->getPath($module['ext'], $file);
384 $result[] = $this->res
->getUrl($module['ext'], $file);
388 $result[] = $this->res
->getUrl($module['ext'], $file, TRUE);
391 case 'path-assetBuilder':
392 $assetName = parse_url($file, PHP_URL_HOST
) . parse_url($file, PHP_URL_PATH
);
394 parse_str('' . parse_url($file, PHP_URL_QUERY
), $assetParams);
395 $result[] = \Civi
::service('asset_builder')->getPath($assetName, $assetParams);
398 case 'rawUrl-assetBuilder':
399 case 'cacheUrl-assetBuilder':
400 $assetName = parse_url($file, PHP_URL_HOST
) . parse_url($file, PHP_URL_PATH
);
402 parse_str('' . parse_url($file, PHP_URL_QUERY
), $assetParams);
403 $result[] = \Civi
::service('asset_builder')->getUrl($assetName, $assetParams);
407 $result[] = $this->res
->getPath(parse_url($file, PHP_URL_HOST
), ltrim(parse_url($file, PHP_URL_PATH
), '/'));
411 $result[] = $this->res
->getUrl(parse_url($file, PHP_URL_HOST
), ltrim(parse_url($file, PHP_URL_PATH
), '/'));
415 $result[] = $this->res
->getUrl(parse_url($file, PHP_URL_HOST
), ltrim(parse_url($file, PHP_URL_PATH
), '/'), TRUE);
419 case 'settingsFactory':
423 if (!empty($module[$resType])) {
424 $result[$moduleName] = $module[$resType];
429 throw new \
CRM_Core_Exception("Unrecognized resource format");
435 return ChangeSet
::applyResourceFilters($this->getChangeSets(), $resType, $result);
440 * Array(string $name => ChangeSet $changeSet).
442 public function getChangeSets() {
443 if ($this->changeSets
=== NULL) {
444 $this->changeSets
= [];
445 \CRM_Utils_Hook
::alterAngular($this);
447 return $this->changeSets
;
451 * @param ChangeSet $changeSet
452 * @return \Civi\Angular\Manager
454 public function add($changeSet) {
455 $this->changeSets
[$changeSet->getName()] = $changeSet;