1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164:
<?php
namespace Guzzle\Plugin\History;
use Guzzle\Common\Event;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Maintains a list of requests and responses sent using a request or client
*/
class HistoryPlugin implements EventSubscriberInterface, \IteratorAggregate, \Countable
{
/** @var int The maximum number of requests to maintain in the history */
protected $limit = 10;
/** @var array Requests and responses that have passed through the plugin */
protected $transactions = array();
public static function getSubscribedEvents()
{
return array('request.sent' => array('onRequestSent', 9999));
}
/**
* Convert to a string that contains all request and response headers
*
* @return string
*/
public function __toString()
{
$lines = array();
foreach ($this->transactions as $entry) {
$response = isset($entry['response']) ? $entry['response'] : '';
$lines[] = '> ' . trim($entry['request']) . "\n\n< " . trim($response) . "\n";
}
return implode("\n", $lines);
}
/**
* Add a request to the history
*
* @param RequestInterface $request Request to add
* @param Response $response Response of the request
*
* @return HistoryPlugin
*/
public function add(RequestInterface $request, Response $response = null)
{
if (!$response && $request->getResponse()) {
$response = $request->getResponse();
}
$this->transactions[] = array('request' => $request, 'response' => $response);
if (count($this->transactions) > $this->getlimit()) {
array_shift($this->transactions);
}
return $this;
}
/**
* Set the max number of requests to store
*
* @param int $limit Limit
*
* @return HistoryPlugin
*/
public function setLimit($limit)
{
$this->limit = (int) $limit;
return $this;
}
/**
* Get the request limit
*
* @return int
*/
public function getLimit()
{
return $this->limit;
}
/**
* Get all of the raw transactions in the form of an array of associative arrays containing
* 'request' and 'response' keys.
*
* @return array
*/
public function getAll()
{
return $this->transactions;
}
/**
* Get the requests in the history
*
* @return \ArrayIterator
*/
public function getIterator()
{
// Return an iterator just like the old iteration of the HistoryPlugin for BC compatibility (use getAll())
return new \ArrayIterator(array_map(function ($entry) {
$entry['request']->getParams()->set('actual_response', $entry['response']);
return $entry['request'];
}, $this->transactions));
}
/**
* Get the number of requests in the history
*
* @return int
*/
public function count()
{
return count($this->transactions);
}
/**
* Get the last request sent
*
* @return RequestInterface
*/
public function getLastRequest()
{
$last = end($this->transactions);
return $last['request'];
}
/**
* Get the last response in the history
*
* @return Response|null
*/
public function getLastResponse()
{
$last = end($this->transactions);
return isset($last['response']) ? $last['response'] : null;
}
/**
* Clears the history
*
* @return HistoryPlugin
*/
public function clear()
{
$this->transactions = array();
return $this;
}
public function onRequestSent(Event $event)
{
$this->add($event['request'], $event['response']);
}
}