Development

Changeset 5018

You must first sign up to be able to contribute.

Changeset 5018

Show
Ignore:
Timestamp:
09/09/07 18:12:34 (1 year ago)
Author:
fabien
Message:

removed context dependency for sf*ViewParameterHolder?, added a new template.filter_parameters event

Files:

Legend:

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

    r4961 r5018  
    5555    } 
    5656 
     57    $this->dispatcher->connect('template.filter_parameters', array($this, 'filterTemplateParameters')); 
     58 
    5759    // register our shutdown function 
    5860    register_shutdown_function(array($this, 'shutdown')); 
     
    358360 
    359361  /** 
     362   * Listens to the template.filter_parameters event. 
     363   * 
     364   * @param  sfEvent An sfEvent instance 
     365   * @param  array   An array of template parameters to filter 
     366   * 
     367   * @return array   The filtered parameters array 
     368   */ 
     369  public function filterTemplateParameters(sfEvent $event, $parameters) 
     370  { 
     371    $parameters['sf_context']  = $this; 
     372    $parameters['sf_request']  = $this->factories['request']; 
     373    $parameters['sf_params']   = $this->factories['request']->getParameterHolder(); 
     374    $parameters['sf_response'] = $this->factories['response']; 
     375    $parameters['sf_user']     = $this->factories['user']; 
     376 
     377    return $parameters; 
     378  } 
     379 
     380  /** 
    360381   * Execute the shutdown procedure. 
    361382   * 
  • trunk/lib/view/sfEscapedViewParameterHolder.class.php

    r4898 r5018  
    2828   * Initializes this view parameter holder. 
    2929   * 
    30    * @param  sfContext A sfContext instance. 
    31    * @param  array     An associative array of initialization parameters. 
    32    * @param  array     An associative array of options. 
     30   * @param  sfEventDispatcher A sfEventDispatcher instance. 
     31   * @param  array             An associative array of initialization parameters. 
     32   * @param  array             An associative array of options. 
    3333   * 
    3434   * <b>Options:</b> 
     
    4141   * @throws <b>sfInitializationException</b> If an error occurs while initializing this view parameter holder. 
    4242   */ 
    43   public function initialize($context, $parameters = array(), $options = array()) 
     43  public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $options = array()) 
    4444  { 
    45     parent::initialize($context, $parameters, $options); 
     45    parent::initialize($dispatcher, $parameters, $options); 
    4646 
    4747    $this->setEscaping(isset($options['escaping_strategy']) ? $options['escaping_strategy'] : 'bc'); 
  • trunk/lib/view/sfView.class.php

    r5016 r5018  
    120120 
    121121    $this->attributeHolder = false === sfConfig::get('sf_escaping_method') ? new sfViewParameterHolder() : new sfEscapedViewParameterHolder(); 
    122     $this->attributeHolder->initialize($context, array(), array( 
     122    $this->attributeHolder->initialize($this->dispatcher, array(), array( 
    123123      'escaping_method'   => sfConfig::get('sf_escaping_method'), 
    124       'escaping_strategy' => sfConfig::get('sf_escaping_strategy') 
     124      'escaping_strategy' => sfConfig::get('sf_escaping_strategy'), 
    125125    )); 
    126126 
  • trunk/lib/view/sfViewParameterHolder.class.php

    r5016 r5018  
    2020{ 
    2121  protected 
    22     $context = null; 
     22    $dispatcher = null; 
    2323 
    2424  /** 
    2525   * Initializes this view parameter holder. 
    2626   * 
    27    * @param sfContext A sfContext instance. 
    28    * @param array     An associative array of initialization parameters. 
    29    * @param array     An associative array of options. 
     27   * @param sfEventDispatcher A sfEventDispatcher instance. 
     28   * @param array             An associative array of initialization parameters. 
     29   * @param array             An associative array of options. 
    3030   * 
    3131   * @return Boolean  true, if initialization completes successfully, otherwise false. 
     
    3333   * @throws <b>sfInitializationException</b> If an error occurs while initializing this view parameter holder. 
    3434   */ 
    35   public function initialize($context, $parameters = array(), $options = array()) 
     35  public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $options = array()) 
    3636  { 
    37     $this->context = $context
     37    $this->dispatcher = $dispatcher
    3838 
    39     $this->add($this->getGlobalAttributes()); 
     39    $event = $dispatcher->filter(new sfEvent($this, 'template.filter_parameters'), $parameters); 
     40    $parameters = $event->getReturnValue(); 
     41 
    4042    $this->add($parameters); 
    4143  } 
     
    4951  { 
    5052    return $this->getAll(); 
    51   } 
    52  
    53   /** 
    54    * Returns view attributes accessible for every view. 
    55    * 
    56    * @return array An array of view attributes 
    57    */ 
    58   protected function getGlobalAttributes() 
    59   { 
    60     $attributes = array( 
    61       'sf_context'  => $this->context, 
    62       'sf_params'   => $this->context->getRequest()->getParameterHolder(), 
    63       'sf_request'  => $this->context->getRequest(), 
    64       'sf_response' => $this->context->getResponse(), 
    65       'sf_user'     => $this->context->getUser(), 
    66     ); 
    67  
    68     return $attributes; 
    6953  } 
    7054 
     
    8468      } 
    8569    } 
    86     $tmp->context = null; 
     70    $tmp->dispatcher = null; 
    8771 
    88     return serialize($tmp->parameters); 
     72    return serialize($tmp->getAll()); 
    8973  } 
    9074 
     
    9680    parent::unserialize($serialized); 
    9781 
    98     $this->context = sfContext::getInstance(); 
    99     $this->add($this->getGlobalAttributes()); 
     82    $this->initialize(sfContext::hasInstance() ? sfContext::getInstance()->getEventDispatcher() : new sfEventDispatcher()); 
    10083  } 
    10184} 
  • trunk/test/unit/sfContextMock.class.php

    r4957 r5018  
    3939 
    4040    return self::$instance; 
     41  } 
     42 
     43  static public function hasInstance() 
     44  { 
     45    return true; 
    4146  } 
    4247 
  • trunk/test/unit/view/sfEscapedViewParameterHolderTest.php

    r4898 r5018  
    2626} 
    2727 
    28 class myUser 
    29 { 
    30   public function getAttributeHolder() 
    31   { 
    32     return new sfParameterHolder(); 
    33   } 
    34 } 
    35  
    3628class myRequest 
    3729{ 
     
    4234} 
    4335 
     36class FilterParameters 
     37{ 
     38  static public $context = null; 
     39 
     40  static function filter($event, $parameters) 
     41  { 
     42    $parameters['sf_request'] = self::$context->request; 
     43 
     44    return $parameters; 
     45  } 
     46} 
     47 
    4448$context = sfContext::getInstance(array( 
    45   'user'    => 'myUser', 
    4649  'request' => 'myRequest', 
    4750)); 
     51$dispatcher = $context->dispatcher; 
     52 
     53FilterParameters::$context = $context; 
     54$dispatcher->connect('template.filter_parameters', array('FilterParameters', 'filter')); 
    4855 
    4956// ->initialize() 
    5057$t->diag('->initialize()'); 
    5158$p = new sfEscapedViewParameterHolder(); 
    52 $p->initialize($context); 
     59$p->initialize($dispatcher); 
    5360$t->is($p->get('sf_user'), $context->user, '->initialize() add some symfony shortcuts as parameters'); 
    5461$t->is($p->get('sf_request'), $context->request, '->initialize() add some symfony shortcuts as parameters'); 
    5562$t->is($p->get('sf_response'), $context->response, '->initialize() add some symfony shortcuts as parameters'); 
    5663 
    57 $p->initialize($context, array('foo' => 'bar')); 
     64$p->initialize($dispatcher, array('foo' => 'bar')); 
    5865$t->is($p->get('foo'), 'bar', '->initialize() takes an array of default parameters as its second argument'); 
    5966 
    60 $p->initialize($context, array(), array('escaping_strategy' => 'on', 'escaping_method' => 'ESC_RAW')); 
     67$p->initialize($dispatcher, array(), array('escaping_strategy' => 'on', 'escaping_method' => 'ESC_RAW')); 
    6168$t->is($p->getEscaping(), 'on', '->initialize() takes an array of options as its third argument'); 
    6269$t->is($p->getEscapingMethod(), ESC_RAW, '->initialize() takes an array of options as its third argument'); 
     
    6471// ->getEscaping() ->setEscaping() 
    6572$t->diag('->getEscaping() ->setEscaping()'); 
    66 $p->initialize($context); 
     73$p->initialize($dispatcher); 
    6774$p->setEscaping('on'); 
    6875$t->is($p->getEscaping(), 'on', '->setEscaping() changes the escaping strategy'); 
     
    7582// ->toArray() 
    7683$t->diag('->toArray()'); 
    77 $p->initialize($context, array('foo' => 'bar')); 
     84$p->initialize($dispatcher, array('foo' => 'bar')); 
    7885$a = $p->toArray(); 
    7986$t->is($a['foo'], 'bar', '->toArray() returns an array representation of the parameter holder'); 
     
    8188// ->serialize() / ->unserialize() 
    8289$t->diag('->serialize() / ->unserialize()'); 
    83 $p->initialize($context, array('foo' => 'bar')); 
     90$p->initialize($dispatcher, array('foo' => 'bar')); 
    8491$unserialized = unserialize(serialize($p)); 
    8592$t->is($p->toArray(), $unserialized->toArray(), 'sfEscapedViewParameterHolder implements the Serializable interface'); 
  • trunk/test/unit/view/sfViewParameterHolderTest.php

    r4898 r5018  
    1212require_once($_test_dir.'/unit/sfContextMock.class.php'); 
    1313 
    14 $t = new lime_test(6, new lime_output_color()); 
    15  
    16 class myUser 
    17 
    18   public function getAttributeHolder() 
    19   { 
    20     return new sfParameterHolder(); 
    21   } 
    22 
     14$t = new lime_test(4, new lime_output_color()); 
    2315 
    2416class myRequest 
     
    3022} 
    3123 
    32 $context = sfContext::getInstance(array( 
    33   'user'    => 'myUser', 
    34   'request' => 'myRequest', 
    35 )); 
     24class FilterParameters 
     25
     26  static public $context = null; 
     27 
     28  static function filter($event, $parameters) 
     29  { 
     30    $parameters['sf_request'] = self::$context->request; 
     31 
     32    return $parameters; 
     33  } 
     34
     35 
     36$context = sfContext::getInstance(array('request' => 'myRequest')); 
     37$dispatcher = $context->dispatcher; 
     38 
     39FilterParameters::$context = $context; 
     40$dispatcher->connect('template.filter_parameters', array('FilterParameters', 'filter')); 
    3641 
    3742// ->initialize() 
    3843$t->diag('->initialize()'); 
    3944$p = new sfViewParameterHolder(); 
    40 $p->initialize($context); 
    41 $t->is($p->get('sf_user'), $context->user, '->initialize() add some symfony shortcuts as parameters'); 
    42 $t->is($p->get('sf_request'), $context->request, '->initialize() add some symfony shortcuts as parameters'); 
    43 $t->is($p->get('sf_response'), $context->response, '->initialize() add some symfony shortcuts as parameters'); 
    44  
    45 $p->initialize($context, array('foo' => 'bar')); 
     45$p->initialize($dispatcher, array('foo' => 'bar')); 
    4646$t->is($p->get('foo'), 'bar', '->initialize() takes an array of default parameters as its second argument'); 
    4747 
    4848// ->toArray() 
    4949$t->diag('->toArray()'); 
    50 $p->initialize($context, array('foo' => 'bar')); 
     50$p->initialize($dispatcher, array('foo' => 'bar')); 
    5151$a = $p->toArray(); 
    5252$t->is($a['foo'], 'bar', '->toArray() returns an array representation of the parameter holder'); 
     
    5454// ->serialize() / ->unserialize() 
    5555$t->diag('->serialize() / ->unserialize()'); 
    56 $p->initialize($context, array('foo' => 'bar')); 
     56$p->initialize($dispatcher, array('foo' => 'bar')); 
    5757$unserialized = unserialize(serialize($p)); 
    5858$t->is($p->toArray(), $unserialized->toArray(), 'sfViewParameterHolder implements the Serializable interface'); 
     59 
     60// template.filter_parameters 
     61$p = new sfViewParameterHolder(); 
     62$p->initialize($dispatcher); 
     63$t->is($p->get('sf_request'), $context->request, '->initialize() add some symfony shortcuts as parameters');