mirror of
https://github.com/guzzle/guzzle.git
synced 2025-01-17 13:28:13 +01:00
[Common] [Http] [Service] Renaming the Subject namespace to Event. Removing the redundant behavior of signal slots vs intercepting filters for emitting events from requests and clients. Updating the SubjectMediator to become EventManager and simplifying how it emits events to observers.
This commit is contained in:
parent
83a6f47f94
commit
3c0984f03e
@ -4,7 +4,7 @@
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Common\Subject;
|
||||
namespace Guzzle\Common\Event;
|
||||
|
||||
/**
|
||||
* Abstract subject class
|
||||
@ -14,21 +14,21 @@ namespace Guzzle\Common\Subject;
|
||||
abstract class AbstractSubject implements Subject
|
||||
{
|
||||
/**
|
||||
* @var SubjectMediator
|
||||
* @var EventManager
|
||||
*/
|
||||
protected $subjectMediator;
|
||||
protected $eventManager;
|
||||
|
||||
/**
|
||||
* Get the subject mediator associated with the subject
|
||||
*
|
||||
* @return SubjectMediator
|
||||
* @return EventManager
|
||||
*/
|
||||
public function getSubjectMediator()
|
||||
public function getEventManager()
|
||||
{
|
||||
if (!$this->subjectMediator) {
|
||||
$this->subjectMediator = new SubjectMediator($this);
|
||||
if (!$this->eventManager) {
|
||||
$this->eventManager = new EventManager($this);
|
||||
}
|
||||
|
||||
return $this->subjectMediator;
|
||||
return $this->eventManager;
|
||||
}
|
||||
}
|
@ -4,38 +4,33 @@
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Common\Subject;
|
||||
namespace Guzzle\Common\Event;
|
||||
|
||||
/**
|
||||
* Subject mediator that connects {@see Subject}s and their {@see Observer}s for
|
||||
* loose coupling.
|
||||
* Subject mediator event manager that connects {@see Subject}s and their
|
||||
* {@see Observer}s for loose coupling.
|
||||
*
|
||||
* @author Michael Dowling <michael@guzzle-project.org>
|
||||
*/
|
||||
class SubjectMediator
|
||||
class EventManager
|
||||
{
|
||||
/**
|
||||
* @var Subject Mediated {@see Subject} to connect with {@see Observer}s
|
||||
*/
|
||||
protected $subject;
|
||||
|
||||
/**
|
||||
* @var string The current state of the {@see Subject}
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* @var mixed Contextual information used with state change notifications
|
||||
*/
|
||||
protected $stateContext;
|
||||
|
||||
/**
|
||||
* @var array Array of {@see Observer} objects.
|
||||
*/
|
||||
protected $observers = array();
|
||||
|
||||
/**
|
||||
* Construct a new SubjectMediator
|
||||
* @var array Array of observer priorities
|
||||
*/
|
||||
protected $priorities = array();
|
||||
|
||||
/**
|
||||
* Construct a new EventManager
|
||||
*
|
||||
* @param Subject Subject colleague object
|
||||
* @param array $observers (optional) Array of {@see Observer} objects
|
||||
@ -54,13 +49,41 @@ class SubjectMediator
|
||||
* Attach a new observer.
|
||||
*
|
||||
* @param Observer $observer Object that observes the subject.
|
||||
* @param int $priority (optional) Priority to attach to the subject. The
|
||||
* higher the priority, the sooner it will be notified
|
||||
*
|
||||
* @return Observer Returns the $observer that was attached.
|
||||
*/
|
||||
public function attach(Observer $observer)
|
||||
public function attach(Observer $observer, $priority = 0)
|
||||
{
|
||||
if (!$this->hasObserver($observer)) {
|
||||
|
||||
$hash = spl_object_hash($observer);
|
||||
$this->observers[] = $observer;
|
||||
|
||||
if ($priority) {
|
||||
$this->priorities[$hash] = $priority;
|
||||
}
|
||||
$priorities = $this->priorities;
|
||||
|
||||
// Sort the events by priority
|
||||
usort($this->observers, function($a, $b) use ($priorities) {
|
||||
$priority1 = $priority2 = 0;
|
||||
$ah = spl_object_hash($a);
|
||||
$bh = spl_object_hash($b);
|
||||
if (isset($priorities[$ah])) {
|
||||
$priority1 = $priorities[$ah];
|
||||
}
|
||||
if (isset($priorities[$bh])) {
|
||||
$priority2 = $priorities[$bh];
|
||||
}
|
||||
|
||||
if ($priority1 === $priority2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $priority1 > $priority2 ? -1 : 1;
|
||||
});
|
||||
}
|
||||
|
||||
return $observer;
|
||||
@ -107,40 +130,29 @@ class SubjectMediator
|
||||
* Set the state and stateContext of the subject and notify all observers
|
||||
* of the state change.
|
||||
*
|
||||
* @param string $state (optional) State of the object. Leave unchanged to
|
||||
* use the current state.
|
||||
* @param mixed $stateContext (optional) The context of the object's state
|
||||
* @param bool $unsetContext (optional) Set to TRUE to remove the
|
||||
* stateContext of the subject after sending the notification.
|
||||
* @param string $event (optional) Event signal to emit
|
||||
* @param mixed $context (optional) Context about the event
|
||||
* @param bool $until (optional) Set to TRUE to stop event propagation when
|
||||
* one of the observers returns TRUE
|
||||
*
|
||||
* @return array Returns an array containing the response of each observer
|
||||
*/
|
||||
public function notify($state = Subject::STATE_UNCHANGED,
|
||||
$stateContext = Subject::STATE_UNCHANGED, $unsetContext = false)
|
||||
public function notify($event, $context = null, $until = false)
|
||||
{
|
||||
if ($state != Subject::STATE_UNCHANGED) {
|
||||
$this->state = (string) $state;
|
||||
}
|
||||
|
||||
if ($stateContext != Subject::STATE_UNCHANGED) {
|
||||
$this->stateContext = $stateContext;
|
||||
}
|
||||
|
||||
$responses = array();
|
||||
|
||||
foreach ($this->observers as $observer) {
|
||||
if ($observer) {
|
||||
$result = $observer->update($this);
|
||||
$result = $observer->update($this->subject, $event, $context);
|
||||
if ($result) {
|
||||
$responses[] = $result;
|
||||
if ($until == true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($unsetContext) {
|
||||
$this->stateContext = null;
|
||||
}
|
||||
|
||||
|
||||
return $responses;
|
||||
}
|
||||
|
||||
@ -168,30 +180,6 @@ class SubjectMediator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get contextual information about the state of the subject.
|
||||
*
|
||||
* @param mixed $default (optional) Pass a default value so that if the
|
||||
* state context of the subject isn't set, the default value will be
|
||||
* returned.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getContext($default = null)
|
||||
{
|
||||
return (isset($this->stateContext)) ? $this->stateContext : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the state of the subject
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mediated {@see Subject} or NULL if no Subject has been associated
|
||||
*
|
||||
@ -221,18 +209,4 @@ class SubjectMediator
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if the Subject is an instance of a class or a specific class
|
||||
*
|
||||
* @param string|Object $check A concrete Subject or class name of a subject
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is($check)
|
||||
{
|
||||
return (is_string($check))
|
||||
? $this->subject instanceof $check
|
||||
: $this->subject === $check;
|
||||
}
|
||||
}
|
26
library/Guzzle/Common/Event/Observer.php
Normal file
26
library/Guzzle/Common/Event/Observer.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Guzzle PHP <http://www.guzzlephp.org>
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Common\Event;
|
||||
|
||||
/**
|
||||
* Guzzle Observer class
|
||||
*
|
||||
* @author Michael Dowling <michael@guzzle-project.org>
|
||||
*/
|
||||
interface Observer
|
||||
{
|
||||
/**
|
||||
* Receive notifications from a EventManager
|
||||
*
|
||||
* @param Subject $subject Subject emitting the event
|
||||
* @param string $event Event signal state
|
||||
* @param mixed $context (optional) Contextual information
|
||||
*
|
||||
* @return null|bool|mixed
|
||||
*/
|
||||
public function update(Subject $subject, $event, $context = null);
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Common\Subject;
|
||||
namespace Guzzle\Common\Event;
|
||||
|
||||
/**
|
||||
* Guzzle subject interface
|
||||
@ -24,7 +24,7 @@ interface Subject
|
||||
/**
|
||||
* Get the subject mediator associated with the subject
|
||||
*
|
||||
* @return SubjectMediator
|
||||
* @return EventManager
|
||||
*/
|
||||
public function getSubjectMediator();
|
||||
public function getEventManager();
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Guzzle PHP <http://www.guzzlephp.org>
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Common\Subject;
|
||||
|
||||
/**
|
||||
* Guzzle Observer class
|
||||
*
|
||||
* @author Michael Dowling <michael@guzzle-project.org>
|
||||
*/
|
||||
interface Observer
|
||||
{
|
||||
/**
|
||||
* Receive notifications from a SubjectMediator
|
||||
*
|
||||
* @param SubjectMediator $subject Subject mediator sending the update
|
||||
*/
|
||||
public function update(SubjectMediator $subject);
|
||||
}
|
@ -336,7 +336,7 @@ class CurlFactory implements CurlFactoryInterface
|
||||
CURLOPT_PROXY => '', // Reset proxy settings,
|
||||
CURLOPT_NOPROGRESS => false,
|
||||
CURLOPT_WRITEFUNCTION => function($curl, $data) use ($request) {
|
||||
$request->getSubjectMediator()->notify('curl.callback.write', $data);
|
||||
$request->getEventManager()->notify('curl.callback.write', $data);
|
||||
|
||||
return $request->getResponse()->getBody()->write($data);
|
||||
},
|
||||
@ -346,13 +346,13 @@ class CurlFactory implements CurlFactoryInterface
|
||||
CURLOPT_READFUNCTION => function($ch, $fd, $length) use ($request) {
|
||||
$read = ($request->getBody()) ? $request->getBody()->read($length) : 0;
|
||||
if ($read) {
|
||||
$request->getSubjectMediator()->notify('curl.callback.read', $read, true);
|
||||
$request->getEventManager()->notify('curl.callback.read', $read, true);
|
||||
}
|
||||
|
||||
return $read === false || $read === 0 ? '' : $read;
|
||||
},
|
||||
CURLOPT_PROGRESSFUNCTION => function($downloadSize, $downloaded, $uploadSize, $uploaded) use ($request) {
|
||||
$request->getSubjectMediator()->notify('curl.callback.progress', array(
|
||||
$request->getEventManager()->notify('curl.callback.progress', array(
|
||||
'download_size' => $downloadSize,
|
||||
'downloaded' => $downloaded,
|
||||
'upload_size' => $uploadSize,
|
||||
|
@ -35,11 +35,11 @@ class DefaultResponseProcessor implements ResponseProcessorInterface
|
||||
$e->setResponse($response);
|
||||
$e->setRequest($request);
|
||||
|
||||
$request->getSubjectMediator()->notify('request.failure', $e);
|
||||
$request->getEventManager()->notify('request.failure', $e);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$request->getSubjectMediator()->notify('request.success', $response);
|
||||
$request->getEventManager()->notify('request.success', $response);
|
||||
}
|
||||
}
|
@ -8,9 +8,8 @@ namespace Guzzle\Http\Message;
|
||||
|
||||
use Guzzle\Common\Collection;
|
||||
use Guzzle\Common\Filter\Chain;
|
||||
use Guzzle\Common\Filter\FilterInterface;
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Subject\Observer;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\QueryString;
|
||||
use Guzzle\Http\MultipartFormData;
|
||||
@ -42,7 +41,7 @@ class EntityEnclosingRequest extends Request implements EntityEnclosingRequestIn
|
||||
// Process the object as it might contain POST fields that need to be
|
||||
// generated into an EntityBody
|
||||
if (!$this->response) {
|
||||
$this->process($this);
|
||||
$this->getEventManager()->notify('request.prepare_entity_body');
|
||||
}
|
||||
|
||||
$body = count($this->getPostFields()) && 0 == count($this->getPostFiles())
|
||||
@ -82,10 +81,13 @@ class EntityEnclosingRequest extends Request implements EntityEnclosingRequestIn
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function process($context)
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
// @codeCoverageIgnoreStart
|
||||
if ($context !== $this) {
|
||||
if ($subject !== $this ||
|
||||
!($event == 'request.prepare_entity_body' ||
|
||||
$event == 'request.before_send' ||
|
||||
$event == 'request.curl.before_create')) {
|
||||
return;
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
@ -234,9 +236,10 @@ class EntityEnclosingRequest extends Request implements EntityEnclosingRequestIn
|
||||
*/
|
||||
private function addChain()
|
||||
{
|
||||
$chain = $this->getPrepareChain();
|
||||
if (!$chain->hasFilter($this)) {
|
||||
$chain->prependFilter($this);
|
||||
$sm = $this->getEventManager();
|
||||
if (!$sm->hasObserver($this)) {
|
||||
// Attach to itself at a high priority
|
||||
$sm->attach($this, 9999);
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
|
||||
namespace Guzzle\Http\Message;
|
||||
|
||||
use Guzzle\Common\Filter\FilterInterface;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\QueryString;
|
||||
|
||||
@ -15,7 +15,7 @@ use Guzzle\Http\QueryString;
|
||||
*
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
interface EntityEnclosingRequestInterface extends RequestInterface, FilterInterface
|
||||
interface EntityEnclosingRequestInterface extends RequestInterface, Observer
|
||||
{
|
||||
/**
|
||||
* Set the body of the request
|
||||
|
@ -8,9 +8,8 @@ namespace Guzzle\Http\Message;
|
||||
|
||||
use Guzzle\Guzzle;
|
||||
use Guzzle\Common\Collection;
|
||||
use Guzzle\Common\Filter\Chain;
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Subject\Observer;
|
||||
use Guzzle\Common\Event\EventManager;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
use Guzzle\Http\Curl\CurlFactoryInterface;
|
||||
use Guzzle\Http\Curl\CurlFactory;
|
||||
use Guzzle\Http\Curl\CurlHandle;
|
||||
@ -46,9 +45,9 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
protected $method;
|
||||
|
||||
/**
|
||||
* @var SubjectMediator Subject mediator
|
||||
* @var EventManager Subject mediator
|
||||
*/
|
||||
protected $subjectMediator;
|
||||
protected $eventManager;
|
||||
|
||||
/**
|
||||
* @var Response Response of the request
|
||||
@ -61,20 +60,10 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
protected $responseBody;
|
||||
|
||||
/**
|
||||
* @var bool Has the response been sent through the process chain
|
||||
* @var bool Has the response been processed
|
||||
*/
|
||||
protected $processedResponse = false;
|
||||
|
||||
/**
|
||||
* @var Chain Chain of intercepting filters to process the request before sending
|
||||
*/
|
||||
protected $prepareChain;
|
||||
|
||||
/**
|
||||
* @var Chain Chain of intercepting filters to process the request after sending
|
||||
*/
|
||||
protected $processChain;
|
||||
|
||||
/**
|
||||
* @var string State of the request object
|
||||
*/
|
||||
@ -144,7 +133,7 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
$this->curlFactory = $curlFactory ?: CurlFactory::getInstance();
|
||||
$this->curlOptions = new Collection();
|
||||
$this->cookie = Cookie::factory($this->getHeader('Cookie'));
|
||||
$this->subjectMediator = new SubjectMediator($this);
|
||||
$this->eventManager = new EventManager($this);
|
||||
|
||||
if (!$this->hasHeader('User-Agent', true)) {
|
||||
$this->setHeader('User-Agent', Guzzle::getDefaultUserAgent());
|
||||
@ -172,12 +161,10 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
public function __clone()
|
||||
{
|
||||
// Clone object properties
|
||||
$this->prepareChain = $this->prepareChain ? clone $this->prepareChain : null;
|
||||
$this->processChain = $this->processChain ? clone $this->processChain : null;
|
||||
$this->subjectMediator = clone $this->subjectMediator;
|
||||
$this->eventManager = clone $this->eventManager;
|
||||
|
||||
// Reattach any plugins
|
||||
foreach ($this->subjectMediator->getAttached() as $observer) {
|
||||
foreach ($this->eventManager->getAttached() as $observer) {
|
||||
if ($observer instanceof AbstractPlugin) {
|
||||
$observer->attach($this);
|
||||
}
|
||||
@ -260,13 +247,13 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the SubjectMediator of the request
|
||||
* Get the EventManager of the request
|
||||
*
|
||||
* @return SubjectMediator
|
||||
* @return EventManager
|
||||
*/
|
||||
public function getSubjectMediator()
|
||||
public function getEventManager()
|
||||
{
|
||||
return $this->subjectMediator;
|
||||
return $this->eventManager;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -281,14 +268,13 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
try {
|
||||
try {
|
||||
$this->state = self::STATE_TRANSFER;
|
||||
$this->getSubjectMediator()->notify('request.before_send');
|
||||
$this->getPrepareChain()->process($this);
|
||||
$this->getEventManager()->notify('request.before_send');
|
||||
if (!$this->response && !$this->getParams()->get('queued_response')) {
|
||||
curl_exec($this->getCurlHandle()->getHandle());
|
||||
}
|
||||
$this->setState(self::STATE_COMPLETE);
|
||||
} catch (BadResponseException $e) {
|
||||
$this->getSubjectMediator()->notify('request.bad_response');
|
||||
$this->getEventManager()->notify('request.bad_response');
|
||||
if ($this->response) {
|
||||
$e->setResponse($this->response);
|
||||
}
|
||||
@ -296,7 +282,7 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
}
|
||||
} catch (RequestException $e) {
|
||||
$e->setRequest($this);
|
||||
$this->getSubjectMediator()->notify('request.exception', $e);
|
||||
$this->getEventManager()->notify('request.exception', $e);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@ -315,36 +301,6 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the intercepting filter Chain that is processed before the request
|
||||
* is sent.
|
||||
*
|
||||
* @return Chain
|
||||
*/
|
||||
public function getPrepareChain()
|
||||
{
|
||||
if (!$this->prepareChain) {
|
||||
$this->prepareChain = new Chain();
|
||||
}
|
||||
|
||||
return $this->prepareChain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the intercepting filter Chain that is processed after the response is
|
||||
* received
|
||||
*
|
||||
* @return Chain
|
||||
*/
|
||||
public function getProcessChain()
|
||||
{
|
||||
if (!$this->processChain) {
|
||||
$this->processChain = new Chain();
|
||||
}
|
||||
|
||||
return $this->processChain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the collection of key value pairs that will be used as the query
|
||||
* string in the request
|
||||
@ -659,9 +615,9 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
{
|
||||
// Create a new cURL handle using the cURL factory
|
||||
if (!$this->curlHandle) {
|
||||
// Call the prepare chain to prepare the request
|
||||
$this->getPrepareChain()->process($this);
|
||||
$this->getEventManager()->notify('request.curl.before_create');
|
||||
$this->curlHandle = $this->curlFactory->getHandle($this);
|
||||
$this->getEventManager()->notify('request.curl.after_create', $this->curlHandle);
|
||||
}
|
||||
|
||||
return $this->curlHandle;
|
||||
@ -699,7 +655,7 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
|
||||
if (preg_match('/^\HTTP\/1\.[0|1]\s\d{3}\s.+$/', $data)) {
|
||||
list($dummy, $code, $status) = explode(' ', str_replace("\r\n", '', $data), 3);
|
||||
$this->getSubjectMediator()->notify('transaction.receive_response_header', array(
|
||||
$this->getEventManager()->notify('transaction.receive_response_header', array(
|
||||
'header' => 'HTTP',
|
||||
'value' => $code
|
||||
), true);
|
||||
@ -710,7 +666,7 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
$this->response->addHeaders(array(
|
||||
$header => $value
|
||||
));
|
||||
$this->getSubjectMediator()->notify('transaction.receive_response_header', array(
|
||||
$this->getEventManager()->notify('transaction.receive_response_header', array(
|
||||
'header' => $header,
|
||||
'value' => $value
|
||||
), true);
|
||||
@ -746,7 +702,7 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
$this->getParams()->set('queued_response', $response);
|
||||
}
|
||||
|
||||
$this->getSubjectMediator()->notify('request.set_response', $this->response);
|
||||
$this->getEventManager()->notify('request.set_response', $this->response);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -1017,16 +973,15 @@ class Request extends AbstractMessage implements RequestInterface
|
||||
}
|
||||
}
|
||||
|
||||
$this->getSubjectMediator()->notify('request.sent', $this);
|
||||
$this->state = self::STATE_COMPLETE;
|
||||
$this->getProcessChain()->process($this);
|
||||
$this->getEventManager()->notify('request.sent', $this);
|
||||
|
||||
// Some response processor can remove the response or reset the state
|
||||
if ($this->state == RequestInterface::STATE_COMPLETE) {
|
||||
|
||||
$this->processedResponse = true;
|
||||
|
||||
$this->getSubjectMediator()->notify('request.complete', $this->response);
|
||||
$this->getEventManager()->notify('request.complete', $this->response);
|
||||
|
||||
// Release the cURL handle
|
||||
$this->releaseCurlHandle();
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
namespace Guzzle\Http\Message;
|
||||
|
||||
use Guzzle\Common\Subject\Subject;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\QueryString;
|
||||
use Guzzle\Http\Curl\CurlFactoryInterface;
|
||||
@ -88,22 +88,6 @@ interface RequestInterface extends MessageInterface, Subject
|
||||
*/
|
||||
public function getResponse();
|
||||
|
||||
/**
|
||||
* Get the intercepting filter Chain that is processed before the request
|
||||
* is sent.
|
||||
*
|
||||
* @return Chain
|
||||
*/
|
||||
public function getPrepareChain();
|
||||
|
||||
/**
|
||||
* Get the intercepting filter Chain that is processed after the response is
|
||||
* received
|
||||
*
|
||||
* @return Chain
|
||||
*/
|
||||
public function getProcessChain();
|
||||
|
||||
/**
|
||||
* Get the collection of key value pairs that will be used as the query
|
||||
* string in the request
|
||||
|
@ -7,7 +7,7 @@
|
||||
namespace Guzzle\Http\Message;
|
||||
|
||||
use Guzzle\Common\Collection;
|
||||
use Guzzle\Common\Subject\AbstractSubject;
|
||||
use Guzzle\Common\Event\AbstractSubject;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\HttpException;
|
||||
|
||||
|
@ -7,9 +7,8 @@
|
||||
namespace Guzzle\Http\Plugin;
|
||||
|
||||
use Guzzle\Common;
|
||||
use Guzzle\Common\Filter\FilterInterface;
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Subject\Observer;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
@ -17,8 +16,13 @@ use Guzzle\Http\Message\RequestInterface;
|
||||
*
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
abstract class AbstractPlugin implements Observer, FilterInterface
|
||||
abstract class AbstractPlugin implements Observer
|
||||
{
|
||||
/**
|
||||
* @var int Priority to attach to this plugin. Override in subclasses.
|
||||
*/
|
||||
protected $priority = 0;
|
||||
|
||||
/**
|
||||
* Check if the plugin is attached to a request
|
||||
*
|
||||
@ -28,7 +32,7 @@ abstract class AbstractPlugin implements Observer, FilterInterface
|
||||
*/
|
||||
public function isAttached(RequestInterface $request)
|
||||
{
|
||||
return $request->getSubjectMediator()->hasObserver($this) && $request->getPrepareChain()->hasFilter($this) && $request->getProcessChain()->hasFilter($this);
|
||||
return $request->getEventManager()->hasObserver($this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,9 +56,7 @@ abstract class AbstractPlugin implements Observer, FilterInterface
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
$request->getSubjectMediator()->attach($this);
|
||||
$request->getPrepareChain()->addFilter($this);
|
||||
$request->getProcessChain()->addFilter($this);
|
||||
$request->getEventManager()->attach($this, $this->priority);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -70,28 +72,18 @@ abstract class AbstractPlugin implements Observer, FilterInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
$request->getSubjectMediator()->detach($this);
|
||||
$request->getPrepareChain()->removeFilter($this);
|
||||
$request->getProcessChain()->removeFilter($this);
|
||||
$request->getEventManager()->detach($this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Observer update method
|
||||
*
|
||||
* @param SubjectMediator $subject Subject of the notification
|
||||
* {@inheritdoc}
|
||||
* @codeCoverageIgnoreStart
|
||||
*/
|
||||
public function update(SubjectMediator $subject) {}
|
||||
|
||||
/**
|
||||
* Intercepting filter process method
|
||||
*
|
||||
* @param RequestInterface $context
|
||||
* @codeCoverageIgnoreStart
|
||||
*/
|
||||
public function process($context) {}
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to run when the plugin is attached to a request
|
||||
@ -101,5 +93,7 @@ abstract class AbstractPlugin implements Observer, FilterInterface
|
||||
* @return null|bool Returns TRUE or FALSE if it can attach or NULL if indifferent
|
||||
* @codeCoverageIgnoreStart
|
||||
*/
|
||||
protected function handleAttach(RequestInterface $request) {}
|
||||
protected function handleAttach(RequestInterface $request)
|
||||
{
|
||||
}
|
||||
}
|
@ -8,9 +8,9 @@ namespace Guzzle\Http\Plugin\Cache;
|
||||
|
||||
use Guzzle\Guzzle;
|
||||
use Guzzle\Common\CacheAdapter\CacheAdapterInterface;
|
||||
use Guzzle\Common\Filter\FilterInterface;
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
use Guzzle\Http\Message\EntityEnclosingRequestInterface;
|
||||
use Guzzle\Http\Message\Response;
|
||||
use Guzzle\Http\Message\Request;
|
||||
use Guzzle\Http\Plugin\AbstractPlugin;
|
||||
@ -24,7 +24,7 @@ use Guzzle\Http\Plugin\AbstractPlugin;
|
||||
*
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
class CachePlugin extends AbstractPlugin implements FilterInterface
|
||||
class CachePlugin extends AbstractPlugin
|
||||
{
|
||||
/**
|
||||
* @var CacheAdapter Cache adapter used to write cache data to cache objects
|
||||
@ -148,52 +148,50 @@ class CachePlugin extends AbstractPlugin implements FilterInterface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param RequestInterface $command Request to process
|
||||
* @param RequestInterface $subject Request to process
|
||||
*/
|
||||
public function process($command)
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
// @codeCoverageIgnoreStart
|
||||
if (!$command instanceof RequestInterface) {
|
||||
if (!($subject instanceof RequestInterface)) {
|
||||
return;
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
if ($command->getState() != RequestInterface::STATE_COMPLETE) {
|
||||
/* @var $subject EntityEnclosingRequest */
|
||||
if ($event == 'request.before_send' && $subject->canCache()) {
|
||||
|
||||
if ($command->canCache()) {
|
||||
// This request is being prepared
|
||||
$key = spl_object_hash($subject);
|
||||
$hashKey = $this->getCacheKey($subject);
|
||||
$this->cached[$key] = $hashKey;
|
||||
$cachedData = $this->getCacheAdapter()->fetch($hashKey);
|
||||
|
||||
// This request is being prepared
|
||||
$key = spl_object_hash($command);
|
||||
$hashKey = $this->getCacheKey($command);
|
||||
$this->cached[$key] = $hashKey;
|
||||
$cachedData = $this->getCacheAdapter()->fetch($hashKey);
|
||||
// If the cached data was found, then make the request into a
|
||||
// manually set request
|
||||
if ($cachedData) {
|
||||
if ($this->serialize) {
|
||||
$cachedData = unserialize($cachedData);
|
||||
}
|
||||
unset($this->cached[$key]);
|
||||
$response = new Response($cachedData['c'], $cachedData['h'], $cachedData['b']);
|
||||
$response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
|
||||
$response->setHeader('X-Guzzle-Cache', $hashKey);
|
||||
|
||||
// If the cached data was found, then make the request into a
|
||||
// manually set request
|
||||
if ($cachedData) {
|
||||
if ($this->serialize) {
|
||||
$cachedData = unserialize($cachedData);
|
||||
}
|
||||
unset($this->cached[$key]);
|
||||
$response = new Response($cachedData['c'], $cachedData['h'], $cachedData['b']);
|
||||
$response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
|
||||
$response->setHeader('X-Guzzle-Cache', $hashKey);
|
||||
|
||||
// Validate that the response satisfies the request
|
||||
if ($this->canResponseSatisfyRequest($command, $response)) {
|
||||
$command->setResponse($response);
|
||||
}
|
||||
// Validate that the response satisfies the request
|
||||
if ($this->canResponseSatisfyRequest($subject, $response)) {
|
||||
$subject->setResponse($response);
|
||||
}
|
||||
}
|
||||
|
||||
} else if ($command->getResponse()->canCache()) {
|
||||
} else if ($event == 'request.sent' && $subject->getResponse()->canCache()) {
|
||||
|
||||
// The request is complete and now processing the response
|
||||
$response = $command->getResponse();
|
||||
$key = spl_object_hash($command);
|
||||
$response = $subject->getResponse();
|
||||
$key = spl_object_hash($subject);
|
||||
if (isset($this->cached[$key]) && $response->isSuccessful()) {
|
||||
if ($command->getParams()->get('cache.override_ttl')) {
|
||||
$lifetime = $command->getParams()->get('cache.override_ttl');
|
||||
if ($subject->getParams()->get('cache.override_ttl')) {
|
||||
$lifetime = $subject->getParams()->get('cache.override_ttl');
|
||||
$response->setHeader('X-Guzzle-Ttl', $lifetime);
|
||||
} else {
|
||||
$lifetime = $response->getMaxAge();
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
namespace Guzzle\Http\Plugin\Cookie;
|
||||
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Http\Message\Response;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
use Guzzle\Http\Plugin\AbstractPlugin;
|
||||
@ -287,23 +288,21 @@ class CookiePlugin extends AbstractPlugin
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param RequestInterface $command Request to process
|
||||
*/
|
||||
public function process($command)
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
// @codeCoverageIgnoreStart
|
||||
if (!$command instanceof RequestInterface) {
|
||||
if (!($subject instanceof RequestInterface)) {
|
||||
return;
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
if ($command->getState() == RequestInterface::STATE_TRANSFER) {
|
||||
if ($event == 'request.before_send') {
|
||||
// The request is being prepared
|
||||
$this->addCookies($command);
|
||||
} else if ($command->getState() == RequestInterface::STATE_COMPLETE) {
|
||||
$this->addCookies($subject);
|
||||
} else if ($event == 'request.sent') {
|
||||
// The response is being processed
|
||||
$this->extractCookies($command->getResponse());
|
||||
$this->extractCookies($subject->getResponse());
|
||||
}
|
||||
}
|
||||
}
|
@ -2,8 +2,8 @@
|
||||
|
||||
namespace Guzzle\Http\Plugin\ExponentialBackoff;
|
||||
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Subject\Observer;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
use Guzzle\Http\Pool\PoolInterface;
|
||||
|
||||
@ -41,23 +41,21 @@ class ExponentialBackoffObserver implements Observer
|
||||
/**
|
||||
* Check if the request object is ready to be retried
|
||||
*
|
||||
* @param SubjectMediator $subject Subject of the notification
|
||||
*
|
||||
* @return bool
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update(SubjectMediator $subject)
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
if ($subject->getSubject() instanceof PoolInterface && $subject->getState() == PoolInterface::POLLING) {
|
||||
if ($subject instanceof PoolInterface && $event == PoolInterface::POLLING) {
|
||||
|
||||
// If the duration of the delay has passed, retry the request using the pool
|
||||
if (time() >= $this->retryTime) {
|
||||
|
||||
// Remove the request from the pool and then add it back again
|
||||
$pool = $subject->getSubject();
|
||||
$pool->removeRequest($this->request);
|
||||
$pool->addRequest($this->request);
|
||||
$subject->removeRequest($this->request);
|
||||
$subject->addRequest($this->request);
|
||||
|
||||
// Remove this observer from the request
|
||||
$subject->detach($this);
|
||||
$subject->getEventManager()->detach($this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -3,9 +3,8 @@
|
||||
namespace Guzzle\Http\Plugin\ExponentialBackoff;
|
||||
|
||||
use \Closure;
|
||||
use Guzzle\Common\Subject\Subject;
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Subject\Observer;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
use Guzzle\Http\Plugin\AbstractPlugin;
|
||||
|
||||
@ -127,22 +126,22 @@ class ExponentialBackoffPlugin extends AbstractPlugin
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param RequestInterface $command Request to process
|
||||
*/
|
||||
public function process($command)
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
$key = spl_object_hash($command);
|
||||
/* @var $subject RequestInterface */
|
||||
|
||||
$key = spl_object_hash($subject);
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
// Make sure it's the right object and has been attached to the plugin
|
||||
if (!$command instanceof RequestInterface || !array_key_exists($key, $this->state)) {
|
||||
if (!($subject instanceof RequestInterface) || !array_key_exists($key, $this->state)) {
|
||||
return false;
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
// Make sure that the request needs to be retried
|
||||
if ($command->getState() == RequestInterface::STATE_COMPLETE && in_array($command->getResponse()->getStatusCode(), $this->failureCodes)) {
|
||||
if ($event == 'request.sent' && in_array($subject->getResponse()->getStatusCode(), $this->failureCodes)) {
|
||||
|
||||
// If this request has been retried too many times, then throw an exception
|
||||
if (++$this->state[$key] <= $this->maxRetries) {
|
||||
@ -150,17 +149,17 @@ class ExponentialBackoffPlugin extends AbstractPlugin
|
||||
$delay = (int) call_user_func($this->delayClosure, $this->state[$key]);
|
||||
|
||||
// Send the request again
|
||||
$command->setState(RequestInterface::STATE_NEW);
|
||||
$subject->setState(RequestInterface::STATE_NEW);
|
||||
|
||||
// Pooled requests need to be sent via curl multi
|
||||
if ($command->getParams()->get('pool')) {
|
||||
$command->getParams()->get('pool')
|
||||
->getSubjectMediator()
|
||||
->attach(new ExponentialBackoffObserver($command, $delay));
|
||||
if ($subject->getParams()->get('pool')) {
|
||||
$subject->getParams()->get('pool')
|
||||
->getEventManager()
|
||||
->attach(new ExponentialBackoffObserver($subject, $delay));
|
||||
} else {
|
||||
// Wait for a delay then retry the request
|
||||
sleep($delay);
|
||||
$command->send();
|
||||
$subject->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
namespace Guzzle\Http\Plugin\Log;
|
||||
|
||||
use Guzzle\Common\Log\Logger;
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\Message\EntityEnclosingRequestInterface;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
@ -24,6 +24,11 @@ class LogPlugin extends AbstractPlugin
|
||||
const WIRE_HEADERS = 1;
|
||||
const WIRE_FULL = 2;
|
||||
|
||||
/**
|
||||
* {@inheritdoc} Try to ensure this is called last
|
||||
*/
|
||||
protected $priority = -9999;
|
||||
|
||||
/**
|
||||
* @var Logger Logger object used to delegate log messages to adapters
|
||||
*/
|
||||
@ -74,14 +79,14 @@ class LogPlugin extends AbstractPlugin
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update(SubjectMediator $subject)
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
if ($subject->is('Guzzle\\Http\\Message\\RequestInterface')) {
|
||||
if ($subject instanceof RequestInterface) {
|
||||
|
||||
$request = $subject->getSubject();
|
||||
$request = $subject;
|
||||
/* @var $request EntityEnclosingRequest */
|
||||
|
||||
switch ($subject->getState()) {
|
||||
switch ($event) {
|
||||
|
||||
case 'request.before_send':
|
||||
|
||||
@ -110,27 +115,27 @@ class LogPlugin extends AbstractPlugin
|
||||
|
||||
case 'request.success':
|
||||
|
||||
$this->log($request, $subject->getContext());
|
||||
$this->log($request, $context);
|
||||
break;
|
||||
|
||||
case 'request.failure':
|
||||
|
||||
// Log curl exception messages
|
||||
$this->log($request, $subject->getContext()->getResponse(), $subject->getContext()->getMessage());
|
||||
$this->log($request, $context->getResponse(), $context->getMessage());
|
||||
|
||||
break;
|
||||
|
||||
case 'curl.callback.write':
|
||||
// Stream the response body as it is read using cURL
|
||||
if ($request->getParams()->get('response_wire')) {
|
||||
$request->getParams()->get('response_wire')->write($subject->getContext());
|
||||
$request->getParams()->get('response_wire')->write($context);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'curl.callback.read':
|
||||
// Stream the request body as it is read using cURL
|
||||
if ($request->getParams()->get('request_wire')) {
|
||||
$request->getParams()->get('request_wire')->write($subject->getContext());
|
||||
$request->getParams()->get('request_wire')->write($context);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Guzzle\Http\Pool;
|
||||
|
||||
use Guzzle\Common\Subject\AbstractSubject;
|
||||
use Guzzle\Common\Event\AbstractSubject;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
use Guzzle\Http\Message\RequestException;
|
||||
|
||||
@ -63,7 +63,7 @@ class Pool extends AbstractSubject implements PoolInterface
|
||||
curl_multi_add_handle($this->multiHandle, $request->getCurlHandle()->getHandle());
|
||||
}
|
||||
|
||||
$this->getSubjectMediator()->notify(self::ADD_REQUEST, $request, true);
|
||||
$this->getEventManager()->notify(self::ADD_REQUEST, $request);
|
||||
|
||||
// Associate the pool with the request
|
||||
$request->getParams()->set('pool', $this);
|
||||
@ -108,7 +108,7 @@ class Pool extends AbstractSubject implements PoolInterface
|
||||
return $req !== $request;
|
||||
}));
|
||||
|
||||
$this->getSubjectMediator()->notify(self::REMOVE_REQUEST, $request, true);
|
||||
$this->getEventManager()->notify(self::REMOVE_REQUEST, $request);
|
||||
|
||||
// Remove the pool's request association
|
||||
$request->getParams()->remove('pool');
|
||||
@ -136,7 +136,7 @@ class Pool extends AbstractSubject implements PoolInterface
|
||||
$this->multiHandle = curl_multi_init();
|
||||
|
||||
// Notify any observers of the reset event
|
||||
$this->getSubjectMediator()->notify(self::RESET);
|
||||
$this->getEventManager()->notify(self::RESET);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -156,7 +156,7 @@ class Pool extends AbstractSubject implements PoolInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->getSubjectMediator()->notify(self::BEFORE_SEND, $this->attached, true);
|
||||
$this->getEventManager()->notify(self::BEFORE_SEND, $this->attached);
|
||||
$this->state = self::STATE_SENDING;
|
||||
|
||||
foreach ($this->attached as $request) {
|
||||
@ -188,7 +188,7 @@ class Pool extends AbstractSubject implements PoolInterface
|
||||
try {
|
||||
$request->setState(RequestInterface::STATE_COMPLETE);
|
||||
} catch (RequestException $e) {
|
||||
$this->getSubjectMediator()->notify('exception', $e);
|
||||
$this->getEventManager()->notify('exception', $e);
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
curl_multi_remove_handle($this->multiHandle, $done['handle']);
|
||||
@ -204,7 +204,7 @@ class Pool extends AbstractSubject implements PoolInterface
|
||||
}
|
||||
}
|
||||
|
||||
$this->getSubjectMediator()->notify(self::POLLING, null);
|
||||
$this->getEventManager()->notify(self::POLLING);
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
if ($isRunning) {
|
||||
@ -218,7 +218,7 @@ class Pool extends AbstractSubject implements PoolInterface
|
||||
} while ($isRunning || !$finished);
|
||||
|
||||
$this->state = self::STATE_COMPLETE;
|
||||
$this->getSubjectMediator()->notify(self::COMPLETE, $this->attached, true);
|
||||
$this->getEventManager()->notify(self::COMPLETE, $this->attached);
|
||||
|
||||
// Throw any Request exceptions encountered during the transfer
|
||||
if (count($exceptions)) {
|
||||
|
@ -3,7 +3,7 @@
|
||||
namespace Guzzle\Http\Pool;
|
||||
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
use Guzzle\Common\Subject\Subject;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
|
||||
/**
|
||||
* Execute a pool of {@see RequestInterface} objects in
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
namespace Guzzle\Http;
|
||||
|
||||
use Guzzle\Common\Subject\AbstractSubject;
|
||||
use Guzzle\Common\Event\AbstractSubject;
|
||||
use Guzzle\Http\Message\Request;
|
||||
use Guzzle\Http\Message\Response;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
|
@ -6,15 +6,13 @@
|
||||
|
||||
namespace Guzzle\Http;
|
||||
|
||||
use Guzzle\Common\Subject\AbstractSubject;
|
||||
|
||||
/**
|
||||
* Parses and generates URLs based on URL parts. In favor of performance,
|
||||
* URL parts are not validated.
|
||||
*
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
class Url extends AbstractSubject
|
||||
class Url
|
||||
{
|
||||
/**#@+
|
||||
* @var string URL parts
|
||||
@ -221,7 +219,6 @@ class Url extends AbstractSubject
|
||||
public function setHost($host)
|
||||
{
|
||||
$this->host = $host;
|
||||
$this->getSubjectMediator()->notify('change_host', $this->host, true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -246,7 +243,6 @@ class Url extends AbstractSubject
|
||||
public function setScheme($scheme)
|
||||
{
|
||||
$this->scheme = $scheme;
|
||||
$this->getSubjectMediator()->notify('change_scheme', $this->scheme, true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -271,7 +267,6 @@ class Url extends AbstractSubject
|
||||
public function setPort($port)
|
||||
{
|
||||
$this->port = $port;
|
||||
$this->getSubjectMediator()->notify('change_port', $this->port, true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -309,7 +304,6 @@ class Url extends AbstractSubject
|
||||
if ($this->path != '*' && substr($this->path, 0, 1) != '/') {
|
||||
$this->path = '/' . $this->path;
|
||||
}
|
||||
$this->getSubjectMediator()->notify('change_path', $this->getPath(), true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -344,7 +338,6 @@ class Url extends AbstractSubject
|
||||
public function setPassword($password)
|
||||
{
|
||||
$this->password = $password;
|
||||
$this->getSubjectMediator()->notify('change_password', $this->password, true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -369,7 +362,6 @@ class Url extends AbstractSubject
|
||||
public function setUsername($username)
|
||||
{
|
||||
$this->username = $username;
|
||||
$this->getSubjectMediator()->notify('change_username', $this->username, true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -404,7 +396,6 @@ class Url extends AbstractSubject
|
||||
public function setQuery($query)
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->getSubjectMediator()->notify('change_query', $this->query, true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -429,7 +420,6 @@ class Url extends AbstractSubject
|
||||
public function setFragment($fragment)
|
||||
{
|
||||
$this->fragment = $fragment;
|
||||
$this->getSubjectMediator()->notify('change_fragment', $this->fragment);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ use Guzzle\Common\Inspector;
|
||||
use Guzzle\Common\Injector;
|
||||
use Guzzle\Common\Collection;
|
||||
use Guzzle\Common\Filter\Chain;
|
||||
use Guzzle\Common\Subject\AbstractSubject;
|
||||
use Guzzle\Common\Event\AbstractSubject;
|
||||
use Guzzle\Http\EntityBody;
|
||||
use Guzzle\Http\Message\RequestInterface;
|
||||
use Guzzle\Http\Message\RequestFactory;
|
||||
@ -206,7 +206,7 @@ class Client extends AbstractSubject
|
||||
}
|
||||
|
||||
$this->createRequestChain->process($request);
|
||||
$this->getSubjectMediator()->notify('request.create', $request, true);
|
||||
$this->getEventManager()->notify('request.create', $request);
|
||||
|
||||
return $request;
|
||||
}
|
||||
@ -221,7 +221,7 @@ class Client extends AbstractSubject
|
||||
public function setRequestFactory(RequestFactory $factory)
|
||||
{
|
||||
$this->requestFactory = $factory;
|
||||
$this->getSubjectMediator()->notify('request.factory.set', $factory, true);
|
||||
$this->getEventManager()->notify('request.factory.set', $factory);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -237,7 +237,7 @@ class Client extends AbstractSubject
|
||||
{
|
||||
if (!$this->hasPlugin(get_class($plugin))) {
|
||||
$this->plugins[self::getShortPluginName($plugin)] = $plugin;
|
||||
$this->getSubjectMediator()->attach($plugin);
|
||||
$this->getEventManager()->attach($plugin);
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -259,7 +259,7 @@ class Client extends AbstractSubject
|
||||
$plugin = self::getShortPluginName($plugin);
|
||||
}
|
||||
|
||||
$mediator = $this->getSubjectMediator();
|
||||
$mediator = $this->getEventManager();
|
||||
$that = $this;
|
||||
$c = __CLASS__;
|
||||
$this->plugins = array_filter($this->plugins, function($p) use ($plugin, $mediator, $that, $c) {
|
||||
@ -327,7 +327,7 @@ class Client extends AbstractSubject
|
||||
{
|
||||
$command = $this->commandFactory->buildCommand($name, $args);
|
||||
$command->setClient($this);
|
||||
$this->getSubjectMediator()->notify('command.create', $command);
|
||||
$this->getEventManager()->notify('command.create', $command);
|
||||
|
||||
return $command;
|
||||
}
|
||||
@ -350,9 +350,9 @@ class Client extends AbstractSubject
|
||||
if ($command instanceof CommandInterface) {
|
||||
|
||||
$command->prepare($this);
|
||||
$this->getSubjectMediator()->notify('command.before_send', $command);
|
||||
$this->getEventManager()->notify('command.before_send', $command);
|
||||
$command->getRequest()->send();
|
||||
$this->getSubjectMediator()->notify('command.after_send', $command, true);
|
||||
$this->getEventManager()->notify('command.after_send', $command);
|
||||
|
||||
return $command->getResult();
|
||||
|
||||
|
@ -109,7 +109,7 @@ class CommandSet implements \IteratorAggregate, \Countable
|
||||
// Prepare each request and send out Client notifications
|
||||
foreach ($parallel as $command) {
|
||||
$request = $command->prepare();
|
||||
$command->getClient()->getSubjectMediator()->notify('command.before_send', $command, true);
|
||||
$command->getClient()->getEventManager()->notify('command.before_send', $command);
|
||||
$this->pool->addRequest($request);
|
||||
}
|
||||
|
||||
@ -117,7 +117,7 @@ class CommandSet implements \IteratorAggregate, \Countable
|
||||
|
||||
// Notify any observers that the requests are complete
|
||||
foreach ($parallel as $command) {
|
||||
$command->getClient()->getSubjectMediator()->notify('command.after_send', $command, true);
|
||||
$command->getClient()->getEventManager()->notify('command.after_send', $command);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
namespace Guzzle\Service;
|
||||
|
||||
use Guzzle\Common\Subject\AbstractSubject;
|
||||
use Guzzle\Common\Event\AbstractSubject;
|
||||
use Guzzle\Service\Client;
|
||||
|
||||
/**
|
||||
@ -199,9 +199,9 @@ abstract class ResourceIterator extends AbstractSubject implements \Iterator, \C
|
||||
|| ++$this->currentIndex >= count($this->resourceList)
|
||||
&& $this->nextToken
|
||||
&& ($this->limit == -1 || $this->pos < $this->limit)) {
|
||||
$this->getSubjectMediator()->notify('before_send', $this->resourceList);
|
||||
$this->getEventManager()->notify('before_send', $this->resourceList);
|
||||
$this->sendRequest();
|
||||
$this->getSubjectMediator()->notify('after_send', $this->resourceList);
|
||||
$this->getEventManager()->notify('after_send', $this->resourceList);
|
||||
}
|
||||
|
||||
$this->current = (array_key_exists($this->currentIndex, $this->resourceList))
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
namespace Guzzle\Service;
|
||||
|
||||
use Guzzle\Common\Subject\AbstractSubject;
|
||||
use Guzzle\Common\Event\AbstractSubject;
|
||||
|
||||
/**
|
||||
* Apply a callback to the contents of a {@see ResourceIterator}
|
||||
@ -114,10 +114,10 @@ class ResourceIteratorApplyBatched extends AbstractSubject
|
||||
{
|
||||
$this->batches++;
|
||||
|
||||
$this->getSubjectMediator()->notify('before_batch', $batch);
|
||||
$this->getEventManager()->notify('before_batch', $batch);
|
||||
call_user_func_array($this->callback, array(
|
||||
$this->iterator, $batch
|
||||
));
|
||||
$this->getSubjectMediator()->notify('after_batch', $batch);
|
||||
$this->getEventManager()->notify('after_batch', $batch);
|
||||
}
|
||||
}
|
27
tests/Guzzle/Tests/Common/Event/AbstractSubjectTest.php
Normal file
27
tests/Guzzle/Tests/Common/Event/AbstractSubjectTest.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Guzzle PHP <http://www.guzzlephp.org>
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Tests\Common\Event;
|
||||
|
||||
use Guzzle\Tests\Common\Mock\MockSubject;
|
||||
use Guzzle\Common\Event\EventManager;
|
||||
|
||||
/**
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
class AbstractSubjectTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Guzzle\Common\Event\AbstractSubject::getEventManager
|
||||
*/
|
||||
public function testGetEventManager()
|
||||
{
|
||||
$subject = new MockSubject();
|
||||
$mediator = $subject->getEventManager();
|
||||
$this->assertInstanceOf('Guzzle\Common\Event\EventManager', $mediator);
|
||||
$this->assertEquals($mediator, $subject->getEventManager());
|
||||
}
|
||||
}
|
182
tests/Guzzle/Tests/Common/Event/EventManagerTest.php
Normal file
182
tests/Guzzle/Tests/Common/Event/EventManagerTest.php
Normal file
@ -0,0 +1,182 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Guzzle PHP <http://www.guzzlephp.org>
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Tests\Common\Event;
|
||||
|
||||
use Guzzle\Tests\Common\Mock\MockObserver;
|
||||
use Guzzle\Tests\Common\Mock\MockSubject;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Common\Event\EventManager;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
|
||||
/**
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
class EventManagerTest extends \Guzzle\Tests\GuzzleTestCase implements Observer
|
||||
{
|
||||
/**
|
||||
* @covers Guzzle\Common\Event\EventManager::attach
|
||||
* @covers Guzzle\Common\Event\EventManager::getAttached
|
||||
* @covers Guzzle\Common\Event\EventManager::__construct
|
||||
* @covers Guzzle\Common\Event\EventManager::getSubject
|
||||
*/
|
||||
public function testAttach()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$mock = new MockSubject();
|
||||
$subject = new EventManager($mock, array($observer));
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
$this->assertEquals($mock, $subject->getSubject());
|
||||
|
||||
// A single observer can only be attached once
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Event\EventManager::getAttached
|
||||
*/
|
||||
public function testGetAttachedByName()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new EventManager(new MockSubject());
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
$this->assertEquals(array($observer), $subject->getAttached('Guzzle\Tests\Common\Mock\MockObserver'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Event\EventManager::detach
|
||||
* @covers Guzzle\Common\Event\EventManager::getAttached
|
||||
* @depends testAttach
|
||||
*/
|
||||
public function testDetach()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new EventManager(new MockSubject());
|
||||
$this->assertEquals($observer, $subject->detach($observer));
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
$this->assertEquals($observer, $subject->detach($observer));
|
||||
$this->assertEquals(array(), $subject->getAttached());
|
||||
|
||||
// Now detach with more than one observer
|
||||
$subject->attach($this);
|
||||
$subject->attach($observer);
|
||||
$subject->detach($this);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Event\EventManager::detachAll
|
||||
* @depends testAttach
|
||||
*/
|
||||
public function testDetachAll()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new EventManager(new MockSubject());
|
||||
$this->assertEquals(array(), $subject->detachAll($observer));
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
$this->assertEquals(array($observer), $subject->detachAll($observer));
|
||||
$this->assertEquals(array(), $subject->getAttached());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Event\EventManager::hasObserver
|
||||
* @depends testAttach
|
||||
*/
|
||||
public function testHasObserver()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new EventManager(new MockSubject());
|
||||
$this->assertFalse($subject->hasObserver($observer));
|
||||
$this->assertFalse($subject->hasObserver('Guzzle\Tests\Common\Mock\MockObserver'));
|
||||
$subject->attach($observer);
|
||||
$this->assertTrue($subject->hasObserver($observer));
|
||||
$this->assertTrue($subject->hasObserver('Guzzle\Tests\Common\Mock\MockObserver'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Event\EventManager::notify
|
||||
* @covers Guzzle\Common\Event\EventManager::attach
|
||||
*/
|
||||
public function testNotify()
|
||||
{
|
||||
$priorities = array(10, 0, 999, 0, -10);
|
||||
|
||||
$observers = array(
|
||||
new MockObserver(),
|
||||
new MockObserver(),
|
||||
new MockObserver(),
|
||||
new MockObserver(),
|
||||
new MockObserver()
|
||||
);
|
||||
|
||||
$sub = new MockSubject();
|
||||
$subject = new EventManager($sub);
|
||||
|
||||
foreach ($observers as $i => $o) {
|
||||
$subject->attach($o, $priorities[$i]);
|
||||
}
|
||||
|
||||
// Make sure that the observers were properly sorted
|
||||
$attached = $subject->getAttached();
|
||||
$this->assertEquals(5, count($attached));
|
||||
$this->assertSame($attached[0], $observers[2]);
|
||||
$this->assertSame($attached[1], $observers[0]);
|
||||
$this->assertSame($attached[2], $observers[1]);
|
||||
$this->assertSame($attached[3], $observers[3]);
|
||||
$this->assertSame($attached[4], $observers[4]);
|
||||
|
||||
$this->assertEquals(array(true, true, true, true, true), $subject->notify('test', 'context'));
|
||||
|
||||
foreach ($observers as $o) {
|
||||
$this->assertEquals('test', $o->event);
|
||||
$this->assertEquals('context', $o->context);
|
||||
$this->assertEquals(1, $o->notified);
|
||||
$this->assertEquals($sub, $o->subject);
|
||||
}
|
||||
|
||||
// Make sure the it will update them again
|
||||
$this->assertEquals(array(true, true, true, true, true), $subject->notify('test'));
|
||||
foreach ($observers as $o) {
|
||||
$this->assertEquals('test', $o->event);
|
||||
$this->assertEquals(null, $o->context);
|
||||
$this->assertEquals(2, $o->notified);
|
||||
$this->assertEquals($sub, $o->subject);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Event\EventManager::notify
|
||||
*/
|
||||
public function testNotifyUntil()
|
||||
{
|
||||
$sub = new MockSubject();
|
||||
$subject = new EventManager($sub);
|
||||
|
||||
$observer1 = new MockObserver();
|
||||
$observer2 = new MockObserver();
|
||||
$observer3 = new MockObserver();
|
||||
$observer4 = new MockObserver();
|
||||
|
||||
$subject->attach($observer1);
|
||||
$subject->attach($observer2);
|
||||
$subject->attach($observer3);
|
||||
$subject->attach($observer4);
|
||||
|
||||
$this->assertEquals(array(true), $subject->notify('test', null, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
@ -6,8 +6,9 @@
|
||||
|
||||
namespace Guzzle\Tests\Common\Mock;
|
||||
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Subject\Observer;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Common\Event\EventManager;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
|
||||
/**
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
@ -17,18 +18,21 @@ class MockObserver implements Observer
|
||||
public $notified = 0;
|
||||
public $subject;
|
||||
public $context;
|
||||
public $state;
|
||||
public $event;
|
||||
public $log = array();
|
||||
public $logByState = array();
|
||||
public $logByEvent = array();
|
||||
|
||||
public function update(SubjectMediator $subject)
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
$this->notified++;
|
||||
$this->subject = $subject;
|
||||
$this->context = $subject->getContext();
|
||||
$this->state = $subject->getState();
|
||||
$this->log[] = array($subject->getState(), $subject->getContext());
|
||||
$this->logByState[$subject->getState()] = $subject->getContext();
|
||||
$this->context = $context;
|
||||
$this->event = $event;
|
||||
$this->log[] = array($event, $context);
|
||||
$this->logByEvent[$event] = $context;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -9,6 +9,6 @@ namespace Guzzle\Tests\Common\Mock;
|
||||
/**
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
class MockSubject extends \Guzzle\Common\Subject\AbstractSubject
|
||||
class MockSubject extends \Guzzle\Common\Event\AbstractSubject
|
||||
{
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Guzzle PHP <http://www.guzzlephp.org>
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Tests\Common;
|
||||
|
||||
use \Guzzle\Common\Subject\SubjectMediator;
|
||||
|
||||
/**
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
class AbstractSubjectTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Guzzle\Common\Subject\AbstractSubject::getSubjectMediator
|
||||
*/
|
||||
public function testGetSubjectMediator()
|
||||
{
|
||||
$subject = new Mock\MockSubject();
|
||||
$mediator = $subject->getSubjectMediator();
|
||||
$this->assertInstanceOf('Guzzle\Common\Subject\SubjectMediator', $mediator);
|
||||
$this->assertEquals($mediator, $subject->getSubjectMediator());
|
||||
}
|
||||
}
|
@ -1,170 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Guzzle PHP <http://www.guzzlephp.org>
|
||||
* @license See the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Guzzle\Tests\Common;
|
||||
|
||||
use \Guzzle\Tests\Common\Mock\MockObserver,
|
||||
\Guzzle\Tests\Common\Mock\MockSubject,
|
||||
\Guzzle\Common\Subject\Subject,
|
||||
\Guzzle\Common\Subject\SubjectMediator,
|
||||
\Guzzle\Common\Subject\Observer;
|
||||
|
||||
/**
|
||||
* @author Michael Dowling <michael@guzzlephp.org>
|
||||
*/
|
||||
class SubjectMediatorTest extends \Guzzle\Tests\GuzzleTestCase implements Observer
|
||||
{
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::attach
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::getAttached
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::__construct
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::getSubject
|
||||
*/
|
||||
public function testAttach()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$mock = new MockSubject();
|
||||
$subject = new SubjectMediator($mock, array($observer));
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
$this->assertEquals($mock, $subject->getSubject());
|
||||
|
||||
// A single observer can only be attached once
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::getAttached
|
||||
*/
|
||||
public function testGetAttachedByName()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new SubjectMediator(new MockSubject());
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
$this->assertEquals(array($observer), $subject->getAttached('Guzzle\Tests\Common\Mock\MockObserver'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::detach
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::getAttached
|
||||
* @depends testAttach
|
||||
*/
|
||||
public function testDetach()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new SubjectMediator(new MockSubject());
|
||||
$this->assertEquals($observer, $subject->detach($observer));
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
$this->assertEquals($observer, $subject->detach($observer));
|
||||
$this->assertEquals(array(), $subject->getAttached());
|
||||
|
||||
// Now detach with more than one observer
|
||||
$subject->attach($this);
|
||||
$subject->attach($observer);
|
||||
$subject->detach($this);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::detachAll
|
||||
* @depends testAttach
|
||||
*/
|
||||
public function testDetachAll()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new SubjectMediator(new MockSubject());
|
||||
$this->assertEquals(array(), $subject->detachAll($observer));
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array($observer), $subject->getAttached());
|
||||
$this->assertEquals(array($observer), $subject->detachAll($observer));
|
||||
$this->assertEquals(array(), $subject->getAttached());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::getState
|
||||
*/
|
||||
public function testGetState()
|
||||
{
|
||||
$subject = new SubjectMediator(new MockSubject());
|
||||
$this->assertNull($subject->getState());
|
||||
$subject->notify('new_state');
|
||||
$this->assertEquals('new_state', $subject->getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::getContext
|
||||
*/
|
||||
public function testGetContext()
|
||||
{
|
||||
$subject = new SubjectMediator(new MockSubject());
|
||||
$this->assertNull($subject->getContext());
|
||||
$subject->notify('new_state', 'new_context');
|
||||
$this->assertEquals('new_context', $subject->getContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::hasObserver
|
||||
* @depends testAttach
|
||||
*/
|
||||
public function testHasObserver()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new SubjectMediator(new MockSubject());
|
||||
$this->assertFalse($subject->hasObserver($observer));
|
||||
$this->assertFalse($subject->hasObserver('Guzzle\Tests\Common\Mock\MockObserver'));
|
||||
$subject->attach($observer);
|
||||
$this->assertTrue($subject->hasObserver($observer));
|
||||
$this->assertTrue($subject->hasObserver('Guzzle\Tests\Common\Mock\MockObserver'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::notify
|
||||
*/
|
||||
public function testNotify()
|
||||
{
|
||||
$observer = new MockObserver();
|
||||
$subject = new SubjectMediator(new MockSubject());
|
||||
$subject->attach($observer);
|
||||
$this->assertEquals(array(true), $subject->notify('test', 'context', false));
|
||||
$this->assertEquals('test', $subject->getState());
|
||||
$this->assertEquals('context', $subject->getContext());
|
||||
$this->assertEquals(1, $observer->notified);
|
||||
$this->assertEquals($subject, $observer->subject);
|
||||
|
||||
$this->assertEquals(array(true), $subject->notify(Subject::STATE_UNCHANGED, Subject::STATE_UNCHANGED, false));
|
||||
$this->assertEquals(2, $observer->notified);
|
||||
$this->assertEquals('test', $subject->getState());
|
||||
$this->assertEquals('context', $subject->getContext());
|
||||
|
||||
$this->assertEquals(array(true), $subject->notify(Subject::STATE_UNCHANGED, Subject::STATE_UNCHANGED, true));
|
||||
$this->assertEquals(3, $observer->notified);
|
||||
$this->assertEquals('test', $subject->getState());
|
||||
$this->assertEquals(null, $subject->getContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Common\Subject\SubjectMediator::is
|
||||
*/
|
||||
public function testIs()
|
||||
{
|
||||
$mock = new MockSubject();
|
||||
$subject = new SubjectMediator($mock);
|
||||
$this->assertTrue($subject->is('Guzzle\Tests\Common\Mock\MockSubject'));
|
||||
$this->assertTrue($subject->is($mock));
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements Observer
|
||||
*
|
||||
* @param SubjectMediator $subject
|
||||
*/
|
||||
public function update(SubjectMediator $subject)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
@ -70,6 +70,7 @@ class CookieTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\QueryString
|
||||
* @covers Guzzle\Http\Cookie
|
||||
*/
|
||||
public function testAggregatesMultipleCookieValues()
|
||||
{
|
||||
|
@ -405,18 +405,18 @@ class CurlFactoryTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$o = new MockObserver();
|
||||
$request = RequestFactory::getInstance()->newRequest('PUT', $this->getServer()->getUrl());
|
||||
$request->setBody(EntityBody::factory('test'));
|
||||
$request->getSubjectMediator()->attach($o);
|
||||
$request->getEventManager()->attach($o);
|
||||
$h1 = $request->getCurlHandle();
|
||||
$request->send();
|
||||
|
||||
// Make sure that the events were dispatched
|
||||
$this->assertArrayHasKey('curl.callback.read', $o->logByState);
|
||||
$this->assertArrayHasKey('curl.callback.write', $o->logByState);
|
||||
$this->assertArrayHasKey('curl.callback.progress', $o->logByState);
|
||||
$this->assertArrayHasKey('curl.callback.read', $o->logByEvent);
|
||||
$this->assertArrayHasKey('curl.callback.write', $o->logByEvent);
|
||||
$this->assertArrayHasKey('curl.callback.progress', $o->logByEvent);
|
||||
|
||||
// Make sure that the data was sent through the event
|
||||
$this->assertEquals('test', $o->logByState['curl.callback.read']);
|
||||
$this->assertEquals('hi', $o->logByState['curl.callback.write']);
|
||||
$this->assertEquals('test', $o->logByEvent['curl.callback.read']);
|
||||
$this->assertEquals('hi', $o->logByEvent['curl.callback.write']);
|
||||
|
||||
// Ensure that the request was received exactly as intended
|
||||
$r = $this->getServer()->getReceivedRequests(true);
|
||||
|
@ -66,7 +66,7 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Message\EntityEnclosingRequest::process
|
||||
* @covers Guzzle\Http\Message\EntityEnclosingRequest::update
|
||||
*/
|
||||
public function testRequestDeterminesContentBeforeSending()
|
||||
{
|
||||
@ -82,7 +82,7 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$request = $this->factory->newRequest('PUT', 'http://www.test.com/');
|
||||
$request->setBody(EntityBody::factory('test'));
|
||||
$this->assertNull($request->getHeader('Content-Length'));
|
||||
$request->process($request);
|
||||
$request->getEventManager()->notify('request.prepare_entity_body');
|
||||
$this->assertEquals(4, $request->getHeader('Content-Length'));
|
||||
unset($request);
|
||||
|
||||
@ -90,14 +90,14 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$request = $this->factory->newRequest('PUT', 'http://www.test.com/');
|
||||
$request->setBody(EntityBody::factory('test'))
|
||||
->setHeader('Transfer-Encoding', 'chunked');
|
||||
$request->process($request);
|
||||
$request->getEventManager()->notify('request.prepare_entity_body');
|
||||
$this->assertNull($request->getHeader('Content-Length'));
|
||||
|
||||
// Tests using a Transfer-Encoding chunked entity body with undeterminable size
|
||||
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nData");
|
||||
$request = $this->factory->newRequest('PUT', 'http://www.test.com/');
|
||||
$request->setBody(EntityBody::factory(fopen($this->getServer()->getUrl(), 'r')));
|
||||
$request->process($request);
|
||||
$request->getEventManager()->notify('request.prepare_entity_body');
|
||||
$this->assertNull($request->getHeader('Content-Length'));
|
||||
$this->assertEquals('chunked', $request->getHeader('Transfer-Encoding'));
|
||||
|
||||
@ -107,7 +107,7 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$request->setBody(EntityBody::factory(fopen($this->getServer()->getUrl(), 'r')));
|
||||
$request->setProtocolVersion('1.0');
|
||||
try {
|
||||
$request->process($request);
|
||||
$request->getEventManager()->notify('request.prepare_entity_body');
|
||||
$this->fail('Expected exception due to 1.0 and no Content-Length');
|
||||
} catch (RequestException $e) {
|
||||
}
|
||||
@ -182,7 +182,7 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Message\EntityEnclosingRequest::process
|
||||
* @covers Guzzle\Http\Message\EntityEnclosingRequest::update
|
||||
*/
|
||||
public function testProcessMethodAddsPostBodyAndEntityBodyHeaders()
|
||||
{
|
||||
@ -194,14 +194,14 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
|
||||
$request = $this->factory->newRequest('POST', 'http://www.guzzle-project.com/');
|
||||
$request->getPostFields()->set('a', 'b');
|
||||
$request->process($request);
|
||||
$request->getEventManager()->notify('request.prepare_entity_body');
|
||||
$this->assertEquals('application/x-www-form-urlencoded', $request->getHeader('Content-Type'));
|
||||
$this->assertEquals('a=b', $request->getCurlOptions()->get(CURLOPT_POSTFIELDS));
|
||||
unset($request);
|
||||
|
||||
$request = $this->factory->newRequest('POST', 'http://www.guzzle-project.com/');
|
||||
$request->addPostFiles(array('file' => __FILE__));
|
||||
$request->process($request);
|
||||
$request->getEventManager()->notify('request.prepare_entity_body');
|
||||
$this->assertEquals('multipart/form-data', $request->getHeader('Content-Type'));
|
||||
$this->assertEquals(array('file' => '@' . __FILE__), $request->getCurlOptions()->get(CURLOPT_POSTFIELDS));
|
||||
}
|
||||
|
@ -103,13 +103,13 @@ class RequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Message\Request::getSubjectMediator
|
||||
* @covers Guzzle\Http\Message\Request::getEventManager
|
||||
*/
|
||||
public function testGetSubjectMediator()
|
||||
public function testGetEventManager()
|
||||
{
|
||||
$mediator = $this->request->getSubjectMediator();
|
||||
$this->assertInstanceOf('Guzzle\\Common\\Subject\\SubjectMediator', $mediator);
|
||||
$this->assertEquals($mediator, $this->request->getSubjectMediator());
|
||||
$mediator = $this->request->getEventManager();
|
||||
$this->assertInstanceOf('Guzzle\\Common\\Event\\EventManager', $mediator);
|
||||
$this->assertEquals($mediator, $this->request->getEventManager());
|
||||
$this->assertEquals($this->request, $mediator->getSubject());
|
||||
}
|
||||
|
||||
@ -199,26 +199,6 @@ class RequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Message\Request::getPrepareChain
|
||||
*/
|
||||
public function testStoresPrepareChain()
|
||||
{
|
||||
$chain = $this->request->getPrepareChain();
|
||||
$this->assertInstanceOf('Guzzle\\Common\\Filter\\Chain', $chain);
|
||||
$this->assertEquals($chain, $this->request->getPrepareChain());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Message\Request::getProcessChain
|
||||
*/
|
||||
public function testStoresProcessChain()
|
||||
{
|
||||
$chain = $this->request->getProcessChain();
|
||||
$this->assertInstanceOf('Guzzle\\Common\\Filter\\Chain', $chain);
|
||||
$this->assertEquals($chain, $this->request->getProcessChain());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Message\Request::getQuery
|
||||
*/
|
||||
@ -582,10 +562,8 @@ class RequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
|
||||
$this->assertEquals(RequestInterface::STATE_NEW, $r->getState());
|
||||
$this->assertNotSame($r->getQuery(), $this->request->getQuery());
|
||||
$this->assertNotSame($r->getPrepareChain(), $this->request->getPrepareChain());
|
||||
$this->assertNotSame($r->getProcessChain(), $this->request->getProcessChain());
|
||||
$this->assertNotSame($r->getCurlOptions(), $this->request->getCurlOptions());
|
||||
$this->assertNotSame($r->getSubjectMediator(), $this->request->getSubjectMediator());
|
||||
$this->assertNotSame($r->getEventManager(), $this->request->getEventManager());
|
||||
$this->assertNotSame($r->getHeaders(), $this->request->getHeaders());
|
||||
$this->assertNotSame($r->getParams(), $this->request->getParams());
|
||||
$this->assertNull($r->getParams()->get('queued_response'));
|
||||
|
@ -63,7 +63,7 @@ class CachePluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Plugin\AbstractPlugin::attach
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::process
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::update
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::attach
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::saveCache
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::getCacheKey
|
||||
@ -110,7 +110,7 @@ class CachePluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::process
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::update
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::saveCache
|
||||
*/
|
||||
public function testSkipsNonReadableResponseBodies()
|
||||
@ -209,7 +209,7 @@ class CachePluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::process
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::update
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::saveCache
|
||||
*/
|
||||
public function testRequestsCanOverrideTtlUsingCacheParam()
|
||||
@ -231,7 +231,7 @@ class CachePluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::canResponseSatisfyRequest
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::process
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::update
|
||||
* @covers Guzzle\Http\Plugin\Cache\CachePlugin::saveCache
|
||||
*/
|
||||
public function testRequestsCanAcceptStaleResponses()
|
||||
|
@ -84,7 +84,7 @@ class ExponentialBackoffPluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Plugin\ExponentialBackoff\ExponentialBackoffPlugin::process
|
||||
* @covers Guzzle\Http\Plugin\ExponentialBackoff\ExponentialBackoffPlugin::update
|
||||
* @covers Guzzle\Http\Message\Request
|
||||
* @expectedException Guzzle\Http\Message\BadResponseException
|
||||
*/
|
||||
@ -107,7 +107,7 @@ class ExponentialBackoffPluginTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Plugin\ExponentialBackoff\ExponentialBackoffPlugin::process
|
||||
* @covers Guzzle\Http\Plugin\ExponentialBackoff\ExponentialBackoffPlugin::update
|
||||
* @covers Guzzle\Http\Pool\Pool
|
||||
* @covers Guzzle\Http\Plugin\ExponentialBackoff\ExponentialBackoffObserver
|
||||
*/
|
||||
|
@ -7,8 +7,8 @@
|
||||
namespace Guzzle\Tests\Http\Pool;
|
||||
|
||||
use Guzzle\Common\Collection;
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Subject\Observer;
|
||||
use Guzzle\Common\Event\Subject;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
use Guzzle\Http\Server;
|
||||
use Guzzle\Http\Message\Response;
|
||||
use Guzzle\Http\Message\Request;
|
||||
@ -37,7 +37,7 @@ class PoolTest extends \Guzzle\Tests\GuzzleTestCase implements Observer
|
||||
{
|
||||
parent::setUp();
|
||||
$this->pool = new MockPool();
|
||||
$this->pool->getSubjectMediator()->attach($this);
|
||||
$this->pool->getEventManager()->attach($this);
|
||||
$this->updates = new Collection();
|
||||
}
|
||||
|
||||
@ -97,8 +97,8 @@ class PoolTest extends \Guzzle\Tests\GuzzleTestCase implements Observer
|
||||
$this->assertTrue($this->updates->hasKey(Pool::ADD_REQUEST));
|
||||
$this->assertTrue($this->updates->hasKey(Pool::POLLING));
|
||||
$this->assertTrue($this->updates->hasKey(Pool::COMPLETE));
|
||||
$this->assertEquals(array('idle', $request), $this->updates->get(Pool::ADD_REQUEST));
|
||||
$this->assertEquals(array('sending', null), $this->updates->get(Pool::POLLING));
|
||||
$this->assertEquals(array('add_request', $request), $this->updates->get(Pool::ADD_REQUEST));
|
||||
$this->assertEquals(array('polling', null), $this->updates->get(Pool::POLLING));
|
||||
$this->assertEquals(array('complete', array($request)), $this->updates->get(Pool::COMPLETE));
|
||||
$this->assertEquals('complete', $this->pool->getState());
|
||||
$this->assertEquals('Body', $request->getResponse()->getBody()->__toString());
|
||||
@ -131,9 +131,7 @@ class PoolTest extends \Guzzle\Tests\GuzzleTestCase implements Observer
|
||||
$this->assertEquals('idle', $this->pool->getState());
|
||||
|
||||
// Make sure the notification came through
|
||||
$this->assertEquals(array(
|
||||
'idle', null
|
||||
), $this->updates->get('reset'));
|
||||
$this->assertEquals(array('reset', null), $this->updates->get('reset'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -239,11 +237,10 @@ class PoolTest extends \Guzzle\Tests\GuzzleTestCase implements Observer
|
||||
* Listens for updates to the pool object and logs them in a
|
||||
* Guzzle\Common\Collection object.
|
||||
*
|
||||
* @param SubjectMediator $subject The subject sending the update
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update(SubjectMediator $subject)
|
||||
public function update(Subject $subject, $event, $context = null)
|
||||
{
|
||||
$colleague = $subject->getSubject();
|
||||
$this->updates->add($subject->getState(), array($colleague->getState(), $subject->getContext()));
|
||||
$this->updates->add($event, array($event, $context));
|
||||
}
|
||||
}
|
@ -6,8 +6,8 @@
|
||||
|
||||
namespace Guzzle\Tests\Http;
|
||||
|
||||
use Guzzle\Common\Subject\SubjectMediator;
|
||||
use Guzzle\Common\Subject\Observer;
|
||||
use Guzzle\Common\Event\EventManager;
|
||||
use Guzzle\Common\Event\Observer;
|
||||
use Guzzle\Http\Server;
|
||||
use Guzzle\Http\Message\BadResponseException;
|
||||
use Guzzle\Http\Message\Response;
|
||||
|
@ -197,9 +197,7 @@ class ClientTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
|
||||
$request = $client->getRequest('GET');
|
||||
// Make sure the the plugin was attached to the new request
|
||||
$this->assertTrue($request->getPrepareChain()->hasFilter($logPlugin));
|
||||
$this->assertTrue($request->getProcessChain()->hasFilter($logPlugin));
|
||||
$this->assertTrue($request->getSubjectMediator()->hasObserver($logPlugin));
|
||||
$this->assertTrue($request->getEventManager()->hasObserver($logPlugin));
|
||||
|
||||
// Make sure that the log plugin actually logged the request and response
|
||||
ob_start();
|
||||
@ -213,9 +211,7 @@ class ClientTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
// Ensure that it was actually detached
|
||||
$this->assertFalse($client->hasPlugin($logPlugin));
|
||||
$request = $client->getRequest('GET');
|
||||
$this->assertFalse($request->getPrepareChain()->hasFilter($logPlugin));
|
||||
$this->assertFalse($request->getProcessChain()->hasFilter($logPlugin));
|
||||
$this->assertFalse($request->getSubjectMediator()->hasObserver($logPlugin));
|
||||
$this->assertFalse($request->getEventManager()->hasObserver($logPlugin));
|
||||
|
||||
// Try that again, but reference it by name
|
||||
$client->attachPlugin($logPlugin);
|
||||
|
@ -106,7 +106,7 @@ class CommandSetTest extends AbstractCommandTest
|
||||
$client = $this->getClient();
|
||||
|
||||
// Create a mock observer
|
||||
$observer = $this->getMockBuilder('Guzzle\\Common\\Subject\\Observer')->setMethods(array('update'))->getMock();
|
||||
$observer = $this->getMockBuilder('Guzzle\\Common\\Event\\Observer')->setMethods(array('update'))->getMock();
|
||||
// The observer should be called 4 times, 3 times for each command (once to retrieve from client, one before send, one after send)
|
||||
$observer->expects($this->exactly(6))
|
||||
->method('update');
|
||||
@ -131,7 +131,7 @@ class CommandSetTest extends AbstractCommandTest
|
||||
|
||||
$commandSet = new CommandSet(array($command1, $command2));
|
||||
|
||||
$client->getSubjectMediator()->attach($observer);
|
||||
$client->getEventManager()->attach($observer);
|
||||
$commandSet->execute();
|
||||
|
||||
$this->assertTrue($command1->isExecuted());
|
||||
|
Loading…
x
Reference in New Issue
Block a user