This plugin integrates cache functionality into Guzzle Bundle, a bundle for building RESTful web service clients.
- PHP 7.0 or above
- Guzzle Bundle
- Guzzle Cache middleware
Using composer:
{
"require": {
"gregurco/guzzle-bundle-cache-plugin": "dev-master"
}
}
$ composer require gregurco/guzzle-bundle-cache-plugin
Plugin will be activated/connected through bundle constructor in app/AppKernel.php
, like this:
new EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle([
new Gregurco\Bundle\GuzzleBundleCachePlugin\GuzzleBundleCachePlugin(),
])
The registration of bundles was changed in Symfony 4 and now you have to change src/Kernel.php
to achieve the same functionality.
Find next lines:
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
yield new $class();
}
}
and replace them by:
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
if ($class === \EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle::class) {
yield new $class([
new \Gregurco\Bundle\GuzzleBundleCachePlugin\GuzzleBundleCachePlugin(),
]);
} else {
yield new $class();
}
}
}
# app/config/config.yml
eight_points_guzzle:
clients:
api_payment:
base_url: "http://api.domain.tld"
# define headers, options
# plugin settings
plugin:
cache:
enabled: true
# app/config/services.yml
services:
acme.filesystem_cache:
class: Doctrine\Common\Cache\FilesystemCache
arguments: ['/tmp/']
public: false
acme.doctrine_cache_storage:
class: Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage
arguments: ['@acme.filesystem_cache']
public: false
acme.private_cache_strategy:
class: Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy
arguments: ['@acme.doctrine_cache_storage']
public: false
# app/config/config.yml
eight_points_guzzle:
clients:
api_payment:
plugin:
cache:
enabled: true
strategy: "acme.private_cache_strategy"
More information about cache strategies can be found here: Kevinrob/guzzle-cache-middleware
# get client
$apiPaymentClient = $this->get('eight_points_guzzle.client.api_payment');
# do a request
$apiPaymentClient->request('GET', 'ping');
# invalidate cache
$event = new InvalidateRequestEvent($apiPaymentClient, 'GET', 'ping');
$this->get('event_dispatcher')->dispatch(GuzzleBundleCacheEvents::INVALIDATE, $event);
This middleware is licensed under the MIT License - see the LICENSE file for details