Development

Changeset 6509

You must first sign up to be able to contribute.

Changeset 6509

Show
Ignore:
Timestamp:
12/15/07 17:13:54 (9 months ago)
Author:
fabien
Message:

refactored web debug toolbar logging

  • removed sfWebDebugFilter
  • moved logic from sfWebDebugFiler to sfWebDebugLogger
  • added a response.filter_content event
  • deprecated short message logging
  • sfWebDebug::decorateContentWithDebug is now a static method
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/UPGRADE

    r6501 r6509  
    5858  <?php endif; ?> 
    5959 
    60 The flash entry in `filters.yml` must be removed too as the `sfFlashFilter` 
     60The `flash` entry in `filters.yml` must be removed too as the `sfFlashFilter` 
    6161was removed. 
    6262 
     
    315315 
    316316The `initialize()` call is not needed anymore (see the point above). 
     317 
     318Web Debug 
     319--------- 
     320 
     321The `web_debug` entry in `filters.yml` must be removed as the `sfWebDebugFilter` 
     322has been removed. The web debug toolbar is now injected in the response thanks to a listener. 
     323 
     324The `project:upgrade1.1` task makes all those changes for you. 
  • trunk/data/config/filters.yml

    r5384 r6509  
    44  param: 
    55    type: rendering 
    6  
    7 web_debug: 
    8   class: sfWebDebugFilter 
    9   param: 
    10     condition: %SF_WEB_DEBUG% 
    116 
    127# security filter must have a type of security 
  • trunk/data/skeleton/app/app/config/filters.yml

    r6157 r6509  
    11rendering: ~ 
    2  
    3 web_debug: ~ 
    42security:  ~ 
    53 
  • trunk/lib/action/sfComponent.class.php

    r5814 r6509  
    141141  public function debugMessage($message) 
    142142  { 
    143     if (sfConfig::get('sf_web_debug')
     143    if (sfConfig::get('sf_web_debug') && sfConfig::get('sf_logging_enabled')
    144144    { 
    145       $this->context->get('sf_web_debug')->logShortMessage($message); 
     145      sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array('This feature is deprecated in favor of the log_message helper.', 'priority' => sfLogger::ERR))); 
    146146    } 
    147147  } 
  • trunk/lib/config/sfLoader.class.php

    r5260 r6509  
    401401        if (is_readable($dir.'/'.$fileName)) 
    402402        { 
    403           include($dir.'/'.$fileName); 
     403          include_once($dir.'/'.$fileName); 
    404404          $included = true; 
    405405          break; 
     
    410410      { 
    411411        // search in the include path 
    412         if ((@include('helper/'.$fileName)) != 1) 
     412        if ((@include_once('helper/'.$fileName)) != 1) 
    413413        { 
    414414          $dirs = array_merge($dirs, explode(PATH_SEPARATOR, get_include_path())); 
  • trunk/lib/debug/sfWebDebug.class.php

    r4850 r6509  
    2121  protected 
    2222    $log         = array(), 
    23     $shortLog    = array(), 
    2423    $maxPriority = 1000, 
    2524    $types       = array(), 
    2625    $lastTimeLog = -1; 
    27  
    28   /** 
    29    * Registers javascripts and stylesheets needed for the web debug toolbar. 
    30    */ 
    31   public function registerAssets() 
    32   { 
    33     $response = sfContext::getInstance()->getResponse(); 
    34  
    35     // register our css and js 
    36     $response->addJavascript(sfConfig::get('sf_web_debug_web_dir').'/js/main'); 
    37     $response->addStylesheet(sfConfig::get('sf_web_debug_web_dir').'/css/main'); 
    38   } 
    39  
    40   /** 
    41    * Logs a short message to be displayed in the web debug toolbar. 
    42    * 
    43    * @param string The message string 
    44    */ 
    45   public function logShortMessage($message) 
    46   { 
    47     $this->shortLog[] = $message; 
    48   } 
    4926 
    5027  /** 
     
    274251    $timeInfoDetails .= '</table>'; 
    275252 
    276     // short log messages 
    277     $shortMessages = ''; 
    278     if ($this->shortLog) 
    279     { 
    280       $shortMessages = '<ul id="sfWebDebugShortMessages"><li>&raquo;&nbsp;'.implode('</li><li>&raquo&nbsp;', $this->shortLog).'</li></ul>'; 
    281     } 
    282  
    283253    // logs 
    284254    $logInfo = ''; 
    285255    if (sfConfig::get('sf_logging_enabled')) 
    286256    { 
    287       $logInfo .= $shortMessages.
     257      $logInfo .=
    288258        <ul id="sfWebDebugLogMenu"> 
    289259          <li><a href="#" onclick="sfWebDebugToggleAllLogLines(true, \'sfWebDebugLogLine\'); return false;">[all]</a></li> 
     
    389359   * @return string The decorated HTML string 
    390360   */ 
    391   public function decorateContentWithDebug($internalUri, $content, $new = false) 
     361  static public function decorateContentWithDebug($internalUri, $content, $new = false) 
    392362  { 
    393363    $context = sfContext::getInstance(); 
     
    400370 
    401371    $cache = $context->getViewCacheManager(); 
    402     $this->loadHelpers(); 
     372    sfLoader::loadHelpers(array('Helper', 'Url', 'Asset', 'Tag')); 
    403373 
    404374    $bgColor      = $new ? '#9ff' : '#ff9'; 
  • trunk/lib/filter/sfRenderingFilter.class.php

    r5021 r6509  
    4343    // send headers + content 
    4444    $response->send(); 
    45  
    46     // log timers information 
    47     if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) 
    48     { 
    49       $messages = array(); 
    50       foreach (sfTimerManager::getTimers() as $name => $timer) 
    51       { 
    52         $messages[] = sprintf('%s %.2f ms (%d)', $name, $timer->getElapsedTime() * 1000, $timer->getCalls()); 
    53       } 
    54  
    55       $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', $messages)); 
    56     } 
    5745  } 
    5846} 
  • trunk/lib/log/sfWebDebugLogger.class.php

    r5844 r6509  
    2121  protected 
    2222    $context       = null, 
    23     $buffer        = array()
     23    $dispatcher    = null
    2424    $webDebug      = null, 
    2525    $xdebugLogging = false; 
     
    3535  public function initialize(sfEventDispatcher $dispatcher, $options = array()) 
    3636  { 
    37     if (!sfConfig::get('sf_web_debug')) 
    38     { 
    39       return; 
    40     } 
     37    $this->webDebug   = new sfWebDebug(); 
     38    $this->context    = sfContext::getInstance(); 
     39    $this->dispatcher = $dispatcher; 
    4140 
    42     $this->buffer  = array(); 
    43     $this->context = sfContext::getInstance(); 
     41    $dispatcher->connect('response.filter_content', array($this, 'filterResponseContent')); 
    4442 
    4543    if (isset($options['xdebug_logging'])) 
     
    5250 
    5351  /** 
     52   * Listens to the response.filter_content event. 
     53   * 
     54   * @param  sfEvent The sfEvent instance 
     55   * @param  string  The response content 
     56   * 
     57   * @return string  The filtered response content 
     58   */ 
     59  public function filterResponseContent(sfEvent $event, $content) 
     60  { 
     61    if (!sfConfig::get('sf_web_debug')) 
     62    { 
     63      return $content; 
     64    } 
     65 
     66    // log timers information 
     67    $messages = array(); 
     68    foreach (sfTimerManager::getTimers() as $name => $timer) 
     69    { 
     70      $messages[] = sprintf('%s %.2f ms (%d)', $name, $timer->getElapsedTime() * 1000, $timer->getCalls()); 
     71    } 
     72    $this->dispatcher->notify(new sfEvent($this, 'application.log', $messages)); 
     73 
     74    // don't add debug toolbar: 
     75    // * for XHR requests 
     76    // * if 304 
     77    // * if not rendering to the client 
     78    // * if HTTP headers only 
     79    $response = $event->getSubject(); 
     80    if ( 
     81      $this->context->getRequest()->isXmlHttpRequest() || 
     82      strpos($response->getContentType(), 'html') === false || 
     83      $response->getStatusCode() == 304 || 
     84      $this->context->getController()->getRenderMode() != sfView::RENDER_CLIENT || 
     85      $response->isHeaderOnly() 
     86    ) 
     87    { 
     88      return $content; 
     89    } 
     90 
     91    // add needed assets for the web debug toolbar 
     92    $assets = sprintf(' 
     93      <script type="text/javascript" src="%s"></script> 
     94      <link rel="stylesheet" type="text/css" media="screen" href="%s" />', 
     95      sfConfig::get('sf_web_debug_web_dir').'/js/main.js', 
     96      sfConfig::get('sf_web_debug_web_dir').'/css/main.css' 
     97    ); 
     98    $content = str_ireplace('</head>', $assets.'</head>', $content); 
     99 
     100    // add web debug information to response content 
     101    $webDebugContent = $this->webDebug->getResults(); 
     102    $count = 0; 
     103    $content = str_ireplace('</body>', $webDebugContent.'</body>', $content, $count); 
     104    if (!$count) 
     105    { 
     106      $content .= $webDebugContent; 
     107    } 
     108 
     109    return $content; 
     110  } 
     111 
     112  /** 
    54113   * Logs a message. 
    55114   * 
     
    59118  protected function doLog($message, $priority) 
    60119  { 
    61     if (!sfConfig::get('sf_web_debug')) 
    62     { 
    63       return; 
    64     } 
    65  
    66120    // if we have xdebug and dev has not disabled the feature, add some stack information 
    67121    $debugStack = array(); 
     
    78132          if (isset($stack['function'])) 
    79133          { 
    80             $tmp .= 'in "'.$stack['function'].'" '
     134            $tmp .= sprintf('in "%s" ', $stack['function'])
    81135          } 
    82           $tmp .= 'from "'.$stack['file'].'" line '.$stack['line']
     136          $tmp .= sprintf('from "%s" line %s', $stack['file'], $stack['line'])
    83137          $debugStack[] = $tmp; 
    84138        } 
     
    94148    } 
    95149 
    96     // build the object containing the complete log information 
    97     $logEntry = array( 
     150    // send the log object containing the complete log information 
     151    $this->webDebug->log(array( 
    98152      'priority'   => $priority, 
    99153      'time'       => time(), 
     
    101155      'type'       => $type, 
    102156      'debugStack' => $debugStack, 
    103     ); 
    104  
    105     // send the log object 
    106     if (is_null($this->webDebug)) 
    107     { 
    108       $this->buffer[] = $logEntry; 
    109  
    110       if ($this->context->has('sf_web_debug')) 
    111       { 
    112         $this->webDebug = $this->context->get('sf_web_debug'); 
    113         while ($buffer = array_shift($this->buffer)) 
    114         { 
    115           $this->webDebug->log($buffer); 
    116         } 
    117       } 
    118     } 
    119     else 
    120     { 
    121       $this->webDebug->log($logEntry); 
    122     } 
     157    )); 
    123158  } 
    124159} 
  • trunk/lib/plugins/sfCompat10Plugin/lib/helper/DebugHelper.php

    r4951 r6509  
    33function debug_message($message) 
    44{ 
    5   if (sfConfig::get('sf_web_debug')
     5  if (sfConfig::get('sf_web_debug') && sfConfig::get('sf_logging_enabled')
    66  { 
    7     sfContext::getInstance()->get('sf_web_debug')->logShortMessage($message); 
     7    sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array('This feature is deprecated in favor of the log_message helper.', 'priority' => sfLogger::ERR))); 
    88  } 
    99} 
  • trunk/lib/plugins/sfCompat10Plugin/lib/helper/PartialHelper.php

    r5092 r6509  
    268268  if (sfConfig::get('sf_web_debug')) 
    269269  { 
    270     $retval = sfContext::getInstance()->get('sf_web_debug')->decorateContentWithDebug($uri, $retval, false); 
     270    $retval = sfWebDebug::decorateContentWithDebug($uri, $retval, false); 
    271271  } 
    272272 
     
    280280  if ($saved && sfConfig::get('sf_web_debug')) 
    281281  { 
    282     $retval = sfContext::getInstance()->get('sf_web_debug')->decorateContentWithDebug($uri, $retval, true); 
     282    $retval = sfWebDebug::decorateContentWithDebug($uri, $retval, true); 
    283283  } 
    284284 
  • trunk/lib/plugins/sfCompat10Plugin/test/functional/fixtures/apps/frontend/config/filters.yml

    r5320 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43 
  • trunk/lib/plugins/sfPropelPlugin/test/functional/fixtures/apps/backend/config/filters.yml

    r4934 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43 
  • trunk/lib/plugins/sfPropelPlugin/test/functional/fixtures/apps/crud/config/filters.yml

    r4934 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43 
  • trunk/lib/response/sfResponse.class.php

    r5156 r6509  
    7878  public function sendContent() 
    7979  { 
     80    $event = $this->dispatcher->filter(new sfEvent($this, 'response.filter_content'), $this->getContent()); 
     81    $content = $event->getReturnValue(); 
     82 
    8083    if (sfConfig::get('sf_logging_enabled')) 
    8184    { 
    82       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send content (%s o)', strlen($this->getContent()))))); 
     85      $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send content (%s o)', strlen($content))))); 
    8386    } 
    8487 
    85     echo $this->getContent()
     88    echo $content
    8689  } 
    8790 
  • trunk/lib/view/sfViewCacheManager.class.php

    r5025 r6509  
    560560    if (sfConfig::get('sf_web_debug')) 
    561561    { 
    562       $content = $this->context->get('sf_web_debug')->decorateContentWithDebug($uri, $content, false); 
     562      $content = sfWebDebug::decorateContentWithDebug($uri, $content, false); 
    563563    } 
    564564 
     
    586586    if ($saved && sfConfig::get('sf_web_debug')) 
    587587    { 
    588       $content = $this->context->get('sf_web_debug')->decorateContentWithDebug($uri, $content, true); 
     588      $content = sfWebDebug::decorateContentWithDebug($uri, $content, true); 
    589589    } 
    590590 
     
    609609    if ($saved && sfConfig::get('sf_web_debug')) 
    610610    { 
    611       $content = $this->context->get('sf_web_debug')->decorateContentWithDebug($uri, $this->context->getResponse()->getContent(), true); 
     611      $content = sfWebDebug::decorateContentWithDebug($uri, $this->context->getResponse()->getContent(), true); 
    612612      $this->context->getResponse()->setContent($content); 
    613613    } 
     
    643643      if (sfConfig::get('sf_web_debug')) 
    644644      { 
    645         $content = $this->context->get('sf_web_debug')->decorateContentWithDebug($uri, $this->context->getResponse()->getContent(), false); 
     645        $content = sfWebDebug::decorateContentWithDebug($uri, $this->context->getResponse()->getContent(), false); 
    646646        $this->context->getResponse()->setContent($content); 
    647647      } 
  • trunk/test/functional/fixtures/project/apps/cache/config/filters.yml

    r4934 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43 
  • trunk/test/functional/fixtures/project/apps/frontend/config/filters.yml

    r4934 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43 
  • trunk/test/functional/fixtures/project/apps/frontend/modules/configFiltersSimpleFilter/config/filters.yml

    r4934 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43 
  • trunk/test/functional/fixtures/project/apps/i18n/config/filters.yml

    r4934 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43 
  • trunk/test/functional/fixtures/project/config/filters.yml

    r4934 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43 
  • trunk/test/functional/fixtures/project/plugins/sfConfigPlugin/config/filters.yml

    r4934 r6509  
    11rendering: ~ 
    2 web_debug: ~ 
    32security:  ~ 
    43