Log.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * An observer useful for debugging / testing.
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to BSD 3-Clause License that is bundled
  10. * with this package in the file LICENSE and available at the URL
  11. * https://raw.github.com/pear/HTTP_Request2/trunk/docs/LICENSE
  12. *
  13. * @category HTTP
  14. * @package HTTP_Request2
  15. * @author David Jean Louis <izi@php.net>
  16. * @author Alexey Borzov <avb@php.net>
  17. * @copyright 2008-2016 Alexey Borzov <avb@php.net>
  18. * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
  19. * @link http://pear.php.net/package/HTTP_Request2
  20. */
  21. /**
  22. * Exception class for HTTP_Request2 package
  23. */
  24. require_once 'HTTP/Request2/Exception.php';
  25. /**
  26. * A debug observer useful for debugging / testing.
  27. *
  28. * This observer logs to a log target data corresponding to the various request
  29. * and response events, it logs by default to php://output but can be configured
  30. * to log to a file or via the PEAR Log package.
  31. *
  32. * A simple example:
  33. * <code>
  34. * require_once 'HTTP/Request2.php';
  35. * require_once 'HTTP/Request2/Observer/Log.php';
  36. *
  37. * $request = new HTTP_Request2('http://www.example.com');
  38. * $observer = new HTTP_Request2_Observer_Log();
  39. * $request->attach($observer);
  40. * $request->send();
  41. * </code>
  42. *
  43. * A more complex example with PEAR Log:
  44. * <code>
  45. * require_once 'HTTP/Request2.php';
  46. * require_once 'HTTP/Request2/Observer/Log.php';
  47. * require_once 'Log.php';
  48. *
  49. * $request = new HTTP_Request2('http://www.example.com');
  50. * // we want to log with PEAR log
  51. * $observer = new HTTP_Request2_Observer_Log(Log::factory('console'));
  52. *
  53. * // we only want to log received headers
  54. * $observer->events = array('receivedHeaders');
  55. *
  56. * $request->attach($observer);
  57. * $request->send();
  58. * </code>
  59. *
  60. * @category HTTP
  61. * @package HTTP_Request2
  62. * @author David Jean Louis <izi@php.net>
  63. * @author Alexey Borzov <avb@php.net>
  64. * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
  65. * @version Release: 2.3.0
  66. * @link http://pear.php.net/package/HTTP_Request2
  67. */
  68. class HTTP_Request2_Observer_Log implements SplObserver
  69. {
  70. // properties {{{
  71. /**
  72. * The log target, it can be a a resource or a PEAR Log instance.
  73. *
  74. * @var resource|Log $target
  75. */
  76. protected $target = null;
  77. /**
  78. * The events to log.
  79. *
  80. * @var array $events
  81. */
  82. public $events = array(
  83. 'connect',
  84. 'sentHeaders',
  85. 'sentBody',
  86. 'receivedHeaders',
  87. 'receivedBody',
  88. 'disconnect',
  89. );
  90. // }}}
  91. // __construct() {{{
  92. /**
  93. * Constructor.
  94. *
  95. * @param mixed $target Can be a file path (default: php://output), a resource,
  96. * or an instance of the PEAR Log class.
  97. * @param array $events Array of events to listen to (default: all events)
  98. *
  99. * @return void
  100. */
  101. public function __construct($target = 'php://output', array $events = array())
  102. {
  103. if (!empty($events)) {
  104. $this->events = $events;
  105. }
  106. if (is_resource($target) || $target instanceof Log) {
  107. $this->target = $target;
  108. } elseif (false === ($this->target = @fopen($target, 'ab'))) {
  109. throw new HTTP_Request2_Exception("Unable to open '{$target}'");
  110. }
  111. }
  112. // }}}
  113. // update() {{{
  114. /**
  115. * Called when the request notifies us of an event.
  116. *
  117. * @param HTTP_Request2 $subject The HTTP_Request2 instance
  118. *
  119. * @return void
  120. */
  121. public function update(SplSubject $subject)
  122. {
  123. $event = $subject->getLastEvent();
  124. if (!in_array($event['name'], $this->events)) {
  125. return;
  126. }
  127. switch ($event['name']) {
  128. case 'connect':
  129. $this->log('* Connected to ' . $event['data']);
  130. break;
  131. case 'sentHeaders':
  132. $headers = explode("\r\n", $event['data']);
  133. array_pop($headers);
  134. foreach ($headers as $header) {
  135. $this->log('> ' . $header);
  136. }
  137. break;
  138. case 'sentBody':
  139. $this->log('> ' . $event['data'] . ' byte(s) sent');
  140. break;
  141. case 'receivedHeaders':
  142. $this->log(sprintf(
  143. '< HTTP/%s %s %s', $event['data']->getVersion(),
  144. $event['data']->getStatus(), $event['data']->getReasonPhrase()
  145. ));
  146. $headers = $event['data']->getHeader();
  147. foreach ($headers as $key => $val) {
  148. $this->log('< ' . $key . ': ' . $val);
  149. }
  150. $this->log('< ');
  151. break;
  152. case 'receivedBody':
  153. $this->log($event['data']->getBody());
  154. break;
  155. case 'disconnect':
  156. $this->log('* Disconnected');
  157. break;
  158. }
  159. }
  160. // }}}
  161. // log() {{{
  162. /**
  163. * Logs the given message to the configured target.
  164. *
  165. * @param string $message Message to display
  166. *
  167. * @return void
  168. */
  169. protected function log($message)
  170. {
  171. if ($this->target instanceof Log) {
  172. $this->target->debug($message);
  173. } elseif (is_resource($this->target)) {
  174. fwrite($this->target, $message . "\r\n");
  175. }
  176. }
  177. // }}}
  178. }
  179. ?>