Development

Changeset 11089

You must first sign up to be able to contribute.

Changeset 11089

Show
Ignore:
Timestamp:
08/25/08 08:24:38 (10 months ago)
Author:
fabien
Message:

[1.2] added a sfWebResponse::ALL constant to be able to retrieve all javascripts and stylesheets without the position

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/UPGRADE_TO_1_2

    r11083 r11089  
    5454charset for the response. This charset is automatically updated if 
    5555you change it by setting the content type. 
     56 
     57The `getStylesheets()` and `getJavascripts()` methods now return all the 
     58stylesheets and javascripts ordered by position if you pass `sfWebResponse::ALL` 
     59as their first argument: 
     60 
     61    [php] 
     62    $response = new sfWebResponse(new sfEventDispatcher()); 
     63    $response->addStylesheet('foo.css'); 
     64    $response->addStylesheet('bar.css', 'first'); 
     65 
     66    var_export($response->getStylesheets(sfWebResponse::ALL)); 
     67 
     68    // outputs 
     69    array( 
     70      'bar.css' => array(), 
     71      'foo.css' => array(), 
     72    ) 
     73 
     74The old behavior is still available by passing the `sfWebResponse::RAW` position: 
     75 
     76    [php] 
     77    var_export($response->getStylesheets(sfWebResponse::RAW)); 
     78 
     79    // outputs 
     80    array( 
     81      'first' => 
     82        array( 
     83          'bar.css' => array (), 
     84        ), 
     85      '' => 
     86        array( 
     87        'foo.css' => array(), 
     88        ), 
     89      'last' => array(), 
     90    ) 
     91 
     92All the positions (first, '', and last) are now also available as constants: 
     93 
     94    [php] 
     95    sfWebResponse::FIRST  === 'first' 
     96    sfWebResponse::MIDDLE === '' 
     97    sfWebResponse::LAST   === 'last' 
    5698 
    5799Validators 
     
    141183    $yaml = new sfYamlParser(); 
    142184 
    143     print_r($yaml->parse(<<<EOF 
     185    var_export($yaml->parse(<<<EOF 
    144186    default_param: &default_param 
    145187      datasource: propel 
     
    157199    )); 
    158200 
    159     // displays 
    160     Array 
    161     ( 
    162         [default_param] => Array 
    163             ( 
    164                 [datasource] => propel 
    165                 [phptype]    => mysql 
    166                 [hostspec]   => localhost 
    167                 [database]   => db 
    168                 [username]   => user 
    169                 [password]   => pass 
    170             ) 
    171  
    172         [param] => Array 
    173             ( 
    174                 [datasource] => propel 
    175                 [phptype]    => mysql 
    176                 [hostspec]   => localhost 
    177                 [database]   => db 
    178                 [username]   => myuser 
    179                 [password]   => mypass 
    180             ) 
    181  
     201    // outputs 
     202    array( 
     203      'default_param' => array( 
     204        'datasource' => 'propel', 
     205        'phptype'    => 'mysql', 
     206        'hostspec'   => 'localhost', 
     207        'database'   => 'db', 
     208        'username'   => 'user', 
     209        'password'   => 'pass', 
     210      ) 
     211 
     212      'param' => array( 
     213        'datasource' => 'propel', 
     214        'phptype'    => 'mysql', 
     215        'hostspec'   => 'localhost', 
     216        'database'   => 'db', 
     217        'username'   => 'myuser', 
     218        'password'   => 'mypass', 
     219      ) 
    182220    ) 
    183221 
     
    228266    $log->log('foo'); 
    229267 
    230     print_r($log->getLogs()); 
     268    var_export($log->getLogs()); 
    231269 
    232270    // outputs 
    233     Array( 
    234       [0] => Array( 
    235         [priority]      => 6 
    236         [priority_name] => info 
    237         [time]          => 1219385295 
    238         [message]       => foo 
    239         [type]          => sfOther 
    240         [debug_stack]   => Array() 
     271    array( 
     272      0 => array( 
     273        'priority'      => 6, 
     274        'priority_name' => 'info', 
     275        'time'          => 1219385295, 
     276        'message'       => 'foo', 
     277        'type'          => 'sfOther', 
     278        'debug_stack'   => array() 
    241279      ) 
    242280    ) 
  • branches/1.2/lib/helper/AssetHelper.php

    r9101 r11089  
    461461  sfConfig::set('symfony.asset.javascripts_included', true); 
    462462 
    463   $already_seen = array(); 
    464463  $html = ''; 
    465  
    466   foreach ($response->getPositions() as $position) 
    467   { 
    468     foreach ($response->getJavascripts($position) as $files => $options) 
    469     { 
    470       if (!is_array($files)) 
    471       { 
    472         $files = array($files); 
    473       } 
    474  
    475       foreach ($files as $file) 
    476       { 
    477         if (isset($already_seen[$file])) continue; 
    478  
    479         $already_seen[$file] = 1; 
    480         $html .= javascript_include_tag($file, $options); 
    481       } 
    482     } 
     464  foreach ($response->getJavascripts(sfWebResponse::ALL) as $file => $options) 
     465  { 
     466    $html .= javascript_include_tag($file, $options); 
    483467  } 
    484468 
     
    510494  sfConfig::set('symfony.asset.stylesheets_included', true); 
    511495 
    512   $already_seen = array(); 
    513496  $html = ''; 
    514  
    515   foreach ($response->getPositions() as $position) 
    516   { 
    517     foreach ($response->getStylesheets($position) as $files => $options) 
    518     { 
    519       if (!is_array($files)) 
    520       { 
    521         $files = array($files); 
    522       } 
    523  
    524       foreach ($files as $file) 
    525       { 
    526         if (isset($already_seen[$file])) continue; 
    527  
    528         $already_seen[$file] = 1; 
    529         $html .= stylesheet_tag($file, $options); 
    530       } 
    531     } 
     497  foreach ($response->getStylesheets(sfWebResponse::ALL) as $file => $options) 
     498  { 
     499    $html .= stylesheet_tag($file, $options); 
    532500  } 
    533501 
  • branches/1.2/lib/response/sfWebResponse.class.php

    r11059 r11089  
    2121class sfWebResponse extends sfResponse 
    2222{ 
     23  const 
     24    FIRST  = 'first', 
     25    MIDDLE = '', 
     26    LAST   = 'last', 
     27    ALL    = 'ALL', 
     28    RAW    = 'RAW'; 
     29 
    2330  protected 
    2431    $cookies     = array(), 
     
    578585   * Retrieves stylesheets for the current web response. 
    579586   * 
    580    * @param  string  $position 
    581    * 
    582    * @return string Stylesheets 
     587   * If you pass sfWebResponse::ALL as the position, 
     588   * the methods returns all stylesheets ordered by position. 
     589   * 
     590   * @param  string  $position The position 
     591   * 
     592   * @return array   An associative array of javascript files as keys and options as values 
    583593   */ 
    584594  public function getStylesheets($position = '') 
    585595  { 
    586     if ($position == 'ALL') 
     596    if (sfWebResponse::ALL === $position) 
     597    { 
     598      $stylesheets = array(); 
     599      foreach ($this->getPositions() as $position) 
     600      { 
     601        foreach ($this->stylesheets[$position] as $file => $options) 
     602        { 
     603          $stylesheets[$file] = $options; 
     604        } 
     605      } 
     606 
     607      return $stylesheets; 
     608    } 
     609    else if (sfWebResponse::RAW === $position) 
    587610    { 
    588611      return $this->stylesheets; 
     
    591614    $this->validatePosition($position); 
    592615 
    593     return isset($this->stylesheets[$position]) ? $this->stylesheets[$position] : array()
     616    return $this->stylesheets[$position]
    594617  } 
    595618 
     
    597620   * Adds a stylesheet to the current web response. 
    598621   * 
    599    * @param string $css       Stylesheet 
     622   * @param string $file      The stylesheet file 
    600623   * @param string $position  Position 
    601624   * @param string $options   Stylesheet options 
    602625   */ 
    603   public function addStylesheet($css, $position = '', $options = array()) 
     626  public function addStylesheet($file, $position = '', $options = array()) 
    604627  { 
    605628    $this->validatePosition($position); 
    606629 
    607     $this->stylesheets[$position][$css] = $options; 
     630    $this->stylesheets[$position][$file] = $options; 
    608631  } 
    609632 
     
    611634   * Removes a stylesheet from the current web response. 
    612635   * 
    613    * @param string $css       Stylesheet 
     636   * @param string $css       The stylesheet file 
    614637   * @param string $position  Position 
    615638   */ 
    616   public function removeStylesheet($css, $position = '') 
     639  public function removeStylesheet($file, $position = '') 
    617640  { 
    618641    $this->validatePosition($position); 
    619642 
    620     unset($this->stylesheets[$position][$css]); 
    621   } 
    622  
    623   /** 
    624    * Retrieves javascript code from the current web response. 
    625    * 
    626    * @param  string $position  Position 
    627    * 
    628    * @return string Javascript code 
     643    unset($this->stylesheets[$position][$file]); 
     644  } 
     645 
     646  /** 
     647   * Retrieves javascript files from the current web response. 
     648   * 
     649   * If you pass sfWebResponse::ALL as the position, 
     650   * the methods returns all javascripts ordered by position. 
     651   * 
     652   * @param  string $position  The position 
     653   * 
     654   * @return array An associative array of javascript files as keys and options as values 
    629655   */ 
    630656  public function getJavascripts($position = '') 
    631657  { 
    632     if ($position == 'ALL') 
     658    if (sfWebResponse::ALL === $position) 
     659    { 
     660      $javascripts = array(); 
     661      foreach ($this->getPositions() as $position) 
     662      { 
     663        foreach ($this->javascripts[$position] as $file => $options) 
     664        { 
     665          $javascripts[$file] = $options; 
     666        } 
     667      } 
     668 
     669      return $javascripts; 
     670    } 
     671    else if (sfWebResponse::RAW === $position) 
    633672    { 
    634673      return $this->javascripts; 
     
    637676    $this->validatePosition($position); 
    638677 
    639     return isset($this->javascripts[$position]) ? $this->javascripts[$position] : array()
     678    return $this->javascripts[$position]
    640679  } 
    641680 
     
    643682   * Adds javascript code to the current web response. 
    644683   * 
    645    * @param string $js        Javascript cod
     684   * @param string $gile      The JavaScript fil
    646685   * @param string $position  Position 
    647686   * @param string $options   Javascript options 
    648687   */ 
    649   public function addJavascript($js, $position = '', $options = array()) 
     688  public function addJavascript($file, $position = '', $options = array()) 
    650689  { 
    651690    $this->validatePosition($position); 
    652691 
    653     $this->javascripts[$position][$js] = $options; 
    654   } 
    655  
    656   /** 
    657    * Removes javascript code from the current web response. 
    658    * 
    659    * @param string $js        Javascript cod
     692    $this->javascripts[$position][$file] = $options; 
     693  } 
     694 
     695  /** 
     696   * Removes a JavaScript file from the current web response. 
     697   * 
     698   * @param string $file      The Javascript fil
    660699   * @param string $position  Position 
    661700   */ 
    662   public function removeJavascript($js, $position = '') 
     701  public function removeJavascript($file, $position = '') 
    663702  { 
    664703    $this->validatePosition($position); 
    665704 
    666     unset($this->javascripts[$position][$js]); 
     705    unset($this->javascripts[$position][$file]); 
    667706  } 
    668707 
     
    727766    $this->metas       = $response->getMetas(); 
    728767    $this->httpMetas   = $response->getHttpMetas(); 
    729     $this->stylesheets = $response->getStylesheets('ALL'); 
    730     $this->javascripts = $response->getJavascripts('ALL'); 
     768    $this->stylesheets = $response->getStylesheets(sfWebResponse::RAW); 
     769    $this->javascripts = $response->getJavascripts(sfWebResponse::RAW); 
    731770    $this->slots       = $response->getSlots(); 
    732771  } 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.