@@ -34,152 +34,152 @@ |
||
34 | 34 | */ |
35 | 35 | class HistoryPlugin implements EventSubscriberInterface, \IteratorAggregate, \Countable |
36 | 36 | { |
37 | - /** @var int The maximum number of requests to maintain in the history */ |
|
38 | - protected $limit = 10; |
|
39 | - |
|
40 | - /** @var array Requests and responses that have passed through the plugin */ |
|
41 | - protected $transactions = array(); |
|
42 | - |
|
43 | - public static function getSubscribedEvents() |
|
44 | - { |
|
45 | - return array('request.sent' => array('onRequestSent', 9999)); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Convert to a string that contains all request and response headers |
|
50 | - * |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function __toString() |
|
54 | - { |
|
55 | - $lines = array(); |
|
56 | - foreach ($this->transactions as $entry) { |
|
57 | - $response = isset($entry['response']) ? $entry['response'] : ''; |
|
58 | - $lines[] = '> ' . trim($entry['request']) . "\n\n< " . trim($response) . "\n"; |
|
59 | - } |
|
60 | - |
|
61 | - return implode("\n", $lines); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Add a request to the history |
|
66 | - * |
|
67 | - * @param RequestInterface $request Request to add |
|
68 | - * @param Response $response Response of the request |
|
69 | - * |
|
70 | - * @return HistoryPlugin |
|
71 | - */ |
|
72 | - public function add(RequestInterface $request, Response $response = null) |
|
73 | - { |
|
74 | - if (!$response && $request->getResponse()) { |
|
75 | - $response = $request->getResponse(); |
|
76 | - } |
|
77 | - |
|
78 | - $this->transactions[] = array('request' => $request, 'response' => $response); |
|
79 | - if (count($this->transactions) > $this->getlimit()) { |
|
80 | - array_shift($this->transactions); |
|
81 | - } |
|
82 | - |
|
83 | - return $this; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Set the max number of requests to store |
|
88 | - * |
|
89 | - * @param int $limit Limit |
|
90 | - * |
|
91 | - * @return HistoryPlugin |
|
92 | - */ |
|
93 | - public function setLimit($limit) |
|
94 | - { |
|
95 | - $this->limit = (int) $limit; |
|
96 | - |
|
97 | - return $this; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Get the request limit |
|
102 | - * |
|
103 | - * @return int |
|
104 | - */ |
|
105 | - public function getLimit() |
|
106 | - { |
|
107 | - return $this->limit; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get all of the raw transactions in the form of an array of associative arrays containing |
|
112 | - * 'request' and 'response' keys. |
|
113 | - * |
|
114 | - * @return array |
|
115 | - */ |
|
116 | - public function getAll() |
|
117 | - { |
|
118 | - return $this->transactions; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Get the requests in the history |
|
123 | - * |
|
124 | - * @return \ArrayIterator |
|
125 | - */ |
|
126 | - public function getIterator() |
|
127 | - { |
|
128 | - // Return an iterator just like the old iteration of the HistoryPlugin for BC compatibility (use getAll()) |
|
129 | - return new \ArrayIterator(array_map(function ($entry) { |
|
130 | - $entry['request']->getParams()->set('actual_response', $entry['response']); |
|
131 | - return $entry['request']; |
|
132 | - }, $this->transactions)); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Get the number of requests in the history |
|
137 | - * |
|
138 | - * @return int |
|
139 | - */ |
|
140 | - public function count() |
|
141 | - { |
|
142 | - return count($this->transactions); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get the last request sent |
|
147 | - * |
|
148 | - * @return RequestInterface |
|
149 | - */ |
|
150 | - public function getLastRequest() |
|
151 | - { |
|
152 | - $last = end($this->transactions); |
|
153 | - |
|
154 | - return $last['request']; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get the last response in the history |
|
159 | - * |
|
160 | - * @return Response|null |
|
161 | - */ |
|
162 | - public function getLastResponse() |
|
163 | - { |
|
164 | - $last = end($this->transactions); |
|
165 | - |
|
166 | - return isset($last['response']) ? $last['response'] : null; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Clears the history |
|
171 | - * |
|
172 | - * @return HistoryPlugin |
|
173 | - */ |
|
174 | - public function clear() |
|
175 | - { |
|
176 | - $this->transactions = array(); |
|
177 | - |
|
178 | - return $this; |
|
179 | - } |
|
180 | - |
|
181 | - public function onRequestSent(Event $event) |
|
182 | - { |
|
183 | - $this->add($event['request'], $event['response']); |
|
184 | - } |
|
37 | + /** @var int The maximum number of requests to maintain in the history */ |
|
38 | + protected $limit = 10; |
|
39 | + |
|
40 | + /** @var array Requests and responses that have passed through the plugin */ |
|
41 | + protected $transactions = array(); |
|
42 | + |
|
43 | + public static function getSubscribedEvents() |
|
44 | + { |
|
45 | + return array('request.sent' => array('onRequestSent', 9999)); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Convert to a string that contains all request and response headers |
|
50 | + * |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function __toString() |
|
54 | + { |
|
55 | + $lines = array(); |
|
56 | + foreach ($this->transactions as $entry) { |
|
57 | + $response = isset($entry['response']) ? $entry['response'] : ''; |
|
58 | + $lines[] = '> ' . trim($entry['request']) . "\n\n< " . trim($response) . "\n"; |
|
59 | + } |
|
60 | + |
|
61 | + return implode("\n", $lines); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Add a request to the history |
|
66 | + * |
|
67 | + * @param RequestInterface $request Request to add |
|
68 | + * @param Response $response Response of the request |
|
69 | + * |
|
70 | + * @return HistoryPlugin |
|
71 | + */ |
|
72 | + public function add(RequestInterface $request, Response $response = null) |
|
73 | + { |
|
74 | + if (!$response && $request->getResponse()) { |
|
75 | + $response = $request->getResponse(); |
|
76 | + } |
|
77 | + |
|
78 | + $this->transactions[] = array('request' => $request, 'response' => $response); |
|
79 | + if (count($this->transactions) > $this->getlimit()) { |
|
80 | + array_shift($this->transactions); |
|
81 | + } |
|
82 | + |
|
83 | + return $this; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Set the max number of requests to store |
|
88 | + * |
|
89 | + * @param int $limit Limit |
|
90 | + * |
|
91 | + * @return HistoryPlugin |
|
92 | + */ |
|
93 | + public function setLimit($limit) |
|
94 | + { |
|
95 | + $this->limit = (int) $limit; |
|
96 | + |
|
97 | + return $this; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Get the request limit |
|
102 | + * |
|
103 | + * @return int |
|
104 | + */ |
|
105 | + public function getLimit() |
|
106 | + { |
|
107 | + return $this->limit; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get all of the raw transactions in the form of an array of associative arrays containing |
|
112 | + * 'request' and 'response' keys. |
|
113 | + * |
|
114 | + * @return array |
|
115 | + */ |
|
116 | + public function getAll() |
|
117 | + { |
|
118 | + return $this->transactions; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Get the requests in the history |
|
123 | + * |
|
124 | + * @return \ArrayIterator |
|
125 | + */ |
|
126 | + public function getIterator() |
|
127 | + { |
|
128 | + // Return an iterator just like the old iteration of the HistoryPlugin for BC compatibility (use getAll()) |
|
129 | + return new \ArrayIterator(array_map(function ($entry) { |
|
130 | + $entry['request']->getParams()->set('actual_response', $entry['response']); |
|
131 | + return $entry['request']; |
|
132 | + }, $this->transactions)); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Get the number of requests in the history |
|
137 | + * |
|
138 | + * @return int |
|
139 | + */ |
|
140 | + public function count() |
|
141 | + { |
|
142 | + return count($this->transactions); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get the last request sent |
|
147 | + * |
|
148 | + * @return RequestInterface |
|
149 | + */ |
|
150 | + public function getLastRequest() |
|
151 | + { |
|
152 | + $last = end($this->transactions); |
|
153 | + |
|
154 | + return $last['request']; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get the last response in the history |
|
159 | + * |
|
160 | + * @return Response|null |
|
161 | + */ |
|
162 | + public function getLastResponse() |
|
163 | + { |
|
164 | + $last = end($this->transactions); |
|
165 | + |
|
166 | + return isset($last['response']) ? $last['response'] : null; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Clears the history |
|
171 | + * |
|
172 | + * @return HistoryPlugin |
|
173 | + */ |
|
174 | + public function clear() |
|
175 | + { |
|
176 | + $this->transactions = array(); |
|
177 | + |
|
178 | + return $this; |
|
179 | + } |
|
180 | + |
|
181 | + public function onRequestSent(Event $event) |
|
182 | + { |
|
183 | + $this->add($event['request'], $event['response']); |
|
184 | + } |
|
185 | 185 | } |
186 | 186 | \ No newline at end of file |