PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/frapi/library/PEAR/HTTP/Request2/Observer/Log.php

http://github.com/frapi/frapi
PHP | 215 lines | 73 code | 13 blank | 129 comment | 6 complexity | da9e6e4a34176cc5c3be319c7ddbfe82 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * An observer useful for debugging / testing.
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2008-2011, 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 308680 2011-02-25 17:40:17Z avb $
  42. * @link http://pear.php.net/package/HTTP_Request2
  43. */
  44. /**
  45. * Exception class for HTTP_Request2 package
  46. */
  47. require_once 'HTTP/Request2/Exception.php';
  48. /**
  49. * A debug observer useful for debugging / testing.
  50. *
  51. * This observer logs to a log target data corresponding to the various request
  52. * and response events, it logs by default to php://output but can be configured
  53. * to log to a file or via the PEAR Log package.
  54. *
  55. * A simple example:
  56. * <code>
  57. * require_once 'HTTP/Request2.php';
  58. * require_once 'HTTP/Request2/Observer/Log.php';
  59. *
  60. * $request = new HTTP_Request2('http://www.example.com');
  61. * $observer = new HTTP_Request2_Observer_Log();
  62. * $request->attach($observer);
  63. * $request->send();
  64. * </code>
  65. *
  66. * A more complex example with PEAR Log:
  67. * <code>
  68. * require_once 'HTTP/Request2.php';
  69. * require_once 'HTTP/Request2/Observer/Log.php';
  70. * require_once 'Log.php';
  71. *
  72. * $request = new HTTP_Request2('http://www.example.com');
  73. * // we want to log with PEAR log
  74. * $observer = new HTTP_Request2_Observer_Log(Log::factory('console'));
  75. *
  76. * // we only want to log received headers
  77. * $observer->events = array('receivedHeaders');
  78. *
  79. * $request->attach($observer);
  80. * $request->send();
  81. * </code>
  82. *
  83. * @category HTTP
  84. * @package HTTP_Request2
  85. * @author David Jean Louis <izi@php.net>
  86. * @author Alexey Borzov <avb@php.net>
  87. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  88. * @version Release: 2.0.0RC1
  89. * @link http://pear.php.net/package/HTTP_Request2
  90. */
  91. class HTTP_Request2_Observer_Log implements SplObserver
  92. {
  93. // properties {{{
  94. /**
  95. * The log target, it can be a a resource or a PEAR Log instance.
  96. *
  97. * @var resource|Log $target
  98. */
  99. protected $target = null;
  100. /**
  101. * The events to log.
  102. *
  103. * @var array $events
  104. */
  105. public $events = array(
  106. 'connect',
  107. 'sentHeaders',
  108. 'sentBody',
  109. 'receivedHeaders',
  110. 'receivedBody',
  111. 'disconnect',
  112. );
  113. // }}}
  114. // __construct() {{{
  115. /**
  116. * Constructor.
  117. *
  118. * @param mixed $target Can be a file path (default: php://output), a resource,
  119. * or an instance of the PEAR Log class.
  120. * @param array $events Array of events to listen to (default: all events)
  121. *
  122. * @return void
  123. */
  124. public function __construct($target = 'php://output', array $events = array())
  125. {
  126. if (!empty($events)) {
  127. $this->events = $events;
  128. }
  129. if (is_resource($target) || $target instanceof Log) {
  130. $this->target = $target;
  131. } elseif (false === ($this->target = @fopen($target, 'ab'))) {
  132. throw new HTTP_Request2_Exception("Unable to open '{$target}'");
  133. }
  134. }
  135. // }}}
  136. // update() {{{
  137. /**
  138. * Called when the request notifies us of an event.
  139. *
  140. * @param HTTP_Request2 $subject The HTTP_Request2 instance
  141. *
  142. * @return void
  143. */
  144. public function update(SplSubject $subject)
  145. {
  146. $event = $subject->getLastEvent();
  147. if (!in_array($event['name'], $this->events)) {
  148. return;
  149. }
  150. switch ($event['name']) {
  151. case 'connect':
  152. $this->log('* Connected to ' . $event['data']);
  153. break;
  154. case 'sentHeaders':
  155. $headers = explode("\r\n", $event['data']);
  156. array_pop($headers);
  157. foreach ($headers as $header) {
  158. $this->log('> ' . $header);
  159. }
  160. break;
  161. case 'sentBody':
  162. $this->log('> ' . $event['data'] . ' byte(s) sent');
  163. break;
  164. case 'receivedHeaders':
  165. $this->log(sprintf('< HTTP/%s %s %s',
  166. $event['data']->getVersion(),
  167. $event['data']->getStatus(),
  168. $event['data']->getReasonPhrase()));
  169. $headers = $event['data']->getHeader();
  170. foreach ($headers as $key => $val) {
  171. $this->log('< ' . $key . ': ' . $val);
  172. }
  173. $this->log('< ');
  174. break;
  175. case 'receivedBody':
  176. $this->log($event['data']->getBody());
  177. break;
  178. case 'disconnect':
  179. $this->log('* Disconnected');
  180. break;
  181. }
  182. }
  183. // }}}
  184. // log() {{{
  185. /**
  186. * Logs the given message to the configured target.
  187. *
  188. * @param string $message Message to display
  189. *
  190. * @return void
  191. */
  192. protected function log($message)
  193. {
  194. if ($this->target instanceof Log) {
  195. $this->target->debug($message);
  196. } elseif (is_resource($this->target)) {
  197. fwrite($this->target, $message . "\r\n");
  198. }
  199. }
  200. // }}}
  201. }
  202. ?>