From 5f05c2d0274d4117396540eb926f906f1ffc393a Mon Sep 17 00:00:00 2001 From: colemanw Date: Fri, 13 Oct 2023 09:33:43 -0400 Subject: [PATCH] AngularManager - Simplify loops Before - unnecessarily loops through array before just setting the entire value of the array After - skips the unnecessary loop --- Civi/Angular/Manager.php | 32 +++++++++++++++++--------------- Civi/Angular/Page/Modules.php | 21 +++++++++++---------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/Civi/Angular/Manager.php b/Civi/Angular/Manager.php index 9daf7ca622..4a5190f26a 100644 --- a/Civi/Angular/Manager.php +++ b/Civi/Angular/Manager.php @@ -352,7 +352,14 @@ class Manager { } /** - * 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. @@ -361,15 +368,20 @@ class Manager { * @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)) { @@ -416,16 +428,6 @@ class Manager { $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"); } diff --git a/Civi/Angular/Page/Modules.php b/Civi/Angular/Page/Modules.php index 39a2e1ccfa..b26c2d54f6 100644 --- a/Civi/Angular/Page/Modules.php +++ b/Civi/Angular/Page/Modules.php @@ -2,6 +2,8 @@ namespace Civi\Angular\Page; +use Civi\Angular\Manager; + /** * This page aggregates data from Angular modules. * @@ -148,18 +150,17 @@ class Modules extends \CRM_Core_Page { * @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; } -- 2.25.1