Skip to content

Commit

Permalink
Merge branch 'release/2.0.0-beta.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
jalendport committed Jun 8, 2023
2 parents bfb7fb7 + bb8b27f commit dd712e2
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 128 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 👀
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -27,7 +27,7 @@
}
],
"require": {
"craftcms/cms": "^3.0.0"
"craftcms/cms": "^4.0.0"
},
"autoload": {
"psr-4": {
Expand Down
92 changes: 34 additions & 58 deletions src/Fetch.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Fetch plugin for Craft CMS 3.x
* Fetch plugin for Craft CMS 4.x
*
* Utilise the Guzzle HTTP client from within your Craft templates.
*
Expand All @@ -10,15 +10,11 @@

namespace jalendport\fetch;

use jalendport\fetch\variables\FetchVariable;
use jalendport\fetch\twigextensions\FetchTwigExtension;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\twig\variables\CraftVariable;

use jalendport\fetch\variables\FetchVariable;
use jalendport\fetch\twigextensions\FetchTwigExtension;
use yii\base\Event;

/**
Expand All @@ -31,57 +27,37 @@
*/
class Fetch extends Plugin
{
// Static Properties
// =========================================================================

/**
* @var Fetch
*/
public static $plugin;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function init()
{
parent::init();
self::$plugin = $this;

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);
}
);

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__
);
}

}
83 changes: 42 additions & 41 deletions src/twigextensions/FetchTwigExtension.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Fetch plugin for Craft CMS 3.x
* Fetch plugin for Craft CMS 4.x
*
* Utilise the Guzzle HTTP client from within your Craft templates.
*
Expand All @@ -10,56 +10,57 @@

namespace jalendport\fetch\twigextensions;

use jalendport\fetch\Fetch;

use Craft;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* @author Jalen Davenport
* @package Fetch
* @since 1.1.0
*/
class FetchTwigExtension extends \Twig_Extension
* @author Jalen Davenport
* @package Fetch
* @since 1.1.0
*/
class FetchTwigExtension extends AbstractExtension
{
public function getName()
{
return 'Fetch';
}
public function getName(): string
{
return 'Fetch';
}

public function getFunctions()
{
return [
new \Twig_SimpleFunction('fetch', [$this, 'fetch']),
];
}
public function getFunctions(): array
{
return [
new TwigFunction('fetch', [$this, 'fetch']),
];
}

public function fetch($client, $method, $destination, $request = [], $parseJson = true)
{
$client = new \GuzzleHttp\Client($client);
public function fetch($client, $method, $destination, $request = [], $parseJson = true): array
{
$client = new Client($client);

try {
try {

$response = $client->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()
];

}
}
}
}
}
60 changes: 33 additions & 27 deletions src/variables/FetchVariable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Fetch plugin for Craft CMS 3.x
* Fetch plugin for Craft CMS 4.x
*
* Utilise the Guzzle HTTP client from within your Craft templates.
*
Expand All @@ -10,9 +10,9 @@

namespace jalendport\fetch\variables;

use jalendport\fetch\Fetch;

use Craft;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

/**
* @author Jalen Davenport
Expand All @@ -21,36 +21,42 @@
*/
class FetchVariable
{
// Public Methods
// =========================================================================

/**
* @param null $optional
* @return string
*/
public function request($client, $method, $destination, $request = [])
{
// Public Methods
// =========================================================================

/**
* @param $client
* @param $method
* @param $destination
* @param array $request
* @return array|string
* @throws GuzzleException
*/
public function request($client, $method, $destination, array $request = []): array|string
{

$client = new Client($client);

$client = new \GuzzleHttp\Client($client);
try {

try {
$response = $client->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()
];
}

}
}

}
}

0 comments on commit dd712e2

Please sign in to comment.