本文整理汇总了PHP中Guzzle\Http\Message\RequestInterface::getEventDispatcher方法的典型用法代码示例。如果您正苦于以下问题:PHP RequestInterface::getEventDispatcher方法的具体用法?PHP RequestInterface::getEventDispatcher怎么用?PHP RequestInterface::getEventDispatcher使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\RequestInterface
的用法示例。
在下文中一共展示了RequestInterface::getEventDispatcher方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: updateForHandle
protected function updateForHandle(RequestInterface $request)
{
$that = $this;
$request->getEventDispatcher()->addListener('request.sent', function (Event $e) use($that) {
$that->requestHandle = $e['handle'];
});
return $request;
}
示例2: dequeue
/**
* Get a response from the front of the list and add it to a request
*
* @param RequestInterface $request Request to mock
*
* @return self
* @throws CurlException When request.send is called and an exception is queued
*/
public function dequeue(RequestInterface $request)
{
$this->dispatch('mock.request', array('plugin' => $this, 'request' => $request));
$item = array_shift($this->queue);
if ($item instanceof Response) {
if ($this->readBodies && $request instanceof EntityEnclosingRequestInterface) {
$request->getEventDispatcher()->addListener('request.sent', $f = function (Event $event) use(&$f) {
while ($data = $event['request']->getBody()->read(8096)) {
}
// Remove the listener after one-time use
$event['request']->getEventDispatcher()->removeListener('request.sent', $f);
});
}
$request->setResponse($item);
} elseif ($item instanceof CurlException) {
// Emulates exceptions encountered while transferring requests
$item->setRequest($request);
$request->setState(RequestInterface::STATE_ERROR);
$request->dispatch('request.exception', array('request' => $request, 'exception' => $item));
// Only throw if the exception wasn't handled
if ($request->getState() == RequestInterface::STATE_ERROR) {
throw $item;
}
}
return $this;
}
示例3: proceed
/**
* Get a response from the front of the list and add it to a request.
*
* @param RequestInterface $request Request to mock
*
* @return self
*
* @throws CurlException When request.send is called and an exception is queued
*/
public function proceed(RequestInterface $request)
{
$this->dispatch('mock.request', ['plugin' => $this, 'request' => $request]);
$response = $this->dequeue($request->getUrl());
if (!$response) {
throw new \OutOfBoundsException('Mock queue for given url is empty');
}
if ($response instanceof Response) {
if ($this->readBodies && $request instanceof EntityEnclosingRequestInterface) {
$request->getEventDispatcher()->addListener('request.sent', $f = function (Event $event) use(&$f) {
// @codingStandardsIgnoreStart
while ($data = $event['request']->getBody()->read(8096)) {
}
// @codingStandardsIgnoreEnd
// Remove the listener after one-time use
$event['request']->getEventDispatcher()->removeListener('request.sent', $f);
});
}
$request->setResponse($response);
} elseif ($response instanceof CurlException) {
// Emulates exceptions encountered while transferring requests
$response->setRequest($request);
$state = $request->setState(RequestInterface::STATE_ERROR, ['exception' => $response]);
// Only throw if the exception wasn't handled
if ($state == RequestInterface::STATE_ERROR) {
throw $response;
}
}
return $this;
}
示例4: throwTooManyRedirectsException
/**
* Throw a too many redirects exception for a request
*
* @param RequestInterface $original Request
* @param int $max Max allowed redirects
*
* @throws TooManyRedirectsException when too many redirects have been issued
*/
protected function throwTooManyRedirectsException(RequestInterface $original, $max)
{
$original->getEventDispatcher()->addListener('request.complete', $func = function ($e) use(&$func, $original, $max) {
$original->getEventDispatcher()->removeListener('request.complete', $func);
$str = "{$max} redirects were issued for this request:\n" . $e['request']->getRawHeaders();
throw new TooManyRedirectsException($str);
});
}
示例5: visit_exceptions
protected function visit_exceptions(RequestInterface $request, $value, $flags)
{
if ($value === false || $value === 0) {
$dispatcher = $request->getEventDispatcher();
foreach ($dispatcher->getListeners('request.error') as $listener) {
if (is_array($listener) && $listener[0] == 'Guzzle\Http\Message\Request' && $listener[1] = 'onRequestError') {
$dispatcher->removeListener('request.error', $listener);
break;
}
}
}
}
示例6: cloneRequestWithMethod
/**
* Clone a request while changing the method. Emulates the behavior of
* {@see Guzzle\Http\Message\Request::clone}, but can change the HTTP method.
*
* @param RequestInterface $request Request to clone
* @param string $method Method to set
*
* @return RequestInterface
*/
public function cloneRequestWithMethod(RequestInterface $request, $method)
{
// Create the request with the same client if possible
if ($client = $request->getClient()) {
$cloned = $request->getClient()->createRequest($method, $request->getUrl(), $request->getHeaders());
} else {
$cloned = $this->create($method, $request->getUrl(), $request->getHeaders());
}
$cloned->getCurlOptions()->replace($request->getCurlOptions()->getAll());
$cloned->setEventDispatcher(clone $request->getEventDispatcher());
// Ensure that that the Content-Length header is not copied if changing to GET or HEAD
if (!$cloned instanceof EntityEnclosingRequestInterface) {
$cloned->removeHeader('Content-Length');
} elseif ($request instanceof EntityEnclosingRequestInterface) {
$cloned->setBody($request->getBody());
}
$cloned->getParams()->replace($request->getParams()->getAll())->remove('curl_handle')->remove('curl_multi');
return $cloned;
}
示例7: dequeue
/**
* Get a response from the front of the list and add it to a request
*
* @param RequestInterface $request Request to mock
*
* @return MockPlugin
*/
public function dequeue(RequestInterface $request)
{
$this->dispatch('mock.request', array('plugin' => $this, 'request' => $request));
$request->setResponse(array_shift($this->queue), true);
if ($this->readBodies && $request instanceof EntityEnclosingRequestInterface) {
$request->getEventDispatcher()->addListener('request.sent', function (Event $event) {
while ($data = $event['request']->getBody()->read(8096)) {
}
});
}
return $this;
}
示例8: dequeue
public function dequeue(RequestInterface $request)
{
$this->dispatch('mock.request', array('plugin' => $this, 'request' => $request));
$item = array_shift($this->queue);
if ($item instanceof Response) {
if ($this->readBodies && $request instanceof EntityEnclosingRequestInterface) {
$request->getEventDispatcher()->addListener('request.sent', $f = function (Event $event) use(&$f) {
while ($data = $event['request']->getBody()->read(8096)) {
}
$event['request']->getEventDispatcher()->removeListener('request.sent', $f);
});
}
$request->setResponse($item);
} elseif ($item instanceof CurlException) {
$item->setRequest($request);
$state = $request->setState(RequestInterface::STATE_ERROR, array('exception' => $item));
if ($state == RequestInterface::STATE_ERROR) {
throw $item;
}
}
return $this;
}