Development

Changeset 4898

You must first sign up to be able to contribute.

Changeset 4898

Show
Ignore:
Timestamp:
08/25/07 16:03:57 (1 year ago)
Author:
fabien
Message:

sfViewParameterHolder tweaks

  • removed sfConfig dependency
  • fixed some phpdoc
  • fixed serialization / unserialization
  • added unit tests for sfViewParameterHolder and sfEscapedViewParameterHolder
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/util/sfParameterHolder.class.php

    r4645 r4898  
    247247   * @return string A parameter value, if the parameter was removed, otherwise null. 
    248248   */ 
    249   public function & remove($name, $ns = null) 
     249  public function remove($name, $ns = null) 
    250250  { 
    251251    if (!$ns) 
     
    256256    $retval = null; 
    257257 
    258     if (isset($this->parameters[$ns]) && isset($this->parameters[$ns][$name])) 
    259     { 
    260       $retval =& $this->parameters[$ns][$name]; 
     258    if (isset($this->parameters[$ns]) && array_key_exists($name, $this->parameters[$ns])) 
     259    { 
     260      $retval = $this->parameters[$ns][$name]; 
    261261      unset($this->parameters[$ns][$name]); 
    262262    } 
  • trunk/lib/view/sfEscapedViewParameterHolder.class.php

    r4562 r4898  
    1010 
    1111/** 
    12  * A sfEscapedViewParameterHolder stores all variables that will be available to the template. 
     12 * sfEscapedViewParameterHolder stores all variables that will be available to the template. 
    1313 * 
    14  * It also escape all variables with an escaping method. 
     14 * It also escapes variables with an escaping method. 
    1515 * 
    1616 * @package    symfony 
     
    2828   * Initializes this view parameter holder. 
    2929   * 
    30    * @param sfContext A sfContext instance. 
    31    * @param array     An associative array of initialization parameters. 
     30   * @param  sfContext A sfContext instance. 
     31   * @param  array     An associative array of initialization parameters. 
     32   * @param  array     An associative array of options. 
    3233   * 
    33    * @return bool true, if initialization completes successfully, otherwise false. 
     34   * <b>Options:</b> 
     35   * 
     36   * # <b>escaping_strategy</b> - [bc]           - The escaping strategy (bc, both, on or off) 
     37   * # <b>escaping_method</b>   - [ESC_ENTITIES] - The escaping method (ESC_RAW, ESC_ENTITIES, ESC_JS or ESC_JS_NO_ENTITIES) 
     38   * 
     39   * @return Boolean   true, if initialization completes successfully, otherwise false. 
    3440   * 
    3541   * @throws <b>sfInitializationException</b> If an error occurs while initializing this view parameter holder. 
    3642   */ 
    37   public function initialize($context, $parameters = array()
     43  public function initialize($context, $parameters = array(), $options = array()
    3844  { 
    39     parent::initialize($context, $parameters); 
     45    parent::initialize($context, $parameters, $options); 
    4046 
    41     $this->setEscaping(sfConfig::get('sf_escaping_strategy')); 
    42     $this->setEscapingMethod(sfConfig::get('sf_escaping_method')); 
     47    $this->setEscaping(isset($options['escaping_strategy']) ? $options['escaping_strategy'] : 'bc'); 
     48    $this->setEscapingMethod(isset($options['escaping_method']) ? $options['escaping_method'] : 'ESC_ENTITIES'); 
    4349  } 
    4450 
     51  /** 
     52   * Returns an array representation of the view parameters. 
     53   * 
     54   * @return array An array of view parameters 
     55   */ 
    4556  public function toArray() 
    4657  { 
     
    110121    if (!defined($this->escapingMethod)) 
    111122    { 
    112       throw new sfConfigurationException(sprintf('Escaping method "%s" is not available; perhaps another helper needs to be loaded in?', $this->escapingMethod)); 
     123      throw new sfConfigurationException(sprintf('The escaping method "%s" is not available.', $this->escapingMethod)); 
    113124    } 
    114125 
     
    125136    $this->escapingMethod = $method; 
    126137  } 
     138 
     139  /** 
     140   * Serializes the current instance. 
     141   * 
     142   * @return array Objects instance 
     143   */ 
     144  public function serialize() 
     145  { 
     146    $this->set('_sf_escaping_method', $this->escapingMethod); 
     147    $this->set('_sf_escaping', $this->escaping); 
     148 
     149    $serialized = parent::serialize(); 
     150 
     151    $this->remove('_sf_escaping_method'); 
     152    $this->remove('_sf_escaping'); 
     153 
     154    return $serialized; 
     155  } 
     156 
     157  /** 
     158   * Unserializes a sfViewParameterHolder instance. 
     159   */ 
     160  public function unserialize($serialized) 
     161  { 
     162    parent::unserialize($serialized); 
     163 
     164    $this->setEscapingMethod($this->remove('_sf_escaping_method')); 
     165    $this->setEscaping($this->remove('_sf_escaping')); 
     166  } 
    127167} 
  • trunk/lib/view/sfView.class.php

    r4597 r4898  
    177177 
    178178    $this->attributeHolder = false === sfConfig::get('sf_escaping_method') ? new sfViewParameterHolder() : new sfEscapedViewParameterHolder(); 
    179     $this->attributeHolder->initialize($context); 
     179    $this->attributeHolder->initialize($context, array('escaping_method' => sfConfig::get('sf_escaping_method'), 'escaping_strategy' => sfConfig::get('sf_escaping_strategy'))); 
    180180 
    181181    $this->parameterHolder = new sfParameterHolder(); 
  • trunk/lib/view/sfViewParameterHolder.class.php

    r4562 r4898  
    2727   * @param sfContext A sfContext instance. 
    2828   * @param array     An associative array of initialization parameters. 
     29   * @param array     An associative array of options. 
    2930   * 
    30    * @return bool true, if initialization completes successfully, otherwise false. 
     31   * @return Boolean true, if initialization completes successfully, otherwise false. 
    3132   * 
    3233   * @throws <b>sfInitializationException</b> If an error occurs while initializing this view parameter holder. 
    3334   */ 
    34   public function initialize($context, $parameters = array()
     35  public function initialize($context, $parameters = array(), $options = array()
    3536  { 
    3637    $this->context = $context; 
     
    4041  } 
    4142 
     43  /** 
     44   * Returns an array representation of the view parameters. 
     45   * 
     46   * @return array An array of view parameters 
     47   */ 
    4248  public function toArray() 
    4349  { 
     
    5258  protected function getGlobalAttributes() 
    5359  { 
     60    $flash = new sfParameterHolder(); 
     61    $flash->add($this->context->getUser()->getAttributeHolder()->getAll('symfony/flash')); 
     62 
    5463    $attributes = array( 
    5564      'sf_context'  => $this->context, 
     
    5867      'sf_response' => $this->context->getResponse(), 
    5968      'sf_user'     => $this->context->getUser(), 
     69      'sf_flash'    => $flash, 
    6070    ); 
    61  
    62     if (sfConfig::get('sf_use_flash')) 
    63     { 
    64       $sf_flash = new sfParameterHolder(); 
    65       $sf_flash->add($this->context->getUser()->getAttributeHolder()->getAll('symfony/flash')); 
    66       $attributes['sf_flash'] = $sf_flash; 
    67     } 
    6871 
    6972    return $attributes;