Development

Changeset 4934

You must first sign up to be able to contribute.

Changeset 4934

Show
Ignore:
Timestamp:
08/30/07 10:24:10 (2 years ago)
Author:
fabien
Message:

refactored flash attributes

  • flash attributes are now managed by sfUser
  • removed sfFlashFilter
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/data/config/filters.yml

    r2661 r4934  
    2727  class: sfCommonFilter 
    2828 
    29 flash: 
    30   class: sfFlashFilter 
    31   param: 
    32     condition: %SF_USE_FLASH% 
    33  
    3429# execution filter must be the last registered filter 
    3530execution: 
  • trunk/data/generator/sfPropelAdmin/default/template/actions/actions.class.php

    r4334 r4934  
    6363      $this->save<?php echo $this->getClassName() ?>($this-><?php echo $this->getSingularName() ?>); 
    6464 
    65       $this->setFlash('notice', 'Your modifications have been saved'); 
     65      $this->getUser()->setFlash('notice', 'Your modifications have been saved'); 
    6666 
    6767      if ($this->getRequestParameter('save_and_add')) 
  • trunk/data/generator/sfPropelAdmin/default/template/templates/_edit_messages.php

    r4895 r4934  
    99</dl> 
    1010</div> 
    11 [?php elseif ($sf_flash->has('notice')): ?] 
     11[?php elseif ($sf_user->hasFlash('notice')): ?] 
    1212<div class="save-ok"> 
    13 <h2>[?php echo __($sf_flash->get('notice')) ?]</h2> 
     13<h2>[?php echo __($sf_user->getFlash('notice')) ?]</h2> 
    1414</div> 
    1515[?php endif; ?] 
  • trunk/data/skeleton/app/app/config/filters.yml

    r4406 r4934  
    1313cache:     ~ 
    1414common:    ~ 
    15 flash:     ~ 
    1615execution: ~ 
  • trunk/lib/action/sfComponent.class.php

    r4850 r4934  
    318318 
    319319  /** 
    320    * Sets a flash variable that will be passed to the very next action. 
    321    * 
    322    * @param  string  The name of the flash variable 
    323    * @param  string  The value of the flash variable 
    324    * @param  boolean true if the flash have to persist for the following request (true by default) 
    325    */ 
    326   public function setFlash($name, $value, $persist = true) 
    327   { 
    328     $this->getUser()->setAttribute($name, $value, 'symfony/flash'); 
    329  
    330     if ($persist) 
    331     { 
    332       // clear removal flag 
    333       $this->getUser()->getAttributeHolder()->remove($name, 'symfony/flash/remove'); 
    334     } 
    335     else 
    336     { 
    337       $this->getUser()->setAttribute($name, true, 'symfony/flash/remove'); 
    338     } 
    339   } 
    340  
    341   /** 
    342    * Gets a flash variable. 
    343    * 
    344    * @param  string The name of the flash variable 
    345    * 
    346    * @return mixed The value of the flash variable 
    347    */ 
    348   public function getFlash($name) 
    349   { 
    350     return $this->getUser()->getAttribute($name, null, 'symfony/flash'); 
    351   } 
    352  
    353   /** 
    354    * Returns true if a flash variable of the specified name exists. 
    355    *  
    356    * @param  string The name of the flash variable 
    357    * 
    358    * @return boolean   true if the variable exists, false otherwise 
    359    */ 
    360   public function hasFlash($name) 
    361   { 
    362     return $this->getUser()->hasAttribute($name, 'symfony/flash'); 
    363   } 
    364  
    365   /** 
    366320   * Sends and email from the current action. 
    367321   * 
  • trunk/lib/config/sfFactoryConfigHandler.class.php

    r4895 r4934  
    142142 
    143143          // append instance initialization 
    144           $inits[] = sprintf("  \$this->factories['user']->initialize(\$this, sfConfig::get('sf_factory_user_parameters', %s));", var_export($parameters, true)); 
     144          $inits[] = sprintf("  \$this->factories['user']->initialize(\$this, array_merge(array('use_flash' => sfConfig::get('sf_use_flash')), sfConfig::get('sf_factory_user_parameters', %s)));", var_export(is_array($parameters) ? $parameters : array(), true)); 
    145145          break; 
    146146 
  • trunk/lib/user/sfUser.class.php

    r4892 r4934  
    5151   * Initialize this User. 
    5252   * 
    53    * @param Context A Context instance. 
    54    * @param array   An associative array of initialization parameters. 
    55    * 
    56    * @return bool true, if initialization completes successfully, otherwise 
    57    *              false. 
    58    * 
    59    * @throws <b>sfInitializationException</b> If an error occurs while initializing this User. 
     53   * @param sfContext A sfContext instance. 
     54   * @param array     An associative array of initialization parameters. 
     55   * 
     56   * @return Boolean  true, if initialization completes successfully, otherwise false. 
     57   * 
     58   * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfUser. 
    6059   */ 
    6160  public function initialize($context, $parameters = array()) 
     
    9190 
    9291    $this->setCulture($culture); 
     92 
     93    // flag current flash to be removed at shutdown 
     94    if ($this->parameterHolder->get('use_flash', false) && $names = $this->attributeHolder->getNames('symfony/user/sfUser/flash')) 
     95    { 
     96      if (sfConfig::get('sf_logging_enabled')) 
     97      { 
     98        $this->context->getLogger()->info(sprintf('{sfUser} flag old flash messages ("%s")', implode('", "', $names))); 
     99      } 
     100 
     101      foreach ($names as $name) 
     102      { 
     103        $this->attributeHolder->set($name, true, 'symfony/user/sfUser/flash/remove'); 
     104      } 
     105    } 
    93106  } 
    94107 
     
    137150 
    138151  /** 
     152   * Sets a flash variable that will be passed to the very next action. 
     153   * 
     154   * @param  string  The name of the flash variable 
     155   * @param  string  The value of the flash variable 
     156   * @param  Boolean true if the flash have to persist for the following request (true by default) 
     157   */ 
     158  public function setFlash($name, $value, $persist = true) 
     159  { 
     160    if (!$this->parameterHolder->get('use_flash', false)) 
     161    { 
     162      return; 
     163    } 
     164 
     165    $this->setAttribute($name, $value, 'symfony/user/sfUser/flash'); 
     166 
     167    if ($persist) 
     168    { 
     169      // clear removal flag 
     170      $this->attributeHolder->remove($name, 'symfony/user/sfUser/flash/remove'); 
     171    } 
     172    else 
     173    { 
     174      $this->setAttribute($name, true, 'symfony/user/sfUser/flash/remove'); 
     175    } 
     176  } 
     177 
     178  /** 
     179   * Gets a flash variable. 
     180   * 
     181   * @param  string The name of the flash variable 
     182   * 
     183   * @return mixed  The value of the flash variable 
     184   */ 
     185  public function getFlash($name, $default = null) 
     186  { 
     187    if (!$this->parameterHolder->get('use_flash', false)) 
     188    { 
     189      return $default; 
     190    } 
     191 
     192    return $this->getAttribute($name, $default, 'symfony/user/sfUser/flash'); 
     193  } 
     194 
     195  /** 
     196   * Returns true if a flash variable of the specified name exists. 
     197   *  
     198   * @param  string  The name of the flash variable 
     199   * 
     200   * @return Boolean true if the variable exists, false otherwise 
     201   */ 
     202  public function hasFlash($name) 
     203  { 
     204    if (!$this->parameterHolder->get('use_flash', false)) 
     205    { 
     206      return false; 
     207    } 
     208 
     209    return $this->hasAttribute($name, 'symfony/user/sfUser/flash'); 
     210  } 
     211 
     212  /** 
    139213   * Gets culture. 
    140214   * 
     
    193267  public function shutdown() 
    194268  { 
     269    // remove flash that are tagged to be removed 
     270    if ($this->parameterHolder->get('use_flash', false) && $names = $this->attributeHolder->getNames('symfony/user/sfUser/flash/remove')) 
     271    { 
     272      if (sfConfig::get('sf_logging_enabled')) 
     273      { 
     274        $this->context->getLogger()->info(sprintf('{sfUser} remove old flash messages ("%s")', implode('", "', $names))); 
     275      } 
     276 
     277      foreach ($names as $name) 
     278      { 
     279        $this->attributeHolder->remove($name, 'symfony/user/sfUser/flash'); 
     280        $this->attributeHolder->remove($name, 'symfony/user/sfUser/flash/remove'); 
     281      } 
     282    } 
     283 
    195284    $storage = $this->context->getStorage(); 
    196285 
  • trunk/lib/view/sfViewParameterHolder.class.php

    r4898 r4934  
    5858  protected function getGlobalAttributes() 
    5959  { 
    60     $flash = new sfParameterHolder(); 
    61     $flash->add($this->context->getUser()->getAttributeHolder()->getAll('symfony/flash')); 
    62  
    6360    $attributes = array( 
    6461      'sf_context'  => $this->context, 
     
    6764      'sf_response' => $this->context->getResponse(), 
    6865      'sf_user'     => $this->context->getUser(), 
    69       'sf_flash'    => $flash, 
    7066    ); 
    7167 
  • trunk/test/functional/fixtures/project/apps/backend/config/filters.yml

    r2661 r4934  
    88cache:     ~ 
    99common:    ~ 
    10 flash:     ~ 
    1110execution: ~ 
  • trunk/test/functional/fixtures/project/apps/cache/config/filters.yml

    r2661 r4934  
    88cache:     ~ 
    99common:    ~ 
    10 flash:     ~ 
    1110execution: ~ 
  • trunk/test/functional/fixtures/project/apps/crud/config/filters.yml

    r2661 r4934  
    88cache:     ~ 
    99common:    ~ 
    10 flash:     ~ 
    1110execution: ~ 
  • trunk/test/functional/fixtures/project/apps/frontend/config/filters.yml

    r2661 r4934  
    1010cache:     ~ 
    1111common:    ~ 
    12 flash:     ~ 
    1312execution: ~ 
  • trunk/test/functional/fixtures/project/apps/frontend/modules/configFiltersSimpleFilter/config/filters.yml

    r2661 r4934  
    1313cache:     ~ 
    1414common:    ~ 
    15 flash:     ~ 
    1615execution: ~ 
  • trunk/test/functional/fixtures/project/apps/i18n/config/filters.yml

    r2740 r4934  
    77cache:     ~ 
    88common:    ~ 
    9 flash:     ~ 
    109execution: ~ 
  • trunk/test/functional/fixtures/project/config/filters.yml

    r2958 r4934  
    77cache:     ~ 
    88common:    ~ 
    9 flash:     ~ 
    109execution: ~ 
  • trunk/test/functional/fixtures/project/plugins/sfConfigPlugin/config/filters.yml

    r3178 r4934  
    77cache:     ~ 
    88common:    ~ 
    9 flash:     ~ 
    109execution: ~ 
  • trunk/test/unit/user/sfUserTest.php

    r4890 r4934  
    1212require_once($_test_dir.'/unit/sfContextMock.class.php'); 
    1313 
    14 $t = new lime_test(33, new lime_output_color()); 
     14$t = new lime_test(39, new lime_output_color()); 
    1515 
    1616$_SERVER['session_id'] = 'test'; 
     
    4141$userBis = new sfUser(); 
    4242$userBis->initialize($context); 
    43 $t->is($userBis->getCulture(), 'de', '->intialize() serializes the culture to the session data'); 
     43$t->is($userBis->getCulture(), 'de', '->initialize() serializes the culture to the session data'); 
    4444 
    4545// ->setCulture() ->getCulture() 
     
    4747$user->setCulture('fr'); 
    4848$t->is($user->getCulture(), 'fr', '->setCulture() changes the current user culture'); 
     49 
     50// ->setFlash() ->getFlash() ->hasFlash() 
     51$t->diag('->setFlash() ->getFlash() ->hasFlash()'); 
     52$user->initialize($context, array('use_flash' => true)); 
     53$user->setFlash('foo', 'bar'); 
     54$t->is($user->getFlash('foo'), 'bar', '->setFlash() sets a flash variable'); 
     55$t->is($user->hasFlash('foo'), true, '->hasFlash() returns true if the flash variable exists'); 
     56user_flush($context, array('use_flash' => true)); 
     57 
     58$userBis = new sfUser(); 
     59$userBis->initialize($context, array('use_flash' => true)); 
     60$t->is($userBis->getFlash('foo'), 'bar', '->getFlash() returns a flash previously set'); 
     61$t->is($userBis->hasFlash('foo'), true, '->hasFlash() returns true if the flash variable exists'); 
     62user_flush($context, array('use_flash' => true)); 
     63 
     64$userBis = new sfUser(); 
     65$userBis->initialize($context, array('use_flash' => true)); 
     66$t->is($userBis->getFlash('foo'), null, 'Flashes are automatically removed after the next request'); 
     67$t->is($userBis->hasFlash('foo'), false, '->hasFlash() returns true if the flash variable exists'); 
    4968 
    5069// parameter holder proxy 
     
    6584$storage->clear(); 
    6685 
    67 function user_flush($context
     86function user_flush($context, $parameters = array()
    6887{ 
    6988  $context->getUser()->shutdown(); 
    70   $context->getUser()->initialize($context); 
     89  $context->getUser()->initialize($context, $parameters); 
    7190  $parameters = $context->getStorage()->getParameterHolder()->getAll(); 
    7291  $context->getStorage()->shutdown(); 

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.