From c4b76165c92ff5d30009ae7ab221a382b978ad71 Mon Sep 17 00:00:00 2001 From: Tam Date: Thu, 20 Apr 2023 15:46:38 +0100 Subject: [PATCH] v4 bby --- composer.json | 3 +-- src/UtilityBelt.php | 18 +++++++++--------- src/fields/LinkField.php | 12 +++++------- src/jobs/RegenerateLinkCacheJob.php | 6 +++--- src/jobs/RevalidateJob.php | 9 ++++----- src/migrations/Install.php | 4 ++-- ...220406_093529_create_link_element_table.php | 2 +- ..._102710_add_urlSuffix_column_to_content.php | 2 +- src/services/LivePreview.php | 6 +++--- src/services/Revalidator.php | 18 +++++++++--------- src/web/assets/link/LinkFieldAsset.php | 4 ++-- src/widgets/TwigWidget.php | 6 +++--- 12 files changed, 43 insertions(+), 47 deletions(-) diff --git a/composer.json b/composer.json index c3f36e8..e1b1b26 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,11 @@ { "name": "ether/utility-belt", "description": "A collection of things we use on every Craft CMS site", - "version": "0.0.6", "type": "craft-plugin", "license": "GPL-3.0-or-later", "minimum-stability": "dev", "require": { - "craftcms/cms": "^3.7", + "craftcms/cms": "^4", "ether/logs": "*" }, "autoload": { diff --git a/src/UtilityBelt.php b/src/UtilityBelt.php index 4c49b4e..6df373b 100644 --- a/src/UtilityBelt.php +++ b/src/UtilityBelt.php @@ -34,7 +34,7 @@ class UtilityBelt extends Plugin { - public function init () + public function init (): void { $this->setComponents([ 'livePreview' => LivePreview::class, @@ -95,21 +95,21 @@ public function init () // Events // ========================================================================= - public function onAfterUninstallPlugin (PluginEvent $event) + public function onAfterUninstallPlugin (PluginEvent $event): void { if ($event->plugin->getHandle() !== $this->getHandle()) return; Craft::$app->getPlugins()->uninstallPlugin('logs'); } - public function onAfterInstallPlugin (PluginEvent $event) + public function onAfterInstallPlugin (PluginEvent $event): void { if ($event->plugin->getHandle() !== $this->getHandle()) return; Craft::$app->getPlugins()->installPlugin('logs'); } - public function onAfterExecuteGqlQuery (ExecuteGqlQueryEvent $event) + public function onAfterExecuteGqlQuery (ExecuteGqlQueryEvent $event): void { // Make absolute internal URLs relative $res = Json::encode($event->result); @@ -121,7 +121,7 @@ public function onAfterExecuteGqlQuery (ExecuteGqlQueryEvent $event) $event->result = Json::decode($res); } - public function onDefineGqlTypeFields (DefineGqlTypeFieldsEvent $event) + public function onDefineGqlTypeFields (DefineGqlTypeFieldsEvent $event): void { if ($event->typeName === 'AssetInterface') { @@ -141,17 +141,17 @@ public function onDefineGqlTypeFields (DefineGqlTypeFieldsEvent $event) } } - public function onRegisterWidgetTypes (RegisterComponentTypesEvent $event) + public function onRegisterWidgetTypes (RegisterComponentTypesEvent $event): void { $event->types[] = TwigWidget::class; } - public function onRegisterFieldTypes (RegisterComponentTypesEvent $event) + public function onRegisterFieldTypes (RegisterComponentTypesEvent $event): void { $event->types[] = LinkField::class; } - public function onAfterElementSave (ModelEvent $event) + public function onAfterElementSave (ModelEvent $event): void { /** @var Element $element */ $element = $event->sender; @@ -164,4 +164,4 @@ public function onAfterElementSave (ModelEvent $event) ])); } -} \ No newline at end of file +} diff --git a/src/fields/LinkField.php b/src/fields/LinkField.php index 3792697..b5f09a2 100644 --- a/src/fields/LinkField.php +++ b/src/fields/LinkField.php @@ -205,7 +205,7 @@ public function beforeElementSave (ElementInterface $element, bool $isNew): bool return parent::beforeElementSave($element, $isNew); } - public function afterElementSave (ElementInterface $element, bool $isNew) + public function afterElementSave (ElementInterface $element, bool $isNew): void { parent::afterElementSave($element, $isNew); @@ -236,12 +236,10 @@ public function afterElementSave (ElementInterface $element, bool $isNew) ->execute(); } - public function beforeApplyDelete (): bool + public function beforeApplyDelete (): void { parent::beforeApplyDelete(); $this->_dropDbMeta(); - - return true; } public function afterSave (bool $isNew): void @@ -260,7 +258,7 @@ public function afterSave (bool $isNew): void // Helpers // ========================================================================= - public function precacheForElement (ElementInterface $source, ElementInterface $target) + public function precacheForElement (ElementInterface $source, ElementInterface $target): void { $elements = Craft::$app->getElements(); @@ -396,7 +394,7 @@ private function _getContentTable (): ?array return null; } - private function _dropDbMeta () + private function _dropDbMeta (): void { $tbl = $this->_getContentTable(); if (empty($tbl)) return; @@ -418,7 +416,7 @@ private function _dropDbMeta () ->execute(); } - private function _addDbMeta () + private function _addDbMeta (): void { $tbl = $this->_getContentTable(); if (empty($tbl)) return; diff --git a/src/jobs/RegenerateLinkCacheJob.php b/src/jobs/RegenerateLinkCacheJob.php index abfcf34..8f26b05 100644 --- a/src/jobs/RegenerateLinkCacheJob.php +++ b/src/jobs/RegenerateLinkCacheJob.php @@ -13,9 +13,9 @@ class RegenerateLinkCacheJob extends BaseJob public string $elementType; public int $targetId; - public $description = 'Regenerating Link Cache'; + public ?string $description = 'Regenerating Link Cache'; - public function execute ($queue) + public function execute ($queue): void { $target = $this->elementType::findOne(['id' => $this->targetId]); $fieldsService = Craft::$app->getFields(); @@ -65,4 +65,4 @@ public function execute ($queue) } } -} \ No newline at end of file +} diff --git a/src/jobs/RevalidateJob.php b/src/jobs/RevalidateJob.php index 7095fba..eafc2d0 100644 --- a/src/jobs/RevalidateJob.php +++ b/src/jobs/RevalidateJob.php @@ -17,11 +17,12 @@ protected function defaultDescription (): string return 'Revalidating Front-end'; } - public function execute ($queue): bool + public function execute ($queue): void { // Skip if localhost -// if (str_contains(getenv('FRONTEND_URL'), 'local')) -// return true; + $frontendUrl = getenv('FRONTEND_URL'); + if (empty($frontendUrl) || str_contains($frontendUrl, 'local')) + return; $this->uris = array_unique($this->uris); @@ -62,8 +63,6 @@ public function execute ($queue): bool } } } - - return true; } } diff --git a/src/migrations/Install.php b/src/migrations/Install.php index dcd7013..649e371 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -10,7 +10,7 @@ class Install extends Migration { - public function safeUp () + public function safeUp (): void { // Revalidator // --------------------------------------------------------------------- @@ -49,4 +49,4 @@ public function safeDown (): bool return true; } -} \ No newline at end of file +} diff --git a/src/migrations/m220406_093529_create_link_element_table.php b/src/migrations/m220406_093529_create_link_element_table.php index bc81999..41074f7 100644 --- a/src/migrations/m220406_093529_create_link_element_table.php +++ b/src/migrations/m220406_093529_create_link_element_table.php @@ -15,7 +15,7 @@ class m220406_093529_create_link_element_table extends Migration /** * @inheritdoc */ - public function safeUp() + public function safeUp(): void { $this->createTable(LinkField::TABLE, [ 'fieldId' => $this->integer(11), diff --git a/src/migrations/m220407_102710_add_urlSuffix_column_to_content.php b/src/migrations/m220407_102710_add_urlSuffix_column_to_content.php index e17721e..8980ac4 100644 --- a/src/migrations/m220407_102710_add_urlSuffix_column_to_content.php +++ b/src/migrations/m220407_102710_add_urlSuffix_column_to_content.php @@ -19,7 +19,7 @@ class m220407_102710_add_urlSuffix_column_to_content extends Migration /** * @inheritdoc */ - public function safeUp() + public function safeUp(): void { $fieldsService = Craft::$app->getFields(); $fieldIds = (new Query()) diff --git a/src/services/LivePreview.php b/src/services/LivePreview.php index 927b8a5..77fd8e2 100644 --- a/src/services/LivePreview.php +++ b/src/services/LivePreview.php @@ -12,7 +12,7 @@ class LivePreview extends Component { - public function init () + public function init (): void { parent::init(); @@ -50,7 +50,7 @@ public function init () } } - public function onSectionInit (Event $event) + public function onSectionInit (Event $event): void { /** @var Section $section */ $section = $event->sender; @@ -64,4 +64,4 @@ public function onSectionInit (Event $event) ]; } -} \ No newline at end of file +} diff --git a/src/services/Revalidator.php b/src/services/Revalidator.php index 51d74b6..5f04e0d 100644 --- a/src/services/Revalidator.php +++ b/src/services/Revalidator.php @@ -31,7 +31,7 @@ class Revalidator extends Component public static $tableName = '{{%b_revalidate_jobs}}'; - public function init () + public function init (): void { parent::init(); @@ -54,7 +54,7 @@ public function init () ); } - public function onAfterElementSave (ModelEvent $event) + public function onAfterElementSave (ModelEvent $event): void { /** @var Element $element */ $element = $event->sender; @@ -65,12 +65,12 @@ public function onAfterElementSave (ModelEvent $event) $this->push($element); } - public function onAfterRenderTemplate (TemplateEvent $event) + public function onAfterRenderTemplate (TemplateEvent $event): void { $this->injectAdditionalUrisTable($event); } - public function onAfterSectionSave (SectionEvent $event) + public function onAfterSectionSave (SectionEvent $event): void { if (!empty($event->section)) $this->saveAdditionalURIs($event->section->uid); @@ -84,7 +84,7 @@ public function onAfterSectionSave (SectionEvent $event) * @return void * @throws SiteNotFoundException|TemplateLoaderException */ - public function injectAdditionalUrisTable (TemplateEvent $event) + public function injectAdditionalUrisTable (TemplateEvent $event): void { if ($event->template !== 'settings/sections/_edit') return; @@ -157,7 +157,7 @@ public function getAdditionalURIs (string $sectionUid, bool $asRows = false): ar * @throws InvalidConfigException * @throws NotSupportedException */ - public function saveAdditionalURIs (string $sectionUid) + public function saveAdditionalURIs (string $sectionUid): void { $request = Craft::$app->getRequest(); @@ -188,7 +188,7 @@ public function saveAdditionalURIs (string $sectionUid) * @return void * @throws Exception|\yii\base\Exception */ - public function push (Element $element) + public function push (Element $element): void { /** @var RevalidateJob $job */ [$id, $job] = $this->getJob(); @@ -248,7 +248,7 @@ private function getJob (): ?array * @return void * @throws Exception */ - private function storeJobId (int $id) + private function storeJobId (int $id): void { Craft::$app->getDb()->createCommand() ->insert(self::$tableName, ['jobId' => $id], false) @@ -264,7 +264,7 @@ private function storeJobId (int $id) * @return void * @throws Exception */ - private function updateJob (int $id, RevalidateJob $job) + private function updateJob (int $id, RevalidateJob $job): void { $job = Craft::$app->getQueue()->serializer->serialize($job); diff --git a/src/web/assets/link/LinkFieldAsset.php b/src/web/assets/link/LinkFieldAsset.php index 7f9efdf..a9a41f1 100644 --- a/src/web/assets/link/LinkFieldAsset.php +++ b/src/web/assets/link/LinkFieldAsset.php @@ -7,7 +7,7 @@ class LinkFieldAsset extends AssetBundle { - public function init () + public function init (): void { $this->sourcePath = __DIR__; @@ -17,4 +17,4 @@ public function init () parent::init(); } -} \ No newline at end of file +} diff --git a/src/widgets/TwigWidget.php b/src/widgets/TwigWidget.php index d1a8a59..6ef998d 100644 --- a/src/widgets/TwigWidget.php +++ b/src/widgets/TwigWidget.php @@ -17,7 +17,7 @@ public static function displayName (): string return 'Twig'; } - public function getSettingsHtml () + public function getSettingsHtml (): ?string { return Craft::$app->getView()->renderTemplate( 'utility-belt/widgets/twig-settings', @@ -30,7 +30,7 @@ public function getTitle (): string return $this->title ?? parent::getTitle(); } - public function getBodyHtml () + public function getBodyHtml (): ?string { if (empty($this->template)) return ''; @@ -44,4 +44,4 @@ public function getBodyHtml () return $markup; } -} \ No newline at end of file +}