From 060617e9df668cd7d422b6b32276364cd3368310 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 17 Aug 2020 20:45:29 -0700 Subject: [PATCH] CollectionTrait - Use "splats". Split out "adders". Define interfaces. In the prior commits, the signatures for `addScriptFile()`, `addScriptUrl()`, etc are not strictly interoperable between `CRM_Core_Resources` and `CollectionTrait`. This is because they use key-value options instead of positional options. This makes it easier disregard positional options that don't make sense (e.g. when calling `CRM_Core_Region::addScriptFile()`, it's silly to reserve a positional argument for the `$region` option). The signatures *can* be unified by using "splats" (ie `...$options`) to accept either key-value options or backward-compatible positional options. The ultimate is hope is that: * `CRM_Core_Resources`, `CRM_Core_Region`, and `CRM_Core_Bundle` all implement the `CollectionAdderInterface`. * `CRM_Core_Resources`, `CRM_Core_Region`, and `CRM_Core_Bundle` all accept options in either format (key-value or positional). * The positional format will fade-away. The methods in CollectionTrait are newer terrain, so it's safer to change those signatures, so we do that first. Note that CollectionTrait formally builds on CollectionAdderTrait. This ensures that IDE navigation to (eg) `CRM_Core_Region::add()` and `CRM_Core_Resources_Bundle::add()` works as expected. --- CRM/Core/Region.php | 6 +- .../Resources/CollectionAdderInterface.php | 208 ++++++++++ CRM/Core/Resources/CollectionAdderTrait.php | 382 ++++++++++++++++++ CRM/Core/Resources/CollectionInterface.php | 133 ++++++ CRM/Core/Resources/CollectionTrait.php | 249 +----------- 5 files changed, 729 insertions(+), 249 deletions(-) create mode 100644 CRM/Core/Resources/CollectionAdderInterface.php create mode 100644 CRM/Core/Resources/CollectionAdderTrait.php create mode 100644 CRM/Core/Resources/CollectionInterface.php diff --git a/CRM/Core/Region.php b/CRM/Core/Region.php index 7a31455005..c7eab5c584 100644 --- a/CRM/Core/Region.php +++ b/CRM/Core/Region.php @@ -3,7 +3,7 @@ /** * Maintain a set of markup/templates to inject inside various regions */ -class CRM_Core_Region { +class CRM_Core_Region implements CRM_Core_Resources_CollectionInterface, CRM_Core_Resources_CollectionAdderInterface { /** * Obtain the content for a given region. @@ -20,9 +20,7 @@ class CRM_Core_Region { return Civi::$statics[__CLASS__][$name]; } - use CRM_Core_Resources_CollectionTrait { - CRM_Core_Resources_CollectionTrait::add as _add; - } + use CRM_Core_Resources_CollectionTrait; /** * Symbolic name of this region diff --git a/CRM/Core/Resources/CollectionAdderInterface.php b/CRM/Core/Resources/CollectionAdderInterface.php new file mode 100644 index 0000000000..5f435ce1b8 --- /dev/null +++ b/CRM/Core/Resources/CollectionAdderInterface.php @@ -0,0 +1,208 @@ +. + * + * @param string $code + * JavaScript source code. + * @param array $options + * Open-ended list of options (per add()) + * Ex: ['weight' => 123] + * @return static + */ + public function addScript(string $code, ...$options); + + /** + * Add a JavaScript file to the current page using