scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Subscriber;
4  
5 use GuzzleHttp\Event\CompleteEvent;
6 use GuzzleHttp\Event\ErrorEvent;
7 use GuzzleHttp\Event\RequestEvents;
8 use GuzzleHttp\Event\SubscriberInterface;
9 use GuzzleHttp\Message\RequestInterface;
10 use GuzzleHttp\Message\ResponseInterface;
11  
12 /**
13 * Maintains a list of requests and responses sent using a request or client
14 */
15 class History implements SubscriberInterface, \IteratorAggregate, \Countable
16 {
17 /** @var int The maximum number of requests to maintain in the history */
18 private $limit;
19  
20 /** @var array Requests and responses that have passed through the plugin */
21 private $transactions = [];
22  
23 public function __construct($limit = 10)
24 {
25 $this->limit = $limit;
26 }
27  
28 public function getEvents()
29 {
30 return [
31 'complete' => ['onComplete', RequestEvents::EARLY],
32 'error' => ['onError', RequestEvents::EARLY],
33 ];
34 }
35  
36 /**
37 * Convert to a string that contains all request and response headers
38 *
39 * @return string
40 */
41 public function __toString()
42 {
43 $lines = array();
44 foreach ($this->transactions as $entry) {
45 $response = isset($entry['response']) ? $entry['response'] : '';
46 $lines[] = '> ' . trim($entry['request']) . "\n\n< " . trim($response) . "\n";
47 }
48  
49 return implode("\n", $lines);
50 }
51  
52 public function onComplete(CompleteEvent $event)
53 {
54 $this->add($event->getRequest(), $event->getResponse());
55 }
56  
57 public function onError(ErrorEvent $event)
58 {
59 // Only track when no response is present, meaning this didn't ever
60 // emit a complete event
61 if (!$event->getResponse()) {
62 $this->add($event->getRequest());
63 }
64 }
65  
66 /**
67 * Returns an Iterator that yields associative array values where each
68 * associative array contains a 'request' and 'response' key.
69 *
70 * @return \Iterator
71 */
72 public function getIterator()
73 {
74 return new \ArrayIterator($this->transactions);
75 }
76  
77 /**
78 * Get all of the requests sent through the plugin
79 *
80 * @return RequestInterface[]
81 */
82 public function getRequests()
83 {
84 return array_map(function ($t) {
85 return $t['request'];
86 }, $this->transactions);
87 }
88  
89 /**
90 * Get the number of requests in the history
91 *
92 * @return int
93 */
94 public function count()
95 {
96 return count($this->transactions);
97 }
98  
99 /**
100 * Get the last request sent
101 *
102 * @return RequestInterface
103 */
104 public function getLastRequest()
105 {
106 return end($this->transactions)['request'];
107 }
108  
109 /**
110 * Get the last response in the history
111 *
112 * @return ResponseInterface|null
113 */
114 public function getLastResponse()
115 {
116 return end($this->transactions)['response'];
117 }
118  
119 /**
120 * Clears the history
121 */
122 public function clear()
123 {
124 $this->transactions = array();
125 }
126  
127 /**
128 * Add a request to the history
129 *
130 * @param RequestInterface $request Request to add
131 * @param ResponseInterface $response Response of the request
132 */
133 private function add(
134 RequestInterface $request,
135 ResponseInterface $response = null
136 ) {
137 $this->transactions[] = ['request' => $request, 'response' => $response];
138 if (count($this->transactions) > $this->limit) {
139 array_shift($this->transactions);
140 }
141 }
142 }