/vendor/guzzlehttp/guzzle/src/Event/AbstractTransferEvent.php |
@@ -0,0 +1,83 @@ |
<?php |
|
namespace GuzzleHttp\Event; |
|
use GuzzleHttp\Adapter\TransactionInterface; |
use GuzzleHttp\Message\ResponseInterface; |
|
/** |
* Event that contains transaction statistics (time over the wire, lookup time, |
* etc.). |
* |
* Adapters that create this event SHOULD add, at a minimum, the 'total_time' |
* transfer statistic that measures the amount of time, in seconds, taken to |
* complete a transfer for the current request/response cycle. Each event |
* pertains to a single request/response transaction, NOT the entire |
* transaction (e.g. redirects). |
* |
* Adapters that add transaction statistics SHOULD follow the same string |
* attribute names that are provided by cURL listed at |
* http://php.net/manual/en/function.curl-getinfo.php. However, not all |
* adapters will have access to the advanced statistics provided by cURL. The |
* most useful transfer statistics are as follows: |
* |
* - total_time: Total transaction time in seconds for last transfer |
* - namelookup_time: Time in seconds until name resolving was complete |
* - connect_time: Time in seconds it took to establish the connection |
* - pretransfer_time: Time in seconds from start until just before file |
* transfer begins. |
* - starttransfer_time: Time in seconds until the first byte is about to be |
* transferred. |
* - speed_download: Average download speed, measured in bytes/second. |
* - speed_upload: Average upload speed, measured in bytes/second. |
*/ |
abstract class AbstractTransferEvent extends AbstractRequestEvent |
{ |
private $transferInfo; |
|
/** |
* @param TransactionInterface $transaction Transaction |
* @param array $transferInfo Transfer statistics |
*/ |
public function __construct( |
TransactionInterface $transaction, |
$transferInfo = [] |
) { |
parent::__construct($transaction); |
$this->transferInfo = $transferInfo; |
} |
|
/** |
* Get all transfer information as an associative array if no $name |
* argument is supplied, or gets a specific transfer statistic if |
* a $name attribute is supplied (e.g., 'total_time'). |
* |
* @param string $name Name of the transfer stat to retrieve |
* |
* @return mixed|null|array |
*/ |
public function getTransferInfo($name = null) |
{ |
if (!$name) { |
return $this->transferInfo; |
} |
|
return isset($this->transferInfo[$name]) |
? $this->transferInfo[$name] |
: null; |
} |
|
/** |
* Get the response |
* |
* @return ResponseInterface|null |
*/ |
abstract public function getResponse(); |
|
/** |
* Intercept the request and associate a response |
* |
* @param ResponseInterface $response Response to set |
*/ |
abstract public function intercept(ResponseInterface $response); |
} |
/vendor/guzzlehttp/guzzle/src/Event/Emitter.php |
@@ -0,0 +1,157 @@ |
<?php |
|
namespace GuzzleHttp\Event; |
|
/** |
* Guzzle event emitter. |
* |
* Some of this class is based on the Symfony EventDispatcher component, which |
* ships with the following license: |
* |
* This file is part of the Symfony package. |
* |
* (c) Fabien Potencier <fabien@symfony.com> |
* |
* For the full copyright and license information, please view the LICENSE |
* file that was distributed with this source code. |
* |
* @link https://github.com/symfony/symfony/tree/master/src/Symfony/Component/EventDispatcher |
*/ |
class Emitter implements EmitterInterface |
{ |
/** @var array */ |
private $listeners = []; |
|
/** @var array */ |
private $sorted = []; |
|
public function on($eventName, callable $listener, $priority = 0) |
{ |
if ($priority === 'first') { |
$priority = isset($this->listeners[$eventName]) |
? max(array_keys($this->listeners[$eventName])) + 1 |
: 1; |
} elseif ($priority === 'last') { |
$priority = isset($this->listeners[$eventName]) |
? min(array_keys($this->listeners[$eventName])) - 1 |
: -1; |
} |
|
$this->listeners[$eventName][$priority][] = $listener; |
unset($this->sorted[$eventName]); |
} |
|
public function once($eventName, callable $listener, $priority = 0) |
{ |
$onceListener = function ( |
EventInterface $event, |
$eventName |
) use (&$onceListener, $eventName, $listener, $priority) { |
$this->removeListener($eventName, $onceListener); |
$listener($event, $eventName, $this); |
}; |
|
$this->on($eventName, $onceListener, $priority); |
} |
|
public function removeListener($eventName, callable $listener) |
{ |
if (!isset($this->listeners[$eventName])) { |
return; |
} |
|
foreach ($this->listeners[$eventName] as $priority => $listeners) { |
if (false !== ($key = array_search($listener, $listeners, true))) { |
unset( |
$this->listeners[$eventName][$priority][$key], |
$this->sorted[$eventName] |
); |
} |
} |
} |
|
public function listeners($eventName = null) |
{ |
// Return all events in a sorted priority order |
if ($eventName === null) { |
foreach (array_keys($this->listeners) as $eventName) { |
if (!isset($this->sorted[$eventName])) { |
$this->listeners($eventName); |
} |
} |
return $this->sorted; |
} |
|
// Return the listeners for a specific event, sorted in priority order |
if (!isset($this->sorted[$eventName])) { |
if (!isset($this->listeners[$eventName])) { |
return []; |
} else { |
krsort($this->listeners[$eventName]); |
$this->sorted[$eventName] = call_user_func_array( |
'array_merge', |
$this->listeners[$eventName] |
); |
} |
} |
|
return $this->sorted[$eventName]; |
} |
|
public function emit($eventName, EventInterface $event) |
{ |
if (isset($this->listeners[$eventName])) { |
foreach ($this->listeners($eventName) as $listener) { |
$listener($event, $eventName); |
if ($event->isPropagationStopped()) { |
break; |
} |
} |
} |
|
return $event; |
} |
|
public function attach(SubscriberInterface $subscriber) |
{ |
foreach ($subscriber->getEvents() as $eventName => $listeners) { |
if (is_array($listeners[0])) { |
foreach ($listeners as $listener) { |
$this->on( |
$eventName, |
[$subscriber, $listener[0]], |
isset($listener[1]) ? $listener[1] : 0 |
); |
} |
} else { |
$this->on( |
$eventName, |
[$subscriber, $listeners[0]], |
isset($listeners[1]) ? $listeners[1] : 0 |
); |
} |
} |
} |
|
public function detach(SubscriberInterface $subscriber) |
{ |
foreach ($subscriber->getEvents() as $eventName => $listener) { |
$this->removeListener($eventName, array($subscriber, $listener[0])); |
} |
} |
|
public function __call($name, $arguments) |
{ |
return \GuzzleHttp\deprecation_proxy( |
$this, |
$name, |
$arguments, |
[ |
'addSubscriber' => 'attach', |
'removeSubscriber' => 'detach', |
'addListener' => 'on', |
'dispatch' => 'emit' |
] |
); |
} |
} |
/vendor/guzzlehttp/guzzle/src/Event/EmitterInterface.php |
@@ -0,0 +1,88 @@ |
<?php |
|
namespace GuzzleHttp\Event; |
|
/** |
* Guzzle event emitter. |
*/ |
interface EmitterInterface |
{ |
/** |
* Binds a listener to a specific event. |
* |
* @param string $eventName Name of the event to bind to. |
* @param callable $listener Listener to invoke when triggered. |
* @param int|string $priority The higher this value, the earlier an event |
* listener will be triggered in the chain (defaults to 0). You can |
* pass "first" or "last" to dynamically specify the event priority |
* based on the current event priorities associated with the given |
* event name in the emitter. Use "first" to set the priority to the |
* current highest priority plus one. Use "last" to set the priority to |
* the current lowest event priority minus one. |
*/ |
public function on($eventName, callable $listener, $priority = 0); |
|
/** |
* Binds a listener to a specific event. After the listener is triggered |
* once, it is removed as a listener. |
* |
* @param string $eventName Name of the event to bind to. |
* @param callable $listener Listener to invoke when triggered. |
* @param int $priority The higher this value, the earlier an event |
* listener will be triggered in the chain (defaults to 0) |
*/ |
public function once($eventName, callable $listener, $priority = 0); |
|
/** |
* Removes an event listener from the specified event. |
* |
* @param string $eventName The event to remove a listener from |
* @param callable $listener The listener to remove |
*/ |
public function removeListener($eventName, callable $listener); |
|
/** |
* Gets the listeners of a specific event or all listeners if no event is |
* specified. |
* |
* @param string $eventName The name of the event. Pass null (the default) |
* to retrieve all listeners. |
* |
* @return array The event listeners for the specified event, or all event |
* listeners by event name. The format of the array when retrieving a |
* specific event list is an array of callables. The format of the array |
* when retrieving all listeners is an associative array of arrays of |
* callables. |
*/ |
public function listeners($eventName = null); |
|
/** |
* Emits an event to all registered listeners. |
* |
* Each event that is bound to the emitted eventName receives a |
* EventInterface, the name of the event, and the event emitter. |
* |
* @param string $eventName The name of the event to dispatch. |
* @param EventInterface $event The event to pass to the event handlers/listeners. |
* |
* @return EventInterface Returns the provided event object |
*/ |
public function emit($eventName, EventInterface $event); |
|
/** |
* Attaches an event subscriber. |
* |
* The subscriber is asked for all the events it is interested in and added |
* as an event listener for each event. |
* |
* @param SubscriberInterface $subscriber Subscriber to attach. |
*/ |
public function attach(SubscriberInterface $subscriber); |
|
/** |
* Detaches an event subscriber. |
* |
* @param SubscriberInterface $subscriber Subscriber to detach. |
*/ |
public function detach(SubscriberInterface $subscriber); |
} |
/vendor/guzzlehttp/guzzle/src/Event/ErrorEvent.php |
@@ -0,0 +1,80 @@ |
<?php |
|
namespace GuzzleHttp\Event; |
|
use GuzzleHttp\Adapter\TransactionInterface; |
use GuzzleHttp\Exception\RequestException; |
use GuzzleHttp\Message\ResponseInterface; |
|
/** |
* Event object emitted after a request has been sent and an error was |
* encountered. |
* |
* You may intercept the exception and inject a response into the event to |
* rescue the request. |
*/ |
class ErrorEvent extends AbstractTransferEvent |
{ |
private $exception; |
|
/** |
* @param TransactionInterface $transaction Transaction that contains the request |
* @param RequestException $e Exception encountered |
* @param array $transferStats Array of transfer statistics |
*/ |
public function __construct( |
TransactionInterface $transaction, |
RequestException $e, |
$transferStats = [] |
) { |
parent::__construct($transaction, $transferStats); |
$this->exception = $e; |
} |
|
/** |
* Intercept the exception and inject a response |
* |
* @param ResponseInterface $response Response to set |
*/ |
public function intercept(ResponseInterface $response) |
{ |
$this->stopPropagation(); |
$this->getTransaction()->setResponse($response); |
$this->exception->setThrowImmediately(false); |
RequestEvents::emitComplete($this->getTransaction()); |
} |
|
/** |
* Get the exception that was encountered |
* |
* @return RequestException |
*/ |
public function getException() |
{ |
return $this->exception; |
} |
|
/** |
* Get the response the was received (if any) |
* |
* @return ResponseInterface|null |
*/ |
public function getResponse() |
{ |
return $this->getException()->getResponse(); |
} |
|
/** |
* Request that a ParallelAdapterInterface throw the associated exception |
* if the exception is unhandled. |
* |
* If the error event was not emitted from a ParallelAdapterInterface, then |
* the effect of this method is nil. |
* |
* @param bool $throwImmediately Whether or not to throw immediately |
*/ |
public function throwImmediately($throwImmediately) |
{ |
$this->exception->setThrowImmediately($throwImmediately); |
} |
} |
/vendor/guzzlehttp/guzzle/src/Event/ListenerAttacherTrait.php |
@@ -0,0 +1,89 @@ |
<?php |
|
namespace GuzzleHttp\Event; |
|
/** |
* Trait that provides methods for extract event listeners specified in an array |
* and attaching them to an emitter owned by the object or one of its direct |
* dependencies. |
*/ |
trait ListenerAttacherTrait |
{ |
/** |
* Attaches event listeners and properly sets their priorities and whether |
* or not they are are only executed once. |
* |
* @param HasEmitterInterface $object Object that has the event emitter. |
* @param array $listeners Array of hashes representing event |
* event listeners. Each item contains |
* "name", "fn", "priority", & "once". |
*/ |
private function attachListeners(HasEmitterInterface $object, array $listeners) |
{ |
$emitter = $object->getEmitter(); |
foreach ($listeners as $el) { |
if ($el['once']) { |
$emitter->once($el['name'], $el['fn'], $el['priority']); |
} else { |
$emitter->on($el['name'], $el['fn'], $el['priority']); |
} |
} |
} |
|
/** |
* Extracts the allowed events from the provided array, and ignores anything |
* else in the array. The event listener must be specified as a callable or |
* as an array of event listener data ("name", "fn", "priority", "once"). |
* |
* @param array $source Array containing callables or hashes of data to be |
* prepared as event listeners. |
* @param array $events Names of events to look for in the provided $source |
* array. Other keys are ignored. |
* @return array |
*/ |
private function prepareListeners(array $source, array $events) |
{ |
$listeners = []; |
foreach ($events as $name) { |
if (isset($source[$name])) { |
$this->buildListener($name, $source[$name], $listeners); |
} |
} |
|
return $listeners; |
} |
|
/** |
* Creates a complete event listener definition from the provided array of |
* listener data. Also works recursively if more than one listeners are |
* contained in the provided array. |
* |
* @param string $name Name of the event the listener is for. |
* @param array|callable $data Event listener data to prepare. |
* @param array $listeners Array of listeners, passed by reference. |
* |
* @throws \InvalidArgumentException if the event data is malformed. |
*/ |
private function buildListener($name, $data, &$listeners) |
{ |
static $defaults = ['priority' => 0, 'once' => false]; |
|
// If a callable is provided, normalize it to the array format. |
if (is_callable($data)) { |
$data = ['fn' => $data]; |
} |
|
// Prepare the listener and add it to the array, recursively. |
if (isset($data['fn'])) { |
$data['name'] = $name; |
$listeners[] = $data + $defaults; |
} elseif (is_array($data)) { |
foreach ($data as $listenerData) { |
$this->buildListener($name, $listenerData, $listeners); |
} |
} else { |
throw new \InvalidArgumentException('Each event listener must be a ' |
. 'callable or an associative array containing a "fn" key.'); |
} |
} |
} |
/vendor/guzzlehttp/guzzle/src/Event/RequestEvents.php |
@@ -0,0 +1,167 @@ |
<?php |
|
namespace GuzzleHttp\Event; |
|
use GuzzleHttp\Adapter\TransactionInterface; |
use GuzzleHttp\Exception\RequestException; |
|
/** |
* Contains methods used to manage the request event lifecycle. |
*/ |
final class RequestEvents |
{ |
// Generic event priorities |
const EARLY = 10000; |
const LATE = -10000; |
|
// "before" priorities |
const PREPARE_REQUEST = -100; |
const SIGN_REQUEST = -10000; |
|
// "complete" and "error" response priorities |
const VERIFY_RESPONSE = 100; |
const REDIRECT_RESPONSE = 200; |
|
/** |
* Emits the before send event for a request and emits an error |
* event if an error is encountered during the before send. |
* |
* @param TransactionInterface $transaction |
* |
* @throws RequestException |
*/ |
public static function emitBefore(TransactionInterface $transaction) |
{ |
$request = $transaction->getRequest(); |
try { |
$request->getEmitter()->emit( |
'before', |
new BeforeEvent($transaction) |
); |
} catch (RequestException $e) { |
// When a RequestException has been emitted through emitError, the |
// exception is marked as "emitted". This means that the exception |
// had a chance to be rescued but was not. In this case, this method |
// must not emit the error again, but rather throw the exception. |
// This prevents RequestExceptions encountered during the before |
// event from being emitted to listeners twice. |
if ($e->emittedError()) { |
throw $e; |
} |
self::emitError($transaction, $e); |
} catch (\Exception $e) { |
self::emitError($transaction, $e); |
} |
} |
|
/** |
* Emits the complete event for a request and emits an error |
* event if an error is encountered during the after send. |
* |
* @param TransactionInterface $transaction Transaction to emit for |
* @param array $stats Transfer stats |
* |
* @throws RequestException |
*/ |
public static function emitComplete( |
TransactionInterface $transaction, |
array $stats = [] |
) { |
$request = $transaction->getRequest(); |
$transaction->getResponse()->setEffectiveUrl($request->getUrl()); |
try { |
$request->getEmitter()->emit( |
'complete', |
new CompleteEvent($transaction, $stats) |
); |
} catch (RequestException $e) { |
self::emitError($transaction, $e, $stats); |
} |
} |
|
/** |
* Emits the headers event for a request. |
* |
* @param TransactionInterface $transaction Transaction to emit for |
*/ |
public static function emitHeaders(TransactionInterface $transaction) |
{ |
$transaction->getRequest()->getEmitter()->emit( |
'headers', |
new HeadersEvent($transaction) |
); |
} |
|
/** |
* Emits an error event for a request and accounts for the propagation |
* of an error event being stopped to prevent the exception from being |
* thrown. |
* |
* @param TransactionInterface $transaction |
* @param \Exception $e |
* @param array $stats |
* |
* @throws \GuzzleHttp\Exception\RequestException |
*/ |
public static function emitError( |
TransactionInterface $transaction, |
\Exception $e, |
array $stats = [] |
) { |
$request = $transaction->getRequest(); |
|
// Convert non-request exception to a wrapped exception |
if (!($e instanceof RequestException)) { |
$e = new RequestException($e->getMessage(), $request, null, $e); |
} |
|
// Mark the exception as having been emitted for an error event. This |
// works in tandem with the emitBefore method to prevent the error |
// event from being triggered twice for the same exception. |
$e->emittedError(true); |
|
// Dispatch an event and allow interception |
if (!$request->getEmitter()->emit( |
'error', |
new ErrorEvent($transaction, $e, $stats) |
)->isPropagationStopped()) { |
throw $e; |
} |
} |
|
/** |
* Converts an array of event options into a formatted array of valid event |
* configuration. |
* |
* @param array $options Event array to convert |
* @param array $events Event names to convert in the options array. |
* @param mixed $handler Event handler to utilize |
* |
* @return array |
* @throws \InvalidArgumentException if the event config is invalid |
* @internal |
*/ |
public static function convertEventArray( |
array $options, |
array $events, |
$handler |
) { |
foreach ($events as $name) { |
if (!isset($options[$name])) { |
$options[$name] = [$handler]; |
} elseif (is_callable($options[$name])) { |
$options[$name] = [$options[$name], $handler]; |
} elseif (is_array($options[$name])) { |
if (isset($options[$name]['fn'])) { |
$options[$name] = [$options[$name], $handler]; |
} else { |
$options[$name][] = $handler; |
} |
} else { |
throw new \InvalidArgumentException('Invalid event format'); |
} |
} |
|
return $options; |
} |
} |
/vendor/guzzlehttp/guzzle/src/Event/SubscriberInterface.php |
@@ -0,0 +1,35 @@ |
<?php |
|
namespace GuzzleHttp\Event; |
|
/** |
* SubscriberInterface provides an array of events to an |
* EventEmitterInterface when it is registered. The emitter then binds the |
* listeners specified by the EventSubscriber. |
* |
* This interface is based on the SubscriberInterface of the Symfony. |
* @link https://github.com/symfony/symfony/tree/master/src/Symfony/Component/EventDispatcher |
*/ |
interface SubscriberInterface |
{ |
/** |
* Returns an array of event names this subscriber wants to listen to. |
* |
* The returned array keys MUST map to an event name. Each array value |
* MUST be an array in which the first element is the name of a function |
* on the EventSubscriber OR an array of arrays in the aforementioned |
* format. The second element in the array is optional, and if specified, |
* designates the event priority. |
* |
* For example, the following are all valid: |
* |
* - ['eventName' => ['methodName']] |
* - ['eventName' => ['methodName', $priority]] |
* - ['eventName' => [['methodName'], ['otherMethod']] |
* - ['eventName' => [['methodName'], ['otherMethod', $priority]] |
* - ['eventName' => [['methodName', $priority], ['otherMethod', $priority]] |
* |
* @return array |
*/ |
public function getEvents(); |
} |