Open Journal Systems  3.3.0
HistoryPlugin.php
1 <?php
2 
4 
9 
13 class HistoryPlugin implements EventSubscriberInterface, \IteratorAggregate, \Countable
14 {
16  protected $limit = 10;
17 
19  protected $transactions = array();
20 
21  public static function getSubscribedEvents()
22  {
23  return array('request.sent' => array('onRequestSent', 9999));
24  }
25 
31  public function __toString()
32  {
33  $lines = array();
34  foreach ($this->transactions as $entry) {
35  $response = isset($entry['response']) ? $entry['response'] : '';
36  $lines[] = '> ' . trim($entry['request']) . "\n\n< " . trim($response) . "\n";
37  }
38 
39  return implode("\n", $lines);
40  }
41 
50  public function add(RequestInterface $request, Response $response = null)
51  {
52  if (!$response && $request->getResponse()) {
53  $response = $request->getResponse();
54  }
55 
56  $this->transactions[] = array('request' => $request, 'response' => $response);
57  if (count($this->transactions) > $this->getlimit()) {
58  array_shift($this->transactions);
59  }
60 
61  return $this;
62  }
63 
71  public function setLimit($limit)
72  {
73  $this->limit = (int) $limit;
74 
75  return $this;
76  }
77 
83  public function getLimit()
84  {
85  return $this->limit;
86  }
87 
94  public function getAll()
95  {
96  return $this->transactions;
97  }
98 
104  public function getIterator()
105  {
106  // Return an iterator just like the old iteration of the HistoryPlugin for BC compatibility (use getAll())
107  return new \ArrayIterator(array_map(function ($entry) {
108  $entry['request']->getParams()->set('actual_response', $entry['response']);
109  return $entry['request'];
111  }
112 
118  public function count()
119  {
120  return count($this->transactions);
121  }
122 
128  public function getLastRequest()
129  {
130  $last = end($this->transactions);
131 
132  return $last['request'];
133  }
134 
140  public function getLastResponse()
141  {
142  $last = end($this->transactions);
143 
144  return isset($last['response']) ? $last['response'] : null;
145  }
146 
152  public function clear()
153  {
154  $this->transactions = array();
155 
156  return $this;
157  }
158 
159  public function onRequestSent(Event $event)
160  {
161  $this->add($event['request'], $event['response']);
162  }
163 }
Guzzle\Http\Message\RequestInterface\getResponse
getResponse()
Guzzle\Plugin\History\HistoryPlugin\setLimit
setLimit($limit)
Definition: HistoryPlugin.php:77
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Plugin\History\HistoryPlugin
Definition: HistoryPlugin.php:13
Symfony\Component\EventDispatcher\EventSubscriberInterface
Definition: lib/vendor/symfony/event-dispatcher/EventSubscriberInterface.php:25
Guzzle\Plugin\History\HistoryPlugin\getLastResponse
getLastResponse()
Definition: HistoryPlugin.php:146
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Plugin\History\HistoryPlugin\getAll
getAll()
Definition: HistoryPlugin.php:100
Guzzle\Plugin\History\HistoryPlugin\getLimit
getLimit()
Definition: HistoryPlugin.php:89
Guzzle\Plugin\History\HistoryPlugin\count
count()
Definition: HistoryPlugin.php:124
Guzzle\Plugin\History\HistoryPlugin\$limit
$limit
Definition: HistoryPlugin.php:19
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Plugin\History\HistoryPlugin\__toString
__toString()
Definition: HistoryPlugin.php:37
Guzzle\Plugin\History\HistoryPlugin\onRequestSent
onRequestSent(Event $event)
Definition: HistoryPlugin.php:165
Guzzle\Plugin\History\HistoryPlugin\clear
clear()
Definition: HistoryPlugin.php:158
Guzzle\Plugin\History\HistoryPlugin\getLastRequest
getLastRequest()
Definition: HistoryPlugin.php:134
Guzzle\Plugin\History\HistoryPlugin\$transactions
$transactions
Definition: HistoryPlugin.php:25
Guzzle\Plugin\History\HistoryPlugin\getIterator
getIterator()
Definition: HistoryPlugin.php:110
Guzzle\Plugin\History\HistoryPlugin\getSubscribedEvents
static getSubscribedEvents()
Definition: HistoryPlugin.php:27
Guzzle\Plugin\History
Definition: HistoryPlugin.php:3
Guzzle\Plugin\History\HistoryPlugin\add
add(RequestInterface $request, Response $response=null)
Definition: HistoryPlugin.php:56