sfWebDebugLogger.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWebDebugLogger logs messages into the web debug toolbar.
  11. *
  12. * @package symfony
  13. * @subpackage log
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWebDebugLogger.class.php 16555 2009-03-24 17:41:32Z Kris.Wallsmith $
  16. */
  17. class sfWebDebugLogger extends sfVarLogger
  18. {
  19. protected
  20. $context = null,
  21. $dispatcher = null,
  22. $webDebugClass = null;
  23. /**
  24. * Initializes this logger.
  25. *
  26. * Available options:
  27. *
  28. * - web_debug_class: The web debug class (sfWebDebug by default).
  29. *
  30. * @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
  31. * @param array $options An array of options.
  32. *
  33. * @return Boolean true, if initialization completes successfully, otherwise false.
  34. *
  35. * @see sfVarLogger
  36. */
  37. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  38. {
  39. $this->context = sfContext::getInstance();
  40. $this->dispatcher = $dispatcher;
  41. $this->webDebugClass = isset($options['web_debug_class']) ? $options['web_debug_class'] : 'sfWebDebug';
  42. if (sfConfig::get('sf_web_debug'))
  43. {
  44. $dispatcher->connect('response.filter_content', array($this, 'filterResponseContent'));
  45. }
  46. return parent::initialize($dispatcher, $options);
  47. }
  48. /**
  49. * Listens to the response.filter_content event.
  50. *
  51. * @param sfEvent $event The sfEvent instance
  52. * @param string $context The response content
  53. *
  54. * @return string The filtered response content
  55. */
  56. public function filterResponseContent(sfEvent $event, $content)
  57. {
  58. if (!sfConfig::get('sf_web_debug'))
  59. {
  60. return $content;
  61. }
  62. // log timers information
  63. $messages = array();
  64. foreach (sfTimerManager::getTimers() as $name => $timer)
  65. {
  66. $messages[] = sprintf('%s %.2f ms (%d)', $name, $timer->getElapsedTime() * 1000, $timer->getCalls());
  67. }
  68. $this->dispatcher->notify(new sfEvent($this, 'application.log', $messages));
  69. // don't add debug toolbar:
  70. // * for XHR requests
  71. // * if 304
  72. // * if not rendering to the client
  73. // * if HTTP headers only
  74. $response = $event->getSubject();
  75. $request = $this->context->getRequest();
  76. if (!$this->context->has('request') || !$this->context->has('response') || !$this->context->has('controller') ||
  77. $request->isXmlHttpRequest() ||
  78. strpos($response->getContentType(), 'html') === false ||
  79. $response->getStatusCode() == 304 ||
  80. $this->context->getController()->getRenderMode() != sfView::RENDER_CLIENT ||
  81. $response->isHeaderOnly()
  82. )
  83. {
  84. return $content;
  85. }
  86. $webDebug = new $this->webDebugClass($this->dispatcher, $this, array(
  87. 'image_root_path' => ($request->getRelativeUrlRoot() ? $request->getRelativeUrlRoot() : '').sfConfig::get('sf_web_debug_web_dir').'/images',
  88. ));
  89. return $webDebug->injectToolbar($content);
  90. }
  91. }