From 8767bfb9b0ba9c2c467093e3a604be639f6b795a Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Sun, 9 Jan 2022 15:51:00 -0700 Subject: [PATCH] refactoring and generic link support --- system/src/Grav/Common/Assets.php | 61 ++++++++++++++----- system/src/Grav/Common/Assets/BaseAsset.php | 5 +- .../src/Grav/Common/Assets/InlineJsModule.php | 10 ++- system/src/Grav/Common/Assets/JsModule.php | 15 ++++- system/src/Grav/Common/Assets/Link.php | 43 +++++++++++++ 5 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 system/src/Grav/Common/Assets/Link.php diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index 7bda3ef18..09aa7e71f 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -30,12 +30,15 @@ class Assets extends PropertyObject use TestingAssetsTrait; use LegacyAssetsTrait; + const LINK = 'link'; const CSS = 'css'; const JS = 'js'; const JS_MODULE = 'js_module'; + const LINK_COLLECTION = 'assets_link'; const CSS_COLLECTION = 'assets_css'; const JS_COLLECTION = 'assets_js'; const JS_MODULE_COLLECTION = 'assets_js_module'; + const LINK_TYPE = Assets\Link::class; const CSS_TYPE = Assets\Css::class; const JS_TYPE = Assets\Js::class; const JS_MODULE_TYPE = Assets\JsModule::class; @@ -232,7 +235,7 @@ class Assets extends PropertyObject return $this; } - if (($type === $this::CSS_TYPE || $type === $this::JS_TYPE || $type === $this::JS_MODULE_TYPE) && isset($this->collections[$asset])) { + if ($this->isValidType($type) && isset($this->collections[$asset])) { $this->addType($collection, $type, $this->collections[$asset], $options); return $this; } @@ -241,18 +244,7 @@ class Assets extends PropertyObject if (isset($options['pipeline'])) { if ($options['pipeline'] === false) { - switch ($type) { - case $this::JS_TYPE: - case $this::INLINE_JS_TYPE: - $exclude_type = $this::JS; - break; - case $this::JS_MODULE_TYPE: - case $this::INLINE_JS_MODULE_TYPE: - $exclude_type = $this::JS_MODULE; - break; - default: - $exclude_type = $this::CSS; - } + $exclude_type = $this->getBaseType($type); $excludes = strtolower($exclude_type . '_pipeline_before_excludes'); if ($this->{$excludes}) { @@ -292,6 +284,16 @@ class Assets extends PropertyObject return $this; } + /** + * Add a CSS asset or a collection of assets. + * + * @return $this + */ + public function addLink($asset) + { + return $this->addType($this::LINK_COLLECTION, $this::LINK_TYPE, $asset, $this->unifyLegacyArguments(func_get_args(), $this::LINK_TYPE)); + } + /** * Add a CSS asset or a collection of assets. * @@ -442,7 +444,7 @@ class Assets extends PropertyObject $after_assets = $this->filterAssets($group_assets, 'position', 'after', true); // Pipeline - if ($this->{$pipeline_enabled}) { + if ($this->{$pipeline_enabled} ?? false) { $options = array_merge($this->pipeline_options, ['timestamp' => $this->timestamp]); $pipeline = new Pipeline($options); @@ -476,7 +478,9 @@ class Assets extends PropertyObject */ public function css($group = 'head', $attributes = []) { - return $this->render('css', $group, $attributes); + $output = $this->render(self::LINK, $group, $attributes); + $output .= $this->render(self::CSS, $group, $attributes); + return $output; } /** @@ -488,7 +492,32 @@ class Assets extends PropertyObject */ public function js($group = 'head', $attributes = []) { - return $this->render('js', $group, $attributes) . $this->render('js_module', $group, $attributes); + $output = $this->render(self::JS, $group, $attributes); + $output .= $this->render(self::JS_MODULE, $group, $attributes); + return $output; } + + protected function isValidType($type) + { + return in_array($type, [self::CSS_TYPE, self::JS_TYPE, self::JS_MODULE_TYPE]); + } + + protected function getBaseType($type) + { + switch ($type) { + case $this::JS_TYPE: + case $this::INLINE_JS_TYPE: + $base_type = $this::JS; + break; + case $this::JS_MODULE_TYPE: + case $this::INLINE_JS_MODULE_TYPE: + $base_type = $this::JS_MODULE; + break; + default: + $base_type = $this::CSS; + } + + return $base_type; + } } diff --git a/system/src/Grav/Common/Assets/BaseAsset.php b/system/src/Grav/Common/Assets/BaseAsset.php index 2b107a5ee..4f74f55f9 100644 --- a/system/src/Grav/Common/Assets/BaseAsset.php +++ b/system/src/Grav/Common/Assets/BaseAsset.php @@ -26,8 +26,9 @@ abstract class BaseAsset extends PropertyObject { use AssetUtilsTrait; - protected const CSS_ASSET = true; - protected const JS_ASSET = false; + protected const CSS_ASSET = 1; + protected const JS_ASSET = 2; + protected const JS_MODULE_ASSET = 3; /** @var string|false */ protected $asset; diff --git a/system/src/Grav/Common/Assets/InlineJsModule.php b/system/src/Grav/Common/Assets/InlineJsModule.php index c59a38104..42ce6f14a 100644 --- a/system/src/Grav/Common/Assets/InlineJsModule.php +++ b/system/src/Grav/Common/Assets/InlineJsModule.php @@ -15,7 +15,7 @@ use Grav\Common\Utils; * Class InlineJs * @package Grav\Common\Assets */ -class InlineJsModule extends InlineJs +class InlineJsModule extends BaseAsset { /** * InlineJs constructor. @@ -35,4 +35,12 @@ class InlineJsModule extends InlineJs parent::__construct($merged_attributes, $key); } + /** + * @return string + */ + public function render() + { + return 'renderAttributes(). ">\n" . trim($this->asset) . "\n\n"; + } + } diff --git a/system/src/Grav/Common/Assets/JsModule.php b/system/src/Grav/Common/Assets/JsModule.php index e453a0fcc..5c2a836c2 100644 --- a/system/src/Grav/Common/Assets/JsModule.php +++ b/system/src/Grav/Common/Assets/JsModule.php @@ -15,7 +15,7 @@ use Grav\Common\Utils; * Class Js * @package Grav\Common\Assets */ -class JsModule extends Js +class JsModule extends BaseAsset { /** * Js constructor. @@ -33,4 +33,17 @@ class JsModule extends Js parent::__construct($merged_attributes, $key); } + + /** + * @return string + */ + public function render() + { + if (isset($this->attributes['loading']) && $this->attributes['loading'] === 'inline') { + $buffer = $this->gatherLinks([$this], self::JS_MODULE_ASSET); + return 'renderAttributes() . ">\n" . trim($buffer) . "\n\n"; + } + + return '\n"; + } } diff --git a/system/src/Grav/Common/Assets/Link.php b/system/src/Grav/Common/Assets/Link.php new file mode 100644 index 000000000..ecafcea90 --- /dev/null +++ b/system/src/Grav/Common/Assets/Link.php @@ -0,0 +1,43 @@ + 'link', + ]; + + $merged_attributes = Utils::arrayMergeRecursiveUnique($base_options, $elements); + + parent::__construct($merged_attributes, $key); + } + + /** + * @return string + */ + public function render() + { + return 'renderAttributes() . $this->integrityHash($this->asset) . ">\n"; + } +}