]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/HTTP/Request2/Observer/Log.php
Merge branch '0.9.x' into userflag
[quix0rs-gnu-social.git] / extlib / HTTP / Request2 / Observer / Log.php
1 <?php\r
2 /**\r
3  * An observer useful for debugging / testing.\r
4  *\r
5  * PHP version 5\r
6  *\r
7  * LICENSE:\r
8  *\r
9  * Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>\r
10  * All rights reserved.\r
11  *\r
12  * Redistribution and use in source and binary forms, with or without\r
13  * modification, are permitted provided that the following conditions\r
14  * are met:\r
15  *\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
23  *\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
35  *\r
36  * @category HTTP\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
43  */\r
44 \r
45 /**\r
46  * Exception class for HTTP_Request2 package\r
47  */ \r
48 require_once 'HTTP/Request2/Exception.php';\r
49 \r
50 /**\r
51  * A debug observer useful for debugging / testing.\r
52  *\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
56  *\r
57  * A simple example:\r
58  * <code>\r
59  * require_once 'HTTP/Request2.php';\r
60  * require_once 'HTTP/Request2/Observer/Log.php';\r
61  *\r
62  * $request  = new HTTP_Request2('http://www.example.com');\r
63  * $observer = new HTTP_Request2_Observer_Log();\r
64  * $request->attach($observer);\r
65  * $request->send();\r
66  * </code>\r
67  *\r
68  * A more complex example with PEAR Log:\r
69  * <code>\r
70  * require_once 'HTTP/Request2.php';\r
71  * require_once 'HTTP/Request2/Observer/Log.php';\r
72  * require_once 'Log.php';\r
73  *\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
77  *\r
78  * // we only want to log received headers\r
79  * $observer->events = array('receivedHeaders');\r
80  *\r
81  * $request->attach($observer);\r
82  * $request->send();\r
83  * </code>\r
84  *\r
85  * @category HTTP\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
92  */\r
93 class HTTP_Request2_Observer_Log implements SplObserver\r
94 {\r
95     // properties {{{\r
96 \r
97     /**\r
98      * The log target, it can be a a resource or a PEAR Log instance.\r
99      *\r
100      * @var resource|Log $target\r
101      */\r
102     protected $target = null;\r
103 \r
104     /**\r
105      * The events to log.\r
106      *\r
107      * @var array $events\r
108      */\r
109     public $events = array(\r
110         'connect',\r
111         'sentHeaders',\r
112         'sentBodyPart',\r
113         'receivedHeaders',\r
114         'receivedBody',\r
115         'disconnect',\r
116     );\r
117 \r
118     // }}}\r
119     // __construct() {{{\r
120 \r
121     /**\r
122      * Constructor.\r
123      *\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
127      *\r
128      * @return void\r
129      */\r
130     public function __construct($target = 'php://output', array $events = array())\r
131     {\r
132         if (!empty($events)) {\r
133             $this->events = $events;\r
134         }\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
139         }\r
140     }\r
141 \r
142     // }}}\r
143     // update() {{{\r
144 \r
145     /**\r
146      * Called when the request notify us of an event.\r
147      *\r
148      * @param HTTP_Request2 $subject The HTTP_Request2 instance\r
149      *\r
150      * @return void\r
151      */\r
152     public function update(SplSubject $subject)\r
153     {\r
154         $event = $subject->getLastEvent();\r
155         if (!in_array($event['name'], $this->events)) {\r
156             return;\r
157         }\r
158 \r
159         switch ($event['name']) {\r
160         case 'connect':\r
161             $this->log('* Connected to ' . $event['data']);\r
162             break;\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
168             }\r
169             break;\r
170         case 'sentBodyPart':\r
171             $this->log('> ' . $event['data']);\r
172             break;\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
181             }\r
182             $this->log('< ');\r
183             break;\r
184         case 'receivedBody':\r
185             $this->log($event['data']->getBody());\r
186             break;\r
187         case 'disconnect':\r
188             $this->log('* Disconnected');\r
189             break;\r
190         }\r
191     }\r
192     \r
193     // }}}\r
194     // log() {{{\r
195 \r
196     /**\r
197      * Log the given message to the configured target.\r
198      *\r
199      * @param string $message Message to display\r
200      *\r
201      * @return void\r
202      */\r
203     protected function log($message)\r
204     {\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
209         }\r
210     }\r
211 \r
212     // }}}\r
213 }\r
214 \r
215 ?>