1. sfWebDebugLogger.class.php
  2. /** * sfWebDebugLogger logs messages into the web debug toolbar. * * @package symfony * @subpackage log * @author Fabien Potencier * @version SVN: $Id: sfWebDebugLogger.class.php 22853 2009-10-07 12:11:15Z fabien $ */
  3. class sfWebDebugLogger extends sfVarLogger
  4. {
  5. protected
  6. $context = null,
  7. $webDebugClass = null,
  8. $webDebug = null;
  9. /**
  10. * Initializes this logger.
  11. *
  12. * Available options:
  13. *
  14. * * web_debug_class: The web debug class (sfWebDebug by default)
  15. *
  16. * @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
  17. * @param array $options An array of options.
  18. *
  19. * @return Boolean true, if initialization completes successfully, otherwise false.
  20. *
  21. * @see sfVarLogger
  22. */
  23. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  24. {
  25. $this->context = sfContext::getInstance();
  26. $this->webDebugClass = isset($options['web_debug_class']) ? $options['web_debug_class'] : 'sfWebDebug';
  27. if (sfConfig::get('sf_web_debug'))
  28. {
  29. $dispatcher->connect('context.load_factories', array($this, 'listenForLoadFactories'));
  30. $dispatcher->connect('response.filter_content', array($this, 'filterResponseContent'));
  31. }
  32. $this->registerErrorHandler();
  33. return parent::initialize($dispatcher, $options);
  34. }
  35. /**
  36. * Registers logger with PHP error handler.
  37. */
  38. protected function registerErrorHandler()
  39. {
  40. set_error_handler(array($this,'handlePhpError'));
  41. }
  42. /**
  43. * PHP error handler send PHP errors to log.
  44. *
  45. * PHP user space error handler can not handle E_ERROR, E_PARSE,
  46. * E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING,
  47. * and most of E_STRICT.
  48. *
  49. * @param string $errno The level of the error raised, as an integer.
  50. * @param string $errstr The error message, as a string.
  51. * @param string $errfile The filename that the error was raised in, as a string.
  52. * @param string $errline The line number the error was raised at, as an integer.
  53. * @param array $errcontext An array that points to the active symbol table at the point the error occurred.
  54. */
  55. public function handlePhpError($errno, $errstr, $errfile, $errline, $errcontext = array())
  56. {
  57. if (($errno & error_reporting()) == 0)
  58. {
  59. return false;
  60. }
  61. $message = sprintf(' %%s at %s on line %s (%s)', $errfile, $errline, $errstr);
  62. switch ($errno)
  63. {
  64. case E_STRICT:
  65. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('priority' => sfLogger::ERR, sprintf($message, 'Strict notice'))));
  66. break;
  67. case E_NOTICE:
  68. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('priority' => sfLogger::NOTICE, sprintf($message, 'Notice'))));
  69. break;
  70. case E_WARNING:
  71. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('priority' => sfLogger::WARNING, sprintf($message, 'Warning'))));
  72. break;
  73. case E_RECOVERABLE_ERROR:
  74. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('priority' => sfLogger::ERR, sprintf($message, 'Error'))));
  75. break;
  76. }
  77. return false; // do not prevent default error handling
  78. }
  79. /**
  80. * Listens for the context.load_factories event.
  81. *
  82. * @param sfEvent $event
  83. */
  84. public function listenForLoadFactories(sfEvent $event)
  85. {
  86. $path = sprintf('%s/%s/images', $event->getSubject()->getRequest()->getRelativeUrlRoot(), sfConfig::get('sf_web_debug_web_dir'));
  87. $path = str_replace('//', '/', $path);
  88. $this->webDebug = new $this->webDebugClass($this->dispatcher, $this, array(
  89. 'image_root_path' => $path,
  90. 'request_parameters' => $event->getSubject()->getRequest()->getParameterHolder()->getAll(),
  91. ));
  92. }
  93. /**
  94. * Listens to the response.filter_content event.
  95. *
  96. * @param sfEvent $event The sfEvent instance
  97. * @param string $content The response content
  98. *
  99. * @return string The filtered response content
  100. */
  101. public function filterResponseContent(sfEvent $event, $content)
  102. {
  103. if (!sfConfig::get('sf_web_debug'))
  104. {
  105. return $content;
  106. }
  107. // log timers information
  108. $messages = array();
  109. foreach (sfTimerManager::getTimers() as $name => $timer)
  110. {
  111. $messages[] = sprintf('%s %.2f ms (%d)', $name, $timer->getElapsedTime() * 1000, $timer->getCalls());
  112. }
  113. $this->dispatcher->notify(new sfEvent($this, 'application.log', $messages));
  114. // don't add debug toolbar:
  115. // * for XHR requests
  116. // * if response status code is in the 3xx range
  117. // * if not rendering to the client
  118. // * if HTTP headers only
  119. $response = $event->getSubject();
  120. $request = $this->context->getRequest();
  121. if (
  122. null === $this->webDebug
  123. ||
  124. !$this->context->has('request')
  125. ||
  126. !$this->context->has('response')
  127. ||
  128. !$this->context->has('controller')
  129. ||
  130. $request->isXmlHttpRequest()
  131. ||
  132. strpos($response->getContentType(), 'html') === false
  133. ||
  134. '3' == substr($response->getStatusCode(), 0, 1)
  135. ||
  136. $this->context->getController()->getRenderMode() != sfView::RENDER_CLIENT
  137. ||
  138. $response->isHeaderOnly()
  139. )
  140. {
  141. return $content;
  142. }
  143. return $this->webDebug->injectToolbar($content);
  144. }
  145. }

Debug toolbar