| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfWebDebugLogger extends sfVarLogger |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$context = null, |
|---|
| 23 |
$dispatcher = null, |
|---|
| 24 |
$webDebugClass = null; |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
* Initializes this logger. |
|---|
| 28 |
* |
|---|
| 29 |
* Available options: |
|---|
| 30 |
* |
|---|
| 31 |
* - web_debug_class: The web debug class (sfWebDebug by default). |
|---|
| 32 |
* |
|---|
| 33 |
* @param sfEventDispatcher $dispatcher A sfEventDispatcher instance |
|---|
| 34 |
* @param array $options An array of options. |
|---|
| 35 |
* |
|---|
| 36 |
* @return Boolean true, if initialization completes successfully, otherwise false. |
|---|
| 37 |
* |
|---|
| 38 |
* @see sfVarLogger |
|---|
| 39 |
*/ |
|---|
| 40 |
public function initialize(sfEventDispatcher $dispatcher, $options = array()) |
|---|
| 41 |
{ |
|---|
| 42 |
$this->context = sfContext::getInstance(); |
|---|
| 43 |
$this->dispatcher = $dispatcher; |
|---|
| 44 |
|
|---|
| 45 |
$this->webDebugClass = isset($options['web_debug_class']) ? $options['web_debug_class'] : 'sfWebDebug'; |
|---|
| 46 |
|
|---|
| 47 |
if (sfConfig::get('sf_web_debug')) |
|---|
| 48 |
{ |
|---|
| 49 |
$dispatcher->connect('response.filter_content', array($this, 'filterResponseContent')); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
return parent::initialize($dispatcher, $options); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
* Listens to the response.filter_content event. |
|---|
| 57 |
* |
|---|
| 58 |
* @param sfEvent $event The sfEvent instance |
|---|
| 59 |
* @param string $content The response content |
|---|
| 60 |
* |
|---|
| 61 |
* @return string The filtered response content |
|---|
| 62 |
*/ |
|---|
| 63 |
public function filterResponseContent(sfEvent $event, $content) |
|---|
| 64 |
{ |
|---|
| 65 |
if (!sfConfig::get('sf_web_debug')) |
|---|
| 66 |
{ |
|---|
| 67 |
return $content; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
$messages = array(); |
|---|
| 72 |
foreach (sfTimerManager::getTimers() as $name => $timer) |
|---|
| 73 |
{ |
|---|
| 74 |
$messages[] = sprintf('%s %.2f ms (%d)', $name, $timer->getElapsedTime() * 1000, $timer->getCalls()); |
|---|
| 75 |
} |
|---|
| 76 |
$this->dispatcher->notify(new sfEvent($this, 'application.log', $messages)); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
// * for XHR requests |
|---|
| 80 |
// * if response status code is in the 3xx range |
|---|
| 81 |
// * if not rendering to the client |
|---|
| 82 |
// * if HTTP headers only |
|---|
| 83 |
$response = $event->getSubject(); |
|---|
| 84 |
$request = $this->context->getRequest(); |
|---|
| 85 |
if (!$this->context->has('request') || !$this->context->has('response') || !$this->context->has('controller') || |
|---|
| 86 |
$request->isXmlHttpRequest() || |
|---|
| 87 |
strpos($response->getContentType(), 'html') === false || |
|---|
| 88 |
'3' == substr($response->getStatusCode(), 0, 1) || |
|---|
| 89 |
$this->context->getController()->getRenderMode() != sfView::RENDER_CLIENT || |
|---|
| 90 |
$response->isHeaderOnly() |
|---|
| 91 |
) |
|---|
| 92 |
{ |
|---|
| 93 |
return $content; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
$webDebug = new $this->webDebugClass($this->dispatcher, $this, array( |
|---|
| 97 |
'image_root_path' => ($request->getRelativeUrlRoot() ? $request->getRelativeUrlRoot() : '').sfConfig::get('sf_web_debug_web_dir').'/images', |
|---|
| 98 |
)); |
|---|
| 99 |
|
|---|
| 100 |
return $webDebug->injectToolbar($content); |
|---|
| 101 |
} |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|