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 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);
 }
Ejemplo n.º 2
0
 public function setUp()
 {
     // Create default HandlerStack
     $stack = HandlerStack::create(function (RequestInterface $request, array $options) {
         return new FulfilledPromise((new Response())->withBody(\GuzzleHttp\Psr7\stream_for('Hello world!'))->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() + 120)));
     });
     // 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]);
 }
 public function setUp()
 {
     // Create default HandlerStack
     $stack = HandlerStack::create(function (RequestInterface $request, array $options) {
         switch ($request->getUri()->getPath()) {
             case '/expired':
                 return new FulfilledPromise((new Response())->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() - 10)));
             case '/2s':
                 return new FulfilledPromise((new Response())->withHeader("Expires", gmdate('D, d M Y H:i:s T', time() + 2)));
             case '/stale-if-error':
                 if ($this->sendError) {
                     return new FulfilledPromise(new Response(500));
                 }
                 return new FulfilledPromise((new Response())->withHeader("Cache-Control", "stale-if-error=120"));
         }
         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]);
 }
 public function setUp()
 {
     // Create default HandlerStack
     $stack = HandlerStack::create(function (RequestInterface $request, array $options) {
         switch ($request->getUri()->getPath()) {
             case '/2s':
                 return new FulfilledPromise((new Response())->withAddedHeader("Cache-Control", "max-age=2"));
             case '/no-store':
                 return new FulfilledPromise((new Response())->withAddedHeader("Cache-Control", "no-store"));
             case '/no-cache':
                 if ($request->getHeaderLine("If-None-Match") == "TheHash") {
                     return new FulfilledPromise(new Response(304));
                 }
                 return new FulfilledPromise((new Response())->withAddedHeader("Cache-Control", "no-cache")->withAddedHeader("Etag", "TheHash"));
         }
         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]);
 }