source: branches/version-2_13-dev/data/module/HTTP/Request2/Observer/Log.php @ 23125

Revision 23125, 6.7 KB checked in by kimoto, 11 years ago (diff)

#2275 PEAR更新
不要なrequire_onceの削除
レガシーなPEARモジュールは使わない
SearchReplace?.phpのパスが間違っているので修正

Line 
1<?php
2/**
3 * An observer useful for debugging / testing.
4 *
5 * PHP version 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 2008-2012, Alexey Borzov <avb@php.net>
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 *    * Redistributions of source code must retain the above copyright
17 *      notice, this list of conditions and the following disclaimer.
18 *    * Redistributions in binary form must reproduce the above copyright
19 *      notice, this list of conditions and the following disclaimer in the
20 *      documentation and/or other materials provided with the distribution.
21 *    * The names of the authors may not be used to endorse or promote products
22 *      derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
32 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 * @category HTTP
37 * @package  HTTP_Request2
38 * @author   David Jean Louis <izi@php.net>
39 * @author   Alexey Borzov <avb@php.net>
40 * @license  http://opensource.org/licenses/bsd-license.php New BSD License
41 * @version  SVN: $Id: Log.php 324415 2012-03-21 10:50:50Z avb $
42 * @link     http://pear.php.net/package/HTTP_Request2
43 */
44
45/**
46 * Exception class for HTTP_Request2 package
47 */
48require_once 'HTTP/Request2/Exception.php';
49
50/**
51 * A debug observer useful for debugging / testing.
52 *
53 * This observer logs to a log target data corresponding to the various request
54 * and response events, it logs by default to php://output but can be configured
55 * to log to a file or via the PEAR Log package.
56 *
57 * A simple example:
58 * <code>
59 * require_once 'HTTP/Request2.php';
60 * require_once 'HTTP/Request2/Observer/Log.php';
61 *
62 * $request  = new HTTP_Request2('http://www.example.com');
63 * $observer = new HTTP_Request2_Observer_Log();
64 * $request->attach($observer);
65 * $request->send();
66 * </code>
67 *
68 * A more complex example with PEAR Log:
69 * <code>
70 * require_once 'HTTP/Request2.php';
71 * require_once 'HTTP/Request2/Observer/Log.php';
72 * require_once 'Log.php';
73 *
74 * $request  = new HTTP_Request2('http://www.example.com');
75 * // we want to log with PEAR log
76 * $observer = new HTTP_Request2_Observer_Log(Log::factory('console'));
77 *
78 * // we only want to log received headers
79 * $observer->events = array('receivedHeaders');
80 *
81 * $request->attach($observer);
82 * $request->send();
83 * </code>
84 *
85 * @category HTTP
86 * @package  HTTP_Request2
87 * @author   David Jean Louis <izi@php.net>
88 * @author   Alexey Borzov <avb@php.net>
89 * @license  http://opensource.org/licenses/bsd-license.php New BSD License
90 * @version  Release: 2.1.1
91 * @link     http://pear.php.net/package/HTTP_Request2
92 */
93class HTTP_Request2_Observer_Log implements SplObserver
94{
95    // properties {{{
96
97    /**
98     * The log target, it can be a a resource or a PEAR Log instance.
99     *
100     * @var resource|Log $target
101     */
102    protected $target = null;
103
104    /**
105     * The events to log.
106     *
107     * @var array $events
108     */
109    public $events = array(
110        'connect',
111        'sentHeaders',
112        'sentBody',
113        'receivedHeaders',
114        'receivedBody',
115        'disconnect',
116    );
117
118    // }}}
119    // __construct() {{{
120
121    /**
122     * Constructor.
123     *
124     * @param mixed $target Can be a file path (default: php://output), a resource,
125     *                      or an instance of the PEAR Log class.
126     * @param array $events Array of events to listen to (default: all events)
127     *
128     * @return void
129     */
130    public function __construct($target = 'php://output', array $events = array())
131    {
132        if (!empty($events)) {
133            $this->events = $events;
134        }
135        if (is_resource($target) || $target instanceof Log) {
136            $this->target = $target;
137        } elseif (false === ($this->target = @fopen($target, 'ab'))) {
138            throw new HTTP_Request2_Exception("Unable to open '{$target}'");
139        }
140    }
141
142    // }}}
143    // update() {{{
144
145    /**
146     * Called when the request notifies us of an event.
147     *
148     * @param HTTP_Request2 $subject The HTTP_Request2 instance
149     *
150     * @return void
151     */
152    public function update(SplSubject $subject)
153    {
154        $event = $subject->getLastEvent();
155        if (!in_array($event['name'], $this->events)) {
156            return;
157        }
158
159        switch ($event['name']) {
160        case 'connect':
161            $this->log('* Connected to ' . $event['data']);
162            break;
163        case 'sentHeaders':
164            $headers = explode("\r\n", $event['data']);
165            array_pop($headers);
166            foreach ($headers as $header) {
167                $this->log('> ' . $header);
168            }
169            break;
170        case 'sentBody':
171            $this->log('> ' . $event['data'] . ' byte(s) sent');
172            break;
173        case 'receivedHeaders':
174            $this->log(sprintf(
175                '< HTTP/%s %s %s', $event['data']->getVersion(),
176                $event['data']->getStatus(), $event['data']->getReasonPhrase()
177            ));
178            $headers = $event['data']->getHeader();
179            foreach ($headers as $key => $val) {
180                $this->log('< ' . $key . ': ' . $val);
181            }
182            $this->log('< ');
183            break;
184        case 'receivedBody':
185            $this->log($event['data']->getBody());
186            break;
187        case 'disconnect':
188            $this->log('* Disconnected');
189            break;
190        }
191    }
192
193    // }}}
194    // log() {{{
195
196    /**
197     * Logs the given message to the configured target.
198     *
199     * @param string $message Message to display
200     *
201     * @return void
202     */
203    protected function log($message)
204    {
205        if ($this->target instanceof Log) {
206            $this->target->debug($message);
207        } elseif (is_resource($this->target)) {
208            fwrite($this->target, $message . "\r\n");
209        }
210    }
211
212    // }}}
213}
214
215?>
Note: See TracBrowser for help on using the repository browser.