XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
Log.php
Go to the documentation of this file.
1 <?php
25 require_once 'HTTP/Request2/Exception.php';
26 
70 class HTTP_Request2_Observer_Log implements SplObserver
71 {
72  // properties {{{
73 
79  protected $target = null;
80 
86  public $events = array(
87  'connect',
88  'sentHeaders',
89  'sentBody',
90  'receivedHeaders',
91  'receivedBody',
92  'disconnect',
93  );
94 
95  // }}}
96  // __construct() {{{
97 
107  public function __construct($target = 'php://output', array $events = array())
108  {
109  if (!empty($events)) {
110  $this->events = $events;
111  }
112  if (is_resource($target) || $target instanceof Log) {
113  $this->target = $target;
114  } elseif (false === ($this->target = @fopen($target, 'ab'))) {
115  throw new HTTP_Request2_Exception("Unable to open '{$target}'");
116  }
117  }
118 
119  // }}}
120  // update() {{{
121 
129  public function update(SplSubject $subject)
130  {
131  $event = $subject->getLastEvent();
132  if (!in_array($event['name'], $this->events)) {
133  return;
134  }
135 
136  switch ($event['name']) {
137  case 'connect':
138  $this->log('* Connected to ' . $event['data']);
139  break;
140  case 'sentHeaders':
141  $headers = explode("\r\n", $event['data']);
142  array_pop($headers);
143  foreach ($headers as $header) {
144  $this->log('> ' . $header);
145  }
146  break;
147  case 'sentBody':
148  $this->log('> ' . $event['data'] . ' byte(s) sent');
149  break;
150  case 'receivedHeaders':
151  $this->log(sprintf(
152  '< HTTP/%s %s %s', $event['data']->getVersion(),
153  $event['data']->getStatus(), $event['data']->getReasonPhrase()
154  ));
155  $headers = $event['data']->getHeader();
156  foreach ($headers as $key => $val) {
157  $this->log('< ' . $key . ': ' . $val);
158  }
159  $this->log('< ');
160  break;
161  case 'receivedBody':
162  $this->log($event['data']->getBody());
163  break;
164  case 'disconnect':
165  $this->log('* Disconnected');
166  break;
167  }
168  }
169 
170  // }}}
171  // log() {{{
172 
180  protected function log($message)
181  {
182  if ($this->target instanceof Log) {
183  $this->target->debug($message);
184  } elseif (is_resource($this->target)) {
185  fwrite($this->target, $message . "\r\n");
186  }
187  }
188 
189  // }}}
190 }
191 
192 ?>
update(SplSubject $subject)
Definition: Log.php:129
__construct($target= 'php://output', array $events=array())
Definition: Log.php:107