Development

/branches/1.2/lib/log/sfWebDebugLogger.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/log/sfWebDebugLogger.class.php

Revision 17749, 3.1 kB (checked in by fabien, 6 years ago)

[1.2, 1.3] fixed some PHPDoc (closes #6371 - patch from gimler)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfWebDebugLogger logs messages into the web debug toolbar.
13  *
14  * @package    symfony
15  * @subpackage log
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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     // log timers information
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     // don't add debug toolbar:
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
Note: See TracBrowser for help on using the browser.