Changeset 5018
- Timestamp:
- 09/09/07 18:12:34 (1 year ago)
- Files:
-
- trunk/lib/util/sfContext.class.php (modified) (2 diffs)
- trunk/lib/view/sfEscapedViewParameterHolder.class.php (modified) (2 diffs)
- trunk/lib/view/sfView.class.php (modified) (1 diff)
- trunk/lib/view/sfViewParameterHolder.class.php (modified) (5 diffs)
- trunk/test/unit/sfContextMock.class.php (modified) (1 diff)
- trunk/test/unit/view/sfEscapedViewParameterHolderTest.php (modified) (5 diffs)
- trunk/test/unit/view/sfViewParameterHolderTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/util/sfContext.class.php
r4961 r5018 55 55 } 56 56 57 $this->dispatcher->connect('template.filter_parameters', array($this, 'filterTemplateParameters')); 58 57 59 // register our shutdown function 58 60 register_shutdown_function(array($this, 'shutdown')); … … 358 360 359 361 /** 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 /** 360 381 * Execute the shutdown procedure. 361 382 * trunk/lib/view/sfEscapedViewParameterHolder.class.php
r4898 r5018 28 28 * Initializes this view parameter holder. 29 29 * 30 * @param sf Context A sfContextinstance.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. 33 33 * 34 34 * <b>Options:</b> … … 41 41 * @throws <b>sfInitializationException</b> If an error occurs while initializing this view parameter holder. 42 42 */ 43 public function initialize( $context, $parameters = array(), $options = array())43 public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $options = array()) 44 44 { 45 parent::initialize($ context, $parameters, $options);45 parent::initialize($dispatcher, $parameters, $options); 46 46 47 47 $this->setEscaping(isset($options['escaping_strategy']) ? $options['escaping_strategy'] : 'bc'); trunk/lib/view/sfView.class.php
r5016 r5018 120 120 121 121 $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( 123 123 'escaping_method' => sfConfig::get('sf_escaping_method'), 124 'escaping_strategy' => sfConfig::get('sf_escaping_strategy') 124 'escaping_strategy' => sfConfig::get('sf_escaping_strategy'), 125 125 )); 126 126 trunk/lib/view/sfViewParameterHolder.class.php
r5016 r5018 20 20 { 21 21 protected 22 $ context= null;22 $dispatcher = null; 23 23 24 24 /** 25 25 * Initializes this view parameter holder. 26 26 * 27 * @param sfContext A sfContextinstance.28 * @param arrayAn associative array of initialization parameters.29 * @param arrayAn 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. 30 30 * 31 31 * @return Boolean true, if initialization completes successfully, otherwise false. … … 33 33 * @throws <b>sfInitializationException</b> If an error occurs while initializing this view parameter holder. 34 34 */ 35 public function initialize( $context, $parameters = array(), $options = array())35 public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $options = array()) 36 36 { 37 $this-> context = $context;37 $this->dispatcher = $dispatcher; 38 38 39 $this->add($this->getGlobalAttributes()); 39 $event = $dispatcher->filter(new sfEvent($this, 'template.filter_parameters'), $parameters); 40 $parameters = $event->getReturnValue(); 41 40 42 $this->add($parameters); 41 43 } … … 49 51 { 50 52 return $this->getAll(); 51 }52 53 /**54 * Returns view attributes accessible for every view.55 *56 * @return array An array of view attributes57 */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;69 53 } 70 54 … … 84 68 } 85 69 } 86 $tmp-> context= null;70 $tmp->dispatcher = null; 87 71 88 return serialize($tmp-> parameters);72 return serialize($tmp->getAll()); 89 73 } 90 74 … … 96 80 parent::unserialize($serialized); 97 81 98 $this->context = sfContext::getInstance(); 99 $this->add($this->getGlobalAttributes()); 82 $this->initialize(sfContext::hasInstance() ? sfContext::getInstance()->getEventDispatcher() : new sfEventDispatcher()); 100 83 } 101 84 } trunk/test/unit/sfContextMock.class.php
r4957 r5018 39 39 40 40 return self::$instance; 41 } 42 43 static public function hasInstance() 44 { 45 return true; 41 46 } 42 47 trunk/test/unit/view/sfEscapedViewParameterHolderTest.php
r4898 r5018 26 26 } 27 27 28 class myUser29 {30 public function getAttributeHolder()31 {32 return new sfParameterHolder();33 }34 }35 36 28 class myRequest 37 29 { … … 42 34 } 43 35 36 class 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 44 48 $context = sfContext::getInstance(array( 45 'user' => 'myUser',46 49 'request' => 'myRequest', 47 50 )); 51 $dispatcher = $context->dispatcher; 52 53 FilterParameters::$context = $context; 54 $dispatcher->connect('template.filter_parameters', array('FilterParameters', 'filter')); 48 55 49 56 // ->initialize() 50 57 $t->diag('->initialize()'); 51 58 $p = new sfEscapedViewParameterHolder(); 52 $p->initialize($ context);59 $p->initialize($dispatcher); 53 60 $t->is($p->get('sf_user'), $context->user, '->initialize() add some symfony shortcuts as parameters'); 54 61 $t->is($p->get('sf_request'), $context->request, '->initialize() add some symfony shortcuts as parameters'); 55 62 $t->is($p->get('sf_response'), $context->response, '->initialize() add some symfony shortcuts as parameters'); 56 63 57 $p->initialize($ context, array('foo' => 'bar'));64 $p->initialize($dispatcher, array('foo' => 'bar')); 58 65 $t->is($p->get('foo'), 'bar', '->initialize() takes an array of default parameters as its second argument'); 59 66 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')); 61 68 $t->is($p->getEscaping(), 'on', '->initialize() takes an array of options as its third argument'); 62 69 $t->is($p->getEscapingMethod(), ESC_RAW, '->initialize() takes an array of options as its third argument'); … … 64 71 // ->getEscaping() ->setEscaping() 65 72 $t->diag('->getEscaping() ->setEscaping()'); 66 $p->initialize($ context);73 $p->initialize($dispatcher); 67 74 $p->setEscaping('on'); 68 75 $t->is($p->getEscaping(), 'on', '->setEscaping() changes the escaping strategy'); … … 75 82 // ->toArray() 76 83 $t->diag('->toArray()'); 77 $p->initialize($ context, array('foo' => 'bar'));84 $p->initialize($dispatcher, array('foo' => 'bar')); 78 85 $a = $p->toArray(); 79 86 $t->is($a['foo'], 'bar', '->toArray() returns an array representation of the parameter holder'); … … 81 88 // ->serialize() / ->unserialize() 82 89 $t->diag('->serialize() / ->unserialize()'); 83 $p->initialize($ context, array('foo' => 'bar'));90 $p->initialize($dispatcher, array('foo' => 'bar')); 84 91 $unserialized = unserialize(serialize($p)); 85 92 $t->is($p->toArray(), $unserialized->toArray(), 'sfEscapedViewParameterHolder implements the Serializable interface'); trunk/test/unit/view/sfViewParameterHolderTest.php
r4898 r5018 12 12 require_once($_test_dir.'/unit/sfContextMock.class.php'); 13 13 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()); 23 15 24 16 class myRequest … … 30 22 } 31 23 32 $context = sfContext::getInstance(array( 33 'user' => 'myUser', 34 'request' => 'myRequest', 35 )); 24 class 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 39 FilterParameters::$context = $context; 40 $dispatcher->connect('template.filter_parameters', array('FilterParameters', 'filter')); 36 41 37 42 // ->initialize() 38 43 $t->diag('->initialize()'); 39 44 $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')); 46 46 $t->is($p->get('foo'), 'bar', '->initialize() takes an array of default parameters as its second argument'); 47 47 48 48 // ->toArray() 49 49 $t->diag('->toArray()'); 50 $p->initialize($ context, array('foo' => 'bar'));50 $p->initialize($dispatcher, array('foo' => 'bar')); 51 51 $a = $p->toArray(); 52 52 $t->is($a['foo'], 'bar', '->toArray() returns an array representation of the parameter holder'); … … 54 54 // ->serialize() / ->unserialize() 55 55 $t->diag('->serialize() / ->unserialize()'); 56 $p->initialize($ context, array('foo' => 'bar'));56 $p->initialize($dispatcher, array('foo' => 'bar')); 57 57 $unserialized = unserialize(serialize($p)); 58 58 $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');