Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "performance"

Add inline javascript to the response

Allows you to define a script block in a template, and let it be included by the include_javascripts() helper after all js files, so that you can put it right before the body tag , to conform to the Yahoo performance best practice (see http://developer.yahoo.com/performance/rules.html#js_bottom).

First put this class in your /lib/response directory:

<?php
/*
*  LfmWebResponse
* @package    forcemolle
* @subpackage response
* @author     Jules Bernable <julius@laforcemolle.org>
*
*/
class LfmWebResponse extends sfWebResponse
{
  protected
    $inline_scripts = array();
 
  public function initialize(sfEventDispatcher $dispatcher, $options = array())
  {
    parent::initialize($dispatcher, $options);
 
    $this->inline_scripts = array_combine($this->positions, array_fill(0, count($this->positions), array()));
  }
 
  /**
   * Adds a script block to the current web response.
   *
   * @param string $script    The JavaScript code
   * @param string $id        A unique identifier for the script
   * @param string $position  Position
   */
  public function addInlineScript($script='', $position=sfWebResponse::LAST)
  {
    $this->validatePosition($position);
 
    $this->inline_scripts[$position][] = $script;
  }
 
  /**
   * Retrieves inline scripts from the current web response.
   *
   * By default, the position is sfWebResponse::ALL,
   * and the method returns all inline scripts ordered by position.
   *
   * @param  string $position  The position
   *
   * @return array An array of inline scripts
   */
  public function getInlineScripts($position = self::ALL)
  {
    if (self::ALL === $position)
    {
      $inline_scripts = array();
      foreach ($this->getPositions() as $position)
      {
        foreach ($this->inline_scripts[$position] as $script)
        {
          $inline_scripts[] = $script;
        }
      }
      return $inline_scripts;
    }
    else if (self::RAW === $position)
    {
      return $this->inline_scripts;
    }
 
    $this->validatePosition($position);
 
    return $this->inline_scripts[$position];
  }
 
}
 

Update your $sf_app_dir/config/factories.yml accordingly:

all:
  response:
    class: LfmWebResponse
 

Then you need to overwrite the symfony AssetHelper. First copy $sf_symfony_lib_dir/helper/AssetHelper.php to $sf_app_dir/lib/helper/AssetHelper.php (or to $sf_lib_dir/helper/AssetHelper.php if you want it enabled globally). Then replace the get_javascripts() function (line 492) by the following:

function get_javascripts()
{
  $response = sfContext::getInstance()->getResponse();
  sfConfig::set('symfony.asset.javascripts_included', true);
 
  $html = '';
  foreach ($response->getJavascripts() as $file => $options)
  {
    $html .= javascript_include_tag($file, $options);
  }
 
  use_helper('JavascriptBase');
  foreach ($response->getInlineScripts() as $script)
  {
    $html .= content_tag('script', javascript_cdata_section($script), array('type' => 'text/javascript'));
  }
 
  return $html;
}
 

Et voilà ! You can now add a script in a template this way:

<?php $sf_response->addInlineScript(<<<EOF
// Mootools FTW!
window.addEvent('domready', function()
{
  var foo = alert('BAR!');
});
EOF
);
?>
 

Your script block is now added after all js files added by include_javascripts() !

Please note that this is a basic implementation. You can not 'override' a script nor remove it afterwards.

by Jules Bernable on 2010-03-07, tagged javascript  performance  response 
(3 comments)

Using timers to profile functions speed

This snippet allows you to evaluate functions speed and display them in symfony debug bar.

$timer = sfTimerManager::getTimer ('ldap_request'); // Create and start a new timer
 
// do some stuff
 
$timer->addTime (); // stop and store the timer value
 
by Arnaud Didry on 2009-06-22, tagged performance  profile  speed 
(2 comments)