From 29aaf56f80ca0af26d6cdba4148fc9ff02c3658f Mon Sep 17 00:00:00 2001 From: Bryce Gilhome Date: Wed, 27 Jul 2022 10:50:07 -0700 Subject: [PATCH 1/4] Upgrade Craft version to 4. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4d962ee..57e6f40 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ } ], "require": { - "craftcms/cms": "^3.0.0" + "craftcms/cms": "^4.0.0" }, "autoload": { "psr-4": { From 2c4efc03370f8a9ed2e9dd2b63c240e3bea7f876 Mon Sep 17 00:00:00 2001 From: Bryce Gilhome Date: Wed, 27 Jul 2022 11:41:16 -0700 Subject: [PATCH 2/4] Update Twig classes & remove unnecessary use statements. --- src/twigextensions/FetchTwigExtension.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/twigextensions/FetchTwigExtension.php b/src/twigextensions/FetchTwigExtension.php index d47ffc8..d19e196 100644 --- a/src/twigextensions/FetchTwigExtension.php +++ b/src/twigextensions/FetchTwigExtension.php @@ -10,16 +10,15 @@ namespace jalendport\fetch\twigextensions; -use jalendport\fetch\Fetch; - -use Craft; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; /** * @author Jalen Davenport * @package Fetch * @since 1.1.0 */ -class FetchTwigExtension extends \Twig_Extension +class FetchTwigExtension extends AbstractExtension { public function getName() { @@ -29,7 +28,7 @@ public function getName() public function getFunctions() { return [ - new \Twig_SimpleFunction('fetch', [$this, 'fetch']), + new TwigFunction('fetch', [$this, 'fetch']), ]; } From 64aedb17bf641a8bacd58384d213b523e131e913 Mon Sep 17 00:00:00 2001 From: Jalen Davenport Date: Thu, 8 Jun 2023 18:14:56 -0400 Subject: [PATCH 3/4] Perform manual codebase cleanup for Craft 4 --- src/Fetch.php | 92 +++++++++-------------- src/twigextensions/FetchTwigExtension.php | 76 ++++++++++--------- src/variables/FetchVariable.php | 60 ++++++++------- 3 files changed, 106 insertions(+), 122 deletions(-) diff --git a/src/Fetch.php b/src/Fetch.php index f53860e..b4c70a2 100644 --- a/src/Fetch.php +++ b/src/Fetch.php @@ -1,6 +1,6 @@ view->registerTwigExtension(new FetchTwigExtension()); - - Event::on( - CraftVariable::class, - CraftVariable::EVENT_INIT, - function (Event $event) { - /** @var CraftVariable $variable */ - $variable = $event->sender; - $variable->set('fetch', FetchVariable::class); - } - ); - - Event::on( - Plugins::class, - Plugins::EVENT_AFTER_INSTALL_PLUGIN, - function (PluginEvent $event) { - if ($event->plugin === $this) { - } - } - ); - - Craft::info( - Craft::t( - 'fetch', - '{name} plugin loaded', - ['name' => $this->name] - ), - __METHOD__ - ); - } - // Protected Methods - // ========================================================================= + // Public Methods + // ========================================================================= + + /** + * @inheritdoc + */ + public function init(): void + { + parent::init(); + + Craft::$app->view->registerTwigExtension(new FetchTwigExtension()); + + Event::on( + CraftVariable::class, + CraftVariable::EVENT_INIT, + function (Event $event) { + /** @var CraftVariable $variable */ + $variable = $event->sender; + $variable->set('fetch', FetchVariable::class); + } + ); + + Craft::info( + Craft::t( + 'fetch', + '{name} plugin loaded', + ['name' => $this->name] + ), + __METHOD__ + ); + } } diff --git a/src/twigextensions/FetchTwigExtension.php b/src/twigextensions/FetchTwigExtension.php index d19e196..e8fb21b 100644 --- a/src/twigextensions/FetchTwigExtension.php +++ b/src/twigextensions/FetchTwigExtension.php @@ -1,6 +1,6 @@ request($method, $destination, $request); + $response = $client->request($method, $destination, $request); - if ($parseJson) { - $body = json_decode($response->getBody(), true); - } else { - $body = (string)$response->getBody(); - } + if ($parseJson) { + $body = json_decode($response->getBody(), true); + } else { + $body = (string)$response->getBody(); + } - return [ - 'statusCode' => $response->getStatusCode(), - 'reason' => $response->getReasonPhrase(), - 'body' => $body - ]; + return [ + 'statusCode' => $response->getStatusCode(), + 'reason' => $response->getReasonPhrase(), + 'body' => $body + ]; - } catch (\Exception $e) { + } catch (GuzzleException $e) { - return [ - 'error' => true, - 'reason' => $e->getMessage() - ]; + return [ + 'error' => true, + 'reason' => $e->getMessage() + ]; - } - } + } + } } diff --git a/src/variables/FetchVariable.php b/src/variables/FetchVariable.php index e869ad0..f9f5ee6 100644 --- a/src/variables/FetchVariable.php +++ b/src/variables/FetchVariable.php @@ -1,6 +1,6 @@ request($method, $destination, $request); - $response = $client->request($method, $destination, $request); + return [ + 'statusCode' => $response->getStatusCode(), + 'reason' => $response->getReasonPhrase(), + 'body' => json_decode($response->getBody(), true) + ]; - return [ - 'statusCode' => $response->getStatusCode(), - 'reason' => $response->getReasonPhrase(), - 'body' => json_decode($response->getBody(), true) - ]; + } catch (Exception $e) { - } catch (\Exception $e) { + return [ + 'error' => true, + 'reason' => $e->getMessage() + ]; - return [ - 'error' => true, - 'reason' => $e->getMessage() - ]; + } - } + } - } } From bb8b27f937a42e2f137d7a0f53de426ab857c0aa Mon Sep 17 00:00:00 2001 From: Jalen Davenport Date: Thu, 8 Jun 2023 18:22:29 -0400 Subject: [PATCH 4/4] Prepare 2.0.0-beta.1 release --- CHANGELOG.md | 5 +++++ composer.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03ab6fb..643cd62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 2.0.0-beta.1 - 2023-06-08 + +### Added +- Initial Craft 4 release + ## 1.3.0 - 2019-11-16 Transfer of ownership 👀 diff --git a/composer.json b/composer.json index 57e6f40..ae0597d 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "jalendport/craft-fetch", "description": "Utilise the Guzzle HTTP client from within your Craft templates.", "type": "craft-plugin", - "version": "1.3.0", + "version": "2.0.0-beta.1", "keywords": [ "craft", "cms",