Kevinrob\GuzzleCache\CacheMiddleware::getMiddleware PHP Method

getMiddleware() public static method

Deprecation: Use constructor => `new CacheMiddleware()`
public static getMiddleware ( Kevinrob\GuzzleCache\Strategy\CacheStrategyInterface $cacheStorage = null ) : CacheMiddleware
$cacheStorage Kevinrob\GuzzleCache\Strategy\CacheStrategyInterface
return CacheMiddleware the Middleware for Guzzle HandlerStack
    public static function getMiddleware(CacheStrategyInterface $cacheStorage = null)
    {
        return new self($cacheStorage);
    }

Usage Example

 public function setUp()
 {
     // Create default HandlerStack
     $stack = HandlerStack::create(function (RequestInterface $request, array $options) {
         switch ($request->getUri()->getPath()) {
             case '/etag':
                 if ($request->getHeaderLine("If-None-Match") == 'MyBeautifulHash') {
                     return new FulfilledPromise(new Response(304));
                 }
                 return new FulfilledPromise((new Response())->withHeader("Etag", 'MyBeautifulHash'));
             case '/etag-changed':
                 if ($request->getHeaderLine("If-None-Match") == 'MyBeautifulHash') {
                     return new FulfilledPromise((new Response())->withHeader("Etag", 'MyBeautifulHash2'));
                 }
                 return new FulfilledPromise((new Response())->withHeader("Etag", 'MyBeautifulHash'));
             case '/stale-while-revalidate':
                 if ($request->getHeaderLine("If-None-Match") == 'MyBeautifulHash') {
                     return new FulfilledPromise((new Response(304))->withHeader("Cache-Control", 'max-age=10'));
                 }
                 return new FulfilledPromise((new Response())->withHeader("Etag", 'MyBeautifulHash')->withHeader("Cache-Control", 'max-age=1')->withAddedHeader("Cache-Control", 'stale-while-revalidate=60'));
         }
         throw new \InvalidArgumentException();
     });
     // Add this middleware to the top with `push`
     $stack->push(CacheMiddleware::getMiddleware(), 'cache');
     // Initialize the client with the handler option
     $this->client = new Client(['handler' => $stack]);
     CacheMiddleware::setClient($this->client);
 }
All Usage Examples Of Kevinrob\GuzzleCache\CacheMiddleware::getMiddleware