本文整理汇总了PHP中GuzzleHttp\Event\BeforeEvent::intercept方法的典型用法代码示例。如果您正苦于以下问题:PHP BeforeEvent::intercept方法的具体用法?PHP BeforeEvent::intercept怎么用?PHP BeforeEvent::intercept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Event\BeforeEvent
的用法示例。
在下文中一共展示了BeforeEvent::intercept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onBefore
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
if (file_exists($this->getFullFilePath($request))) {
$responsedata = file_get_contents($this->getFullFilePath($request));
$mf = new MessageFactory();
$event->intercept($mf->fromMessage($responsedata));
}
}
示例2: onBefore
/**
* @throws \OutOfBoundsException|\Exception
*/
public function onBefore(BeforeEvent $event)
{
if (!($item = array_shift($this->queue))) {
throw new \OutOfBoundsException('Mock queue is empty');
} elseif ($item instanceof RequestException) {
throw $item;
}
// Emulate reading a response body
$request = $event->getRequest();
if ($this->readBodies && $request->getBody()) {
while (!$request->getBody()->eof()) {
$request->getBody()->read(8096);
}
}
$saveTo = $event->getRequest()->getConfig()->get('save_to');
if (null !== $saveTo) {
$body = $item->getBody();
if (is_resource($saveTo)) {
fwrite($saveTo, $body);
} elseif (is_string($saveTo)) {
file_put_contents($saveTo, $body);
} elseif ($saveTo instanceof StreamInterface) {
$saveTo->write($body);
}
}
$event->intercept($item);
}
示例3: onBefore
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
if (!($response = $this->cache->fetch($request->__toString()))) {
return;
}
$event->intercept($response);
}
示例4: onBefore
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
if (isset(self::$purgeMethods[$request->getMethod()])) {
$this->storage->purge($request->getUrl());
if ('PURGE' === $request->getMethod()) {
$event->intercept(new Response(204));
}
}
}
示例5: testInterceptsWithEvent
public function testInterceptsWithEvent()
{
$t = new Transaction(new Client(), new Request('GET', '/'));
$t->exception = new \Exception('foo');
$e = new BeforeEvent($t);
$response = new Response(200);
$e->intercept($response);
$this->assertTrue($e->isPropagationStopped());
$this->assertSame($t->response, $response);
$this->assertNull($t->exception);
}
示例6: testInterceptsWithEvent
public function testInterceptsWithEvent()
{
$response = new Response(200);
$res = null;
$t = new Transaction(new Client(), new Request('GET', '/'));
$t->getRequest()->getEmitter()->on('complete', function ($e) use(&$res) {
$res = $e;
});
$e = new BeforeEvent($t);
$e->intercept($response);
$this->assertTrue($e->isPropagationStopped());
$this->assertSame($res->getClient(), $e->getClient());
}
示例7: onBefore
/**
* @throws \OutOfBoundsException|\Exception
*/
public function onBefore(BeforeEvent $event)
{
if (!($item = array_shift($this->queue))) {
throw new \OutOfBoundsException('Mock queue is empty');
} elseif ($item instanceof RequestException) {
throw $item;
}
// Emulate reading a response body
$request = $event->getRequest();
if ($this->readBodies && $request->getBody()) {
while (!$request->getBody()->eof()) {
$request->getBody()->read(8096);
}
}
$event->intercept($item);
}
示例8: cacheCheck
public function cacheCheck(BeforeEvent $event, $name)
{
$request = $event->getRequest();
if ($request->getMethod() == 'GET') {
$url = substr($request->getUrl(), 0, strpos($request->getUrl(), '?'));
if (!preg_match('/token|oauth/', $url)) {
$this->_cache_name = md5($request->getUrl() . (string) $request->getBody());
if ($cache = $this->store->get($this->_cache_name, NULL)) {
\Log::info('getting response from cache');
$body = \GuzzleHttp\Stream\create(json_encode($cache));
$response = new \GuzzleHttp\Message\Response(200, array(), $body);
$event->intercept($response);
$event->stopPropagation();
}
}
}
}
示例9: cacheCheck
public function cacheCheck(BeforeEvent $event, $name)
{
if (!$this->cache) {
return;
}
$request = $event->getRequest();
if ($request->getMethod() == 'GET') {
$this->_cache_name = md5($request->getUrl() . (string) $request->getBody());
if ($cache = $this->store->get($this->_cache_name, NULL)) {
\Log::info('getting response from cache');
$body = \GuzzleHttp\Stream\create($cache);
$response = new \GuzzleHttp\Message\Response(200, array(), $body);
$event->intercept($response);
$event->stopPropagation();
}
}
}
示例10: onBefore
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
if (!$this->enabled) {
$request->getConfig()->set('cache.disable', true);
}
if (!$this->canCacheRequest($request)) {
$this->cacheMiss($request);
return;
}
if (!($response = $this->storage->fetch($request))) {
$this->cacheMiss($request);
return;
}
$request->getConfig()->set('cache_lookup', 'HIT');
$request->getConfig()->set('cache_hit', true);
$event->intercept($response);
}
示例11: onBefore
/**
* @throws \OutOfBoundsException|\Exception
*/
public function onBefore(BeforeEvent $event)
{
if (!($item = array_shift($this->queue))) {
throw new \OutOfBoundsException('Mock queue is empty');
} elseif ($item instanceof RequestException) {
throw $item;
}
// Emulate the receiving of the response headers
$request = $event->getRequest();
$transaction = new Transaction($event->getClient(), $request);
$transaction->setResponse($item);
$request->getEmitter()->emit('headers', new HeadersEvent($transaction));
// Emulate reading a response body
if ($this->readBodies && $request->getBody()) {
while (!$request->getBody()->eof()) {
$request->getBody()->read(8096);
}
}
$event->intercept($item);
}
示例12: fail
protected function fail(\Exception $error, BeforeEvent $event)
{
$this->exceptions[] = $error;
// Set a stub response.
// The exception will actually be thrown in
// `verify()`
// If we threw the exception here,
// it would be caught by Guzzle,
// and wrapped into a RequestException
$event->intercept(new Response(200));
}
示例13: onBefore
/**
* Checks if a request can be cached, and if so, intercepts with a cached
* response is available.
*
* @param BeforeEvent $event
*/
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
if (!$this->canCacheRequest($request)) {
$this->cacheMiss($request);
return;
}
if (!($response = $this->storage->fetch($request))) {
$this->cacheMiss($request);
return;
}
$response->setHeader('Age', Utils::getResponseAge($response));
$valid = $this->validate($request, $response);
// Validate that the response satisfies the request
if ($valid) {
$request->getConfig()->set('cache_lookup', 'HIT');
$request->getConfig()->set('cache_hit', true);
$event->intercept($response);
} else {
$this->cacheMiss($request);
}
}
示例14: interceptResponse
public function interceptResponse(BeforeEvent $event)
{
$response = MockResponse::getResponse($event->getRequest());
$event->intercept($response);
$event->stopPropagation();
}
示例15: onBefore
/**
* Check if the request is cached and intercept it.
*
* @param BeforeEvent $event Guzzle 4/5 event.
*
* @return void
*/
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();
$this->request = $request;
if (!Utils::canCacheRequest($request)) {
return;
}
$key = $request->getMethod() . ' ' . $request->getUrl();
if (!$this->cache->contains($key)) {
return;
}
$response = $this->cache->fetch($key);
$event->intercept($response);
}