Exemplo n.º 1
0
 public static function mock(StorageAdapterInterface $storage, $mode, $debug = false)
 {
     return function (callable $handler) use($mode, $storage, $debug) {
         return function (RequestInterface $request, array $options) use($handler, $mode, $storage, $debug) {
             if ('record' === $mode) {
                 return $handler($request, $options)->then(function (ResponseInterface $response) use($request, $storage, $debug) {
                     $storage->save($request, $response);
                     if ($debug) {
                         $response = $response->withHeader('X-Guzzle-Mock', 'RECORD');
                     }
                     return $response;
                 });
             }
             try {
                 $response = $storage->fetch($request);
                 if ($debug) {
                     $response = $response->withHeader('X-Guzzle-Mock', 'REPLAY');
                 }
             } catch (\RuntimeException $e) {
                 return new RejectedPromise($e);
             }
             return new FulfilledPromise($response);
         };
     };
 }
Exemplo n.º 2
0
 public static function mock(StorageAdapterInterface $storage, $mode)
 {
     return function (callable $handler) use($mode, $storage) {
         return function (RequestInterface $request, array $options) use($handler, $mode, $storage) {
             if ('record' === $mode) {
                 return $handler($request, $options)->then(function (ResponseInterface $response) use($request, $storage) {
                     $storage->save($request, $response);
                     return $response;
                 });
             }
             try {
                 $response = $storage->fetch($request);
             } catch (\RuntimeException $e) {
                 return new RejectedPromise($e);
             }
             return new FulfilledPromise($response);
         };
     };
 }
Exemplo n.º 3
0
 public static function cache(StorageAdapterInterface $adapter, $debug = false)
 {
     return function (callable $handler) use($adapter, $debug) {
         return function (RequestInterface $request, array $options) use($handler, $adapter, $debug) {
             if (!($response = $adapter->fetch($request))) {
                 return $handler($request, $options)->then(function (ResponseInterface $response) use($adapter, $request, $debug) {
                     $adapter->save($request, $response);
                     if ($debug) {
                         $response = $response->withHeader('X-Guzzle-Cache', 'MISS');
                     }
                     return $response;
                 });
             }
             if ($debug) {
                 $response = $response->withHeader('X-Guzzle-Cache', 'HIT');
             }
             return new FulfilledPromise($response);
         };
     };
 }