From e9d08c6b837dbe648af2402e63565606420e951f Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 24 Aug 2020 03:10:33 -0700 Subject: [PATCH] CRM_Core_Resources - Use splats, CollectionAdderTrait, CollectionAdderInterface Before ------ `CRM_Core_Resources` directly implements a number of functions (`addFoo()`) After ----- `CRM_Core_Resources` re-uses the implementations from `CollectionAdderTrait` Comment ------- The method signatures in `CollectionAdderTrait` look different, but they are compatible. In particular, the use of splat (`...$options`) means that they can accept either key-value options or positional options. The existing test-cases in `CollectionTestTrait` assert as much. --- CRM/Core/Resources.php | 268 +++----------------- CRM/Core/Resources/CollectionAdderTrait.php | 2 +- CRM/Core/Resources/CollectionTrait.php | 2 +- 3 files changed, 37 insertions(+), 235 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 002adcabf8..5b7765a33a 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -24,10 +24,12 @@ use Civi\Core\Event\GenericHookEvent; * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing */ -class CRM_Core_Resources { +class CRM_Core_Resources implements CRM_Core_Resources_CollectionAdderInterface { const DEFAULT_WEIGHT = 0; const DEFAULT_REGION = 'page-footer'; + use CRM_Core_Resources_CollectionAdderTrait; + /** * We don't have a container or dependency-injection, so use singleton instead * @@ -139,6 +141,38 @@ class CRM_Core_Resources { $this->paths = Civi::paths(); } + /** + * Add an item to the collection. + * + * @param array $snippet + * @return array + * The full/computed snippet (with defaults applied). + * @see CRM_Core_Resources_CollectionInterface::add() + */ + public function add($snippet) { + if (!isset($snippet['region'])) { + $snippet['region'] = self::DEFAULT_REGION; + } + if (!isset($snippet['weight'])) { + $snippet['weight'] = self::DEFAULT_WEIGHT; + } + return CRM_Core_Region::instance($snippet['region'])->add($snippet); + } + + /** + * Locate the 'settings' snippet. + * + * @param array $options + * @return array + * @see CRM_Core_Resources_CollectionTrait::findCreateSettingSnippet() + */ + public function &findCreateSettingSnippet($options = []): array { + $options = CRM_Core_Resources_CollectionAdderTrait::mergeSettingOptions($options, [ + 'region' => NULL, + ]); + return $this->getSettingRegion($options['region'])->findCreateSettingSnippet($options); + } + /** * Assimilate all the resources listed in a bundle. * @@ -187,143 +221,6 @@ class CRM_Core_Resources { return $this; } - /** - * Export permission data to the client to enable smarter GUIs. - * - * Note: Application security stems from the server's enforcement - * of the security logic (e.g. in the API permissions). There's no way - * the client can use this info to make the app more secure; however, - * it can produce a better-tuned (non-broken) UI. - * - * @param string|iterable $permNames - * List of permission names to check/export. - * @return CRM_Core_Resources - */ - public function addPermissions($permNames) { - $this->getSettingRegion()->addPermissions($permNames); - return $this; - } - - /** - * Add a JavaScript file to the current page using