3 * An observer useful for debugging / testing.
\r
9 * Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
\r
10 * All rights reserved.
\r
12 * Redistribution and use in source and binary forms, with or without
\r
13 * modification, are permitted provided that the following conditions
\r
16 * * Redistributions of source code must retain the above copyright
\r
17 * notice, this list of conditions and the following disclaimer.
\r
18 * * Redistributions in binary form must reproduce the above copyright
\r
19 * notice, this list of conditions and the following disclaimer in the
\r
20 * documentation and/or other materials provided with the distribution.
\r
21 * * The names of the authors may not be used to endorse or promote products
\r
22 * derived from this software without specific prior written permission.
\r
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
\r
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
\r
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
\r
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
\r
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
\r
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
\r
32 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
37 * @package HTTP_Request2
\r
38 * @author David Jean Louis <izi@php.net>
\r
39 * @author Alexey Borzov <avb@php.net>
\r
40 * @license http://opensource.org/licenses/bsd-license.php New BSD License
\r
41 * @version CVS: $Id: Log.php 272593 2009-01-02 16:27:14Z avb $
\r
42 * @link http://pear.php.net/package/HTTP_Request2
\r
46 * Exception class for HTTP_Request2 package
\r
48 require_once 'HTTP/Request2/Exception.php';
\r
51 * A debug observer useful for debugging / testing.
\r
53 * This observer logs to a log target data corresponding to the various request
\r
54 * and response events, it logs by default to php://output but can be configured
\r
55 * to log to a file or via the PEAR Log package.
\r
59 * require_once 'HTTP/Request2.php';
\r
60 * require_once 'HTTP/Request2/Observer/Log.php';
\r
62 * $request = new HTTP_Request2('http://www.example.com');
\r
63 * $observer = new HTTP_Request2_Observer_Log();
\r
64 * $request->attach($observer);
\r
68 * A more complex example with PEAR Log:
\r
70 * require_once 'HTTP/Request2.php';
\r
71 * require_once 'HTTP/Request2/Observer/Log.php';
\r
72 * require_once 'Log.php';
\r
74 * $request = new HTTP_Request2('http://www.example.com');
\r
75 * // we want to log with PEAR log
\r
76 * $observer = new HTTP_Request2_Observer_Log(Log::factory('console'));
\r
78 * // we only want to log received headers
\r
79 * $observer->events = array('receivedHeaders');
\r
81 * $request->attach($observer);
\r
86 * @package HTTP_Request2
\r
87 * @author David Jean Louis <izi@php.net>
\r
88 * @author Alexey Borzov <avb@php.net>
\r
89 * @license http://opensource.org/licenses/bsd-license.php New BSD License
\r
90 * @version Release: 0.4.1
\r
91 * @link http://pear.php.net/package/HTTP_Request2
\r
93 class HTTP_Request2_Observer_Log implements SplObserver
\r
98 * The log target, it can be a a resource or a PEAR Log instance.
\r
100 * @var resource|Log $target
\r
102 protected $target = null;
\r
105 * The events to log.
\r
107 * @var array $events
\r
109 public $events = array(
\r
119 // __construct() {{{
\r
124 * @param mixed $target Can be a file path (default: php://output), a resource,
\r
125 * or an instance of the PEAR Log class.
\r
126 * @param array $events Array of events to listen to (default: all events)
\r
130 public function __construct($target = 'php://output', array $events = array())
\r
132 if (!empty($events)) {
\r
133 $this->events = $events;
\r
135 if (is_resource($target) || $target instanceof Log) {
\r
136 $this->target = $target;
\r
137 } elseif (false === ($this->target = @fopen($target, 'w'))) {
\r
138 throw new HTTP_Request2_Exception("Unable to open '{$target}'");
\r
146 * Called when the request notify us of an event.
\r
148 * @param HTTP_Request2 $subject The HTTP_Request2 instance
\r
152 public function update(SplSubject $subject)
\r
154 $event = $subject->getLastEvent();
\r
155 if (!in_array($event['name'], $this->events)) {
\r
159 switch ($event['name']) {
\r
161 $this->log('* Connected to ' . $event['data']);
\r
163 case 'sentHeaders':
\r
164 $headers = explode("\r\n", $event['data']);
\r
165 array_pop($headers);
\r
166 foreach ($headers as $header) {
\r
167 $this->log('> ' . $header);
\r
170 case 'sentBodyPart':
\r
171 $this->log('> ' . $event['data']);
\r
173 case 'receivedHeaders':
\r
174 $this->log(sprintf('< HTTP/%s %s %s',
\r
175 $event['data']->getVersion(),
\r
176 $event['data']->getStatus(),
\r
177 $event['data']->getReasonPhrase()));
\r
178 $headers = $event['data']->getHeader();
\r
179 foreach ($headers as $key => $val) {
\r
180 $this->log('< ' . $key . ': ' . $val);
\r
184 case 'receivedBody':
\r
185 $this->log($event['data']->getBody());
\r
188 $this->log('* Disconnected');
\r
197 * Log the given message to the configured target.
\r
199 * @param string $message Message to display
\r
203 protected function log($message)
\r
205 if ($this->target instanceof Log) {
\r
206 $this->target->debug($message);
\r
207 } elseif (is_resource($this->target)) {
\r
208 fwrite($this->target, $message . "\r\n");
\r