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 4d962ee..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", @@ -27,7 +27,7 @@ } ], "require": { - "craftcms/cms": "^3.0.0" + "craftcms/cms": "^4.0.0" }, "autoload": { "psr-4": { 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 d47ffc8..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() - ]; + } - } + } - } }