Skip to content

Commit

Permalink
AngularManager - Simplify loops
Browse files Browse the repository at this point in the history
Before - unnecessarily loops through array before just setting the entire value of the array
After - skips the unnecessary loop
  • Loading branch information
colemanw committed Oct 16, 2023
1 parent 69d12f1 commit 5f05c2d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
32 changes: 17 additions & 15 deletions Civi/Angular/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,14 @@ public function getStrings($name) {
}

/**
* Get resources for one or more modules.
* Get resources for one or more modules, applying any changesets.
*
* NOTE: The output of this function is a little quirky; depending on the type of resource requested,
* the results will either be a non-associative array (for path and url-type resources)
* or an array indexed by moduleName (for pass-thru resources like settingsFactory, requires, permissions, bundles).
*
* Note: ChangeSets will be applied
* @see \CRM_Utils_Hook::alterAngular()
*
* @param string|array $moduleNames
* List of module names.
Expand All @@ -361,15 +368,20 @@ public function getStrings($name) {
* @param string $refType
* Type of reference to the resource ('cacheUrl', 'rawUrl', 'path', 'settings').
* @return array
* List of URLs or paths.
* Indexed or non-associative array, depending on resource requested (see note)
* @throws \CRM_Core_Exception
*/
public function getResources($moduleNames, $resType, $refType) {
$result = [];
$moduleNames = (array) $moduleNames;
foreach ($moduleNames as $moduleName) {
// Properties that do not require interpolation - they are added to the output keyed by moduleName
$passThru = ['settings', 'settingsFactory', 'requires', 'permissions', 'bundles'];

foreach ((array) $moduleNames as $moduleName) {
$module = $this->getModule($moduleName);
if (isset($module[$resType])) {
if (isset($module[$resType]) && in_array($resType, $passThru, TRUE)) {
$result[$moduleName] = $module[$resType];
}
elseif (isset($module[$resType])) {
foreach ($module[$resType] as $file) {
$refTypeSuffix = '';
if (is_string($file) && preg_match(';^(assetBuilder|ext)://;', $file)) {
Expand Down Expand Up @@ -416,16 +428,6 @@ public function getResources($moduleNames, $resType, $refType) {
$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':
case 'bundles':
if (!empty($module[$resType])) {
$result[$moduleName] = $module[$resType];
}
break;

default:
throw new \CRM_Core_Exception("Unrecognized resource format");
}
Expand Down
21 changes: 11 additions & 10 deletions Civi/Angular/Page/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Civi\Angular\Page;

use Civi\Angular\Manager;

/**
* This page aggregates data from Angular modules.
*
Expand Down Expand Up @@ -148,18 +150,17 @@ public function parseModuleNames($modulesExpr, $angular) {
* @param \Civi\Angular\Manager $angular
* @return array
*/
public function getMetadata($moduleNames, $angular) {
$modules = $angular->getModules();
public function getMetadata(array $moduleNames, Manager $angular): array {
$result = [];
foreach ($moduleNames as $moduleName) {
if (isset($modules[$moduleName])) {
$result[$moduleName] = [];
$result[$moduleName]['domain'] = $modules[$moduleName]['ext'];
$result[$moduleName]['js'] = $angular->getResources($moduleName, 'js', 'rawUrl');
$result[$moduleName]['css'] = $angular->getResources($moduleName, 'css', 'rawUrl');
$result[$moduleName]['partials'] = $angular->getPartials($moduleName);
$result[$moduleName]['strings'] = $angular->getTranslatedStrings($moduleName);
}
$module = $angular->getModule($moduleName);
$result[$moduleName] = [
'domain' => $module['ext'],
'js' => $angular->getResources($moduleName, 'js', 'rawUrl'),
'css' => $angular->getResources($moduleName, 'css', 'rawUrl'),
'partials' => $angular->getPartials($moduleName),
'strings' => $angular->getTranslatedStrings($moduleName),
];
}
return $result;
}
Expand Down

0 comments on commit 5f05c2d

Please sign in to comment.