Development

Changeset 4892

You must first sign up to be able to contribute.

Changeset 4892

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

refactored routing

  • removed sfContext and sfConfig dependencies (the first parameter of sfRouting::initialize() is now an optional logger)
  • added sfPathInfoRouting
  • fixed sfNoRouting
  • added sfRouting::setDefaultParameter() and sfRouting::setDefaultParameters() (replaces the sfConfig::get('sf_routing_defaults') hack)
  • rewrote sfPatternRouting::getCurrentInternalUri() (and added unit tests)
  • added sfPatternRouting::setDefaultSuffix()
  • loading routing.yml in sfPatternRouting is optional (via the "load_configuration" parameter which is false by default)
  • added sfWebRequest::getGetParameters(), getPostParameters() and getRoutingParameters()
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/config/sfFactoryConfigHandler.class.php

    r4890 r4892  
    185185 
    186186          // append instance initialization 
    187           $inits[] = sprintf("  \$this->factories['routing']->initialize(\$this, sfConfig::get('sf_factory_routing_parameters', %s));", var_export($parameters, true)); 
     187          $inits[] = sprintf("  \$this->factories['routing']->initialize(\$this->factories['logger'], array_merge(array('load_configuration' => true, 'suffix' => sfConfig::get('sf_suffix'), 'default_module' => sfConfig::get('sf_default_module'), 'default_action' => sfConfig::get('sf_default_action')), sfConfig::get('sf_factory_routing_parameters', %s)));", var_export(is_array($parameters) ? $parameters : array(), true)); 
    188188          break; 
    189189 
  • trunk/lib/request/sfWebRequest.class.php

    r4887 r4892  
    2424class sfWebRequest extends sfRequest 
    2525{ 
    26   /** 
    27    * A list of languages accepted by the browser. 
    28    * @var array 
    29    */ 
    30   protected $languages = null; 
    31  
    32   /** 
    33    * A list of charsets accepted by the browser 
    34    * @var array 
    35    */ 
    36   protected $charsets = null; 
    37  
    38   /** 
    39    * @var array  List of content types accepted by the client. 
    40    */ 
    41   protected $acceptableContentTypes = null; 
    42  
    43   protected $pathInfoArray = null; 
    44  
    45   protected $relativeUrlRoot = null; 
     26  protected 
     27    $languages              = null, 
     28    $charsets               = null, 
     29    $acceptableContentTypes = null, 
     30    $pathInfoArray          = null, 
     31    $relativeUrlRoot        = null, 
     32    $getParameters          = null, 
     33    $postParameters         = null, 
     34    $routingParameters      = null; 
    4635 
    4736  /** 
     
    5443  public function getFile($name) 
    5544  { 
    56     return ($this->hasFile($name) ? $this->getFileValues($name) : null)
     45    return $this->hasFile($name) ? $this->getFileValues($name) : null
    5746  } 
    5847 
     
    7766  public function getFileError($name) 
    7867  { 
    79     return ($this->hasFile($name) ? $this->getFileValue($name, 'error') : UPLOAD_ERR_NO_FILE)
     68    return $this->hasFile($name) ? $this->getFileValue($name, 'error') : UPLOAD_ERR_NO_FILE
    8069  } 
    8170 
     
    8978  public function getFileName($name) 
    9079  { 
    91     return ($this->hasFile($name) ? $this->getFileValue($name, 'name') : null)
     80    return $this->hasFile($name) ? $this->getFileValue($name, 'name') : null
    9281  } 
    9382 
     
    121110  public function getFilePath($name) 
    122111  { 
    123     return ($this->hasFile($name) ? $this->getFileValue($name, 'tmp_name') : null)
     112    return $this->hasFile($name) ? $this->getFileValue($name, 'tmp_name') : null
    124113  } 
    125114 
     
    133122  public function getFileSize($name) 
    134123  { 
    135     return ($this->hasFile($name) ? $this->getFileValue($name, 'size') : null)
     124    return $this->hasFile($name) ? $this->getFileValue($name, 'size') : null
    136125  } 
    137126 
     
    148137  public function getFileType($name) 
    149138  { 
    150     return ($this->hasFile($name) ? $this->getFileValue($name, 'type') : null)
     139    return $this->hasFile($name) ? $this->getFileValue($name, 'type') : null
    151140  } 
    152141 
     
    179168  public function hasFileError($name) 
    180169  { 
    181     return ($this->hasFile($name) ? ($this->getFileValue($name, 'error') != UPLOAD_ERR_OK) : false)
     170    return $this->hasFile($name) ? ($this->getFileValue($name, 'error') != UPLOAD_ERR_OK) : false
    182171  } 
    183172 
     
    330319 
    331320  /** 
    332    * Returns the array that contains all request information ($_SERVER or $_ENV). 
    333    * 
    334    * This information is stored in the [sf_path_info_array] constant. 
    335    * 
    336    * @return  array Path information 
    337    */ 
    338   protected function getPathInfoArray() 
    339   { 
    340     if (!$this->pathInfoArray) 
    341     { 
    342       // parse PATH_INFO 
    343       switch (sfConfig::get('sf_path_info_array')) 
    344       { 
    345         case 'SERVER': 
    346           $this->pathInfoArray =& $_SERVER; 
    347           break; 
    348  
    349         case 'ENV': 
    350         default: 
    351           $this->pathInfoArray =& $_ENV; 
    352       } 
    353     } 
    354  
    355     return $this->pathInfoArray; 
    356   } 
    357  
    358   /** 
    359321   * Retrieves the uniform resource identifier for the current web request. 
    360322   * 
     
    421383 
    422384    // simulate PATH_INFO if needed 
    423     $sf_path_info_key = sfConfig::get('sf_path_info_key'); 
     385    $sf_path_info_key = sfConfig::get('sf_path_info_key', 'PATH_INFO'); 
    424386    if (!isset($pathArray[$sf_path_info_key]) || !$pathArray[$sf_path_info_key]) 
    425387    { 
     
    458420  } 
    459421 
    460   /** 
    461    * Loads GET, PATH_INFO and POST data into the parameter list. 
    462    * 
    463    */ 
    464   protected function loadParameters() 
    465   { 
    466     // merge GET parameters 
    467     if (get_magic_quotes_gpc()) 
    468     { 
    469       $_GET = sfToolkit::stripslashesDeep($_GET); 
    470     } 
    471     $this->getParameterHolder()->addByRef($_GET); 
    472  
    473     $pathInfo = $this->getPathInfo(); 
    474     if ($pathInfo) 
    475     { 
    476       // routing map defined? 
    477       $r = $this->context->getRouting(); 
    478       if ($r->hasRoutes()) 
    479       { 
    480         $results = $r->parse($pathInfo); 
    481         if ($results !== null) 
    482         { 
    483           $this->getParameterHolder()->addByRef($results); 
    484         } 
    485         else 
    486         { 
    487           $this->setParameter('module', sfConfig::get('sf_error_404_module')); 
    488           $this->setParameter('action', sfConfig::get('sf_error_404_action')); 
    489         } 
    490       } 
    491       else 
    492       { 
    493         $array = explode('/', trim($pathInfo, '/')); 
    494         $count = count($array); 
    495  
    496         for ($i = 0; $i < $count; $i++) 
    497         { 
    498           // see if there's a value associated with this parameter, 
    499           // if not we're done with path data 
    500           if ($count > ($i + 1)) 
    501           { 
    502             $this->getParameterHolder()->setByRef($array[$i], $array[++$i]); 
    503           } 
    504         } 
    505       } 
    506     } 
    507  
    508     // merge POST parameters 
    509     if (get_magic_quotes_gpc()) 
    510     { 
    511       $_POST = sfToolkit::stripslashesDeep((array) $_POST); 
    512     } 
    513     $this->getParameterHolder()->addByRef($_POST); 
    514  
    515     // move symfony parameters in a protected namespace (parameters prefixed with _sf_) 
    516     foreach ($this->getParameterHolder()->getAll() as $key => $value) 
    517     { 
    518       if (0 === stripos($key, '_sf_')) 
    519       { 
    520         $this->getParameterHolder()->remove($key); 
    521         $this->setParameter($key, $value, 'symfony/request/sfWebRequest'); 
    522         unset($_GET[$key]); 
    523       } 
    524     } 
    525  
    526     if (sfConfig::get('sf_logging_enabled')) 
    527     { 
    528       $this->context->getLogger()->info(sprintf('{sfRequest} request parameters %s', str_replace("\n", '', var_export($this->getParameterHolder()->getAll(), true)))); 
    529     } 
     422  public function getGetParameters() 
     423  { 
     424    return $this->getParameters; 
     425  } 
     426 
     427  public function getPostParameters() 
     428  { 
     429    return $this->postParameters; 
     430  } 
     431 
     432  public function getRoutingParameters() 
     433  { 
     434    return $this->routingParameters; 
    530435  } 
    531436 
     
    854759    return array_keys($values); 
    855760  } 
     761 
     762  /** 
     763   * Returns the array that contains all request information ($_SERVER or $_ENV). 
     764   * 
     765   * This information is stored in the [sf_path_info_array] constant. 
     766   * 
     767   * @return  array Path information 
     768   */ 
     769  protected function getPathInfoArray() 
     770  { 
     771    if (!$this->pathInfoArray) 
     772    { 
     773      // parse PATH_INFO 
     774      switch (sfConfig::get('sf_path_info_array', 'SERVER')) 
     775      { 
     776        case 'SERVER': 
     777          $this->pathInfoArray =& $_SERVER; 
     778          break; 
     779 
     780        case 'ENV': 
     781        default: 
     782          $this->pathInfoArray =& $_ENV; 
     783      } 
     784    } 
     785 
     786    return $this->pathInfoArray; 
     787  } 
     788 
     789  protected function parseRoutingParameters() 
     790  { 
     791    $parameters = $this->context->getRouting()->parse($this->getPathInfo()); 
     792    if (!is_null($parameters)) 
     793    { 
     794      if (!isset($parameters['module'])) 
     795      { 
     796        $parameters['module'] = sfConfig::get('sf_default_module'); 
     797      } 
     798 
     799      if (!isset($parameters['action'])) 
     800      { 
     801        $parameters['action'] = sfConfig::get('sf_default_action'); 
     802      } 
     803    } 
     804    else 
     805    { 
     806      $parameters['module'] = sfConfig::get('sf_error_404_module'); 
     807      $parameters['action'] = sfConfig::get('sf_error_404_action'); 
     808    } 
     809 
     810    $this->routingParameters = $parameters; 
     811  } 
     812 
     813  /** 
     814   * Loads GET, PATH_INFO and POST data into the parameter list. 
     815   * 
     816   */ 
     817  protected function loadParameters() 
     818  { 
     819    // GET parameters 
     820    $this->getParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_GET) : $_GET; 
     821    $this->parameterHolder->add($this->getParameters); 
     822 
     823    // routing parameters 
     824    $this->parseRoutingParameters(); 
     825    $this->parameterHolder->add($this->routingParameters); 
     826 
     827    // POST parameters 
     828    $this->postParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_POST) : $_POST; 
     829    $this->parameterHolder->add($this->postParameters); 
     830 
     831    // move symfony parameters in a protected namespace (parameters prefixed with _sf_) 
     832    foreach ($this->parameterHolder->getAll() as $key => $value) 
     833    { 
     834      if (0 === stripos($key, '_sf_')) 
     835      { 
     836        $this->parameterHolder->remove($key); 
     837        $this->setParameter($key, $value, 'symfony/request/sfWebRequest'); 
     838      } 
     839    } 
     840 
     841    if (sfConfig::get('sf_logging_enabled')) 
     842    { 
     843      $this->context->getLogger()->info(sprintf('{sfRequest} request parameters %s', str_replace("\n", '', var_export($this->getParameterHolder()->getAll(), true)))); 
     844    } 
     845  } 
    856846} 
  • trunk/lib/routing/sfNoRouting.class.php

    r4435 r4892  
    1313 * 
    1414 * @package    symfony 
    15  * @subpackage controller 
     15 * @subpackage routing 
    1616 * @author     Fabien Potencier <fabien.potencier@symfony-project.com> 
    1717 * @version    SVN: $Id$ 
     
    2929  public function getCurrentInternalUri($with_route_name = false) 
    3030  { 
    31     $request = $this->context->getRequest()
     31    $parameters = $_GET
    3232 
    33     $parameters = $_GET; 
    34     unset($parameters['module']); 
    35     unset($parameters['action']); 
     33    // module/action 
     34    $module = isset($parameters['module']) ? $parameters['module'] : $this->parameterHolder->get('default_module'); 
     35    $action = isset($parameters['action']) ? $parameters['action'] : $this->parameterHolder->get('default_action'); 
     36 
     37    // other parameters 
     38    unset($parameters['module'], $parameters['action']); 
     39    ksort($parameters); 
    3640    $parameters = count($parameters) ? '?'.http_build_query($parameters) : ''; 
    3741 
    38     return sprintf('%s/%s%s', $request->getParameter('module', sfConfig::get('sf_default_module')), $request->getParameter('action', sfConfig::get('sf_default_action')), $parameters); 
     42    return sprintf('%s/%s%s', $module, $action, $parameters); 
     43  } 
     44 
     45 /** 
     46  * Generates a valid URLs for parameters. 
     47  * 
     48  * @param  array  The parameter values 
     49  * @param  string The divider between key/value pairs 
     50  * @param  string The equal sign to use between key and value 
     51  * 
     52  * @return string The generated URL 
     53  */ 
     54  public function generate($name, $params, $querydiv = '/', $divider = '/', $equals = '/') 
     55  { 
     56    $parameters = http_build_query(array_merge($this->defaultParameters, $params)); 
     57 
     58    return '/'.($parameters ? '?'.$parameters : ''); 
     59  } 
     60 
     61 /** 
     62  * Parses a URL to find a matching route. 
     63  * 
     64  * Returns null if no route match the URL. 
     65  * 
     66  * @param  string URL to be parsed 
     67  * 
     68  * @return array  An array of parameters 
     69  */ 
     70  public function parse($url) 
     71  { 
     72    return array(); 
    3973  } 
    4074 
     
    77111  { 
    78112  } 
    79  
    80  /** 
    81   * Generates a valid URLs for parameters. 
    82   * 
    83   * @param  array  The parameter values 
    84   * @param  string The divider between key/value pairs 
    85   * @param  string The equal sign to use between key and value 
    86   * 
    87   * @return string The generated URL 
    88   */ 
    89   public function generate($name, $params, $querydiv = '/', $divider = '/', $equals = '/') 
    90   { 
    91     $parameters = http_build_query($params); 
    92  
    93     return '/'.($parameters ? '?'.$parameters : ''); 
    94   } 
    95  
    96  /** 
    97   * Parses a URL to find a matching route. 
    98   * 
    99   * Returns null if no route match the URL. 
    100   * 
    101   * @param  string URL to be parsed 
    102   * 
    103   * @return array  An array of parameters 
    104   */ 
    105   public function parse($url) 
    106   { 
    107     $parameters = parse_url($url); 
    108     if (isset($parameters['query'])) 
    109     { 
    110       parse_str($parameters['query'], $parameters); 
    111     } 
    112     else 
    113     { 
    114       $parameters = array(); 
    115     } 
    116  
    117     return array_merge(array('module' => sfConfig::get('sf_default_module'), 'action' => sfConfig::get('sf_default_action')), $parameters); 
    118   } 
    119113} 
  • trunk/lib/routing/sfPatternRouting.class.php

    r4887 r4892  
    1717 * 
    1818 * @package    symfony 
    19  * @subpackage controller 
     19 * @subpackage routing 
    2020 * @author     Fabien Potencier <fabien.potencier@symfony-project.com> 
    2121 * @version    SVN: $Id$ 
     
    2424{ 
    2525  protected 
    26     $currentRouteName   = null, 
    27     $currentInternalUri = null, 
    28     $routes             = array(); 
     26    $currentRouteName       = null, 
     27    $currentInternalUri     = null, 
     28    $currentRouteParameters = null, 
     29    $defaultSuffix          = '', 
     30    $routes                 = array(); 
    2931 
    3032  /** 
    3133   * Initialize this Routing. 
    3234   * 
    33    * @param sfContext A sfContext instance. 
    34    * @param array     An associative array of initialization parameters. 
     35   * @param sfLogger A sfLogger instance (or null) 
     36   * @param array An associative array of initialization parameters. 
    3537   * 
    3638   * @return bool true, if initialization completes successfully, otherwise false. 
     
    3840   * @throws <b>sfInitializationException</b> If an error occurs while initializing this User. 
    3941   */ 
    40   public function initialize($context, $parameters = array()) 
    41   { 
    42     parent::initialize($context, $parameters); 
    43  
    44     if ($config = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/routing.yml', true)) 
    45     { 
    46       include($config); 
     42  public function initialize(sfLogger $logger = null, $parameters = array()) 
     43  { 
     44    parent::initialize($logger, $parameters); 
     45 
     46    $this->defaultSuffix = $this->parameterHolder->get('suffix', ''); 
     47 
     48    if ($this->parameterHolder->get('load_configuration', false)) 
     49    { 
     50      if ($config = sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_config_dir_name').'/routing.yml', true)) 
     51      { 
     52        include($config); 
     53      } 
    4754    } 
    4855  } 
     
    5663   * @return string The current internal URI 
    5764   */ 
    58   public function getCurrentInternalUri($with_route_name = false) 
     65  public function getCurrentInternalUri($withRouteName = false) 
    5966  { 
    6067    if (is_null($this->currentRouteName)) 
     
    6572    if (is_null($this->currentInternalUri)) 
    6673    { 
     74      $parameters = $this->currentRouteParameters; 
     75 
    6776      list($url, $regexp, $names, $namesHash, $defaults, $requirements, $suffix) = $this->routes[$this->currentRouteName]; 
    6877 
    69       $request = $this->context->getRequest(); 
    70  
    71       if ($with_route_name) 
     78      if ($withRouteName) 
    7279      { 
    7380        $internalUri = '@'.$this->currentRouteName; 
     
    7582      else 
    7683      { 
    77         $internalUri = $request->getParameter('module', isset($defaults['module']) ? $defaults['module'] : '').'/'.$request->getParameter('action', isset($defaults['action']) ? $defaults['action'] : ''); 
     84        $module = isset($parameters['module']) ? $parameters['module'] : $this->parameterHolder->get('default_module'); 
     85        $action = isset($parameters['action']) ? $parameters['action'] : $this->parameterHolder->get('default_action'); 
     86 
     87        $internalUri = $module.'/'.$action; 
    7888      } 
    7989 
     
    8595        if ($name == 'module' || $name == 'action') continue; 
    8696 
    87         $params[] = $name.'='.$request->getParameter($name, isset($defaults[$name]) ? $defaults[$name] : ''); 
     97        $params[] = $name.'='.isset($parameters[$name]) ? $parameters[$name] : (isset($defaults[$name]) ? $defaults[$name] : ''); 
    8898      } 
    8999 
     
    91101      if (strpos($url, '*')) 
    92102      { 
    93         foreach ($request->getParameterHolder()->getAll() as $key => $value) 
     103        foreach ($parameters as $key => $value) 
    94104        { 
    95105          if ($key == 'module' || $key == 'action' || in_array($key, $names)) 
     
    111121  } 
    112122 
     123  public function setDefaultSuffix($suffix) 
     124  { 
     125    $this->defaultSuffix = '.' == $suffix ? '' : $suffix; 
     126  } 
     127 
    113128  /** 
    114129   * Gets the current compiled route array. 
     
    148163  public function clearRoutes() 
    149164  { 
    150     if (sfConfig::get('sf_logging_enabled')) 
    151     { 
    152       $this->context->getLogger()->info('{sfRouting} clear all current routes'); 
     165    if (!is_null($this->logger)) 
     166    { 
     167      $this->logger->info('{sfRouting} clear all current routes'); 
    153168    } 
    154169 
     
    225240    $parsed = array(); 
    226241    $names  = array(); 
    227     $suffix = (($sf_suffix = sfConfig::get('sf_suffix')) == '.') ? '' : $sf_suffix; 
     242    $suffix = $this->defaultSuffix; 
    228243 
    229244    // used for performance reasons 
     
    256271      if (preg_match('/^(.+)(\.\w*)$/i', $elements[count($elements) - 1], $matches)) 
    257272      { 
    258         $suffix = ($matches[2] == '.') ? '' : $matches[2]; 
     273        $suffix = '.' == $matches[2] ? '' : $matches[2]; 
    259274        $elements[count($elements) - 1] = $matches[1]; 
    260275        $route = '/'.implode('/', $elements); 
     
    309324    } 
    310325 
    311     if (sfConfig::get('sf_logging_enabled')) 
    312     { 
    313       $this->context->getLogger()->info('{sfRouting} connect "'.$route.'"'.($suffix ? ' ("'.$suffix.'" suffix)' : '')); 
     326    if (!is_null($this->logger)) 
     327    { 
     328      $this->logger->info(sprintf('{sfRouting} connect "%s"%s', $route, $suffix ? ' ("'.$suffix.'" suffix)' : '')); 
    314329    } 
    315330 
     
    328343  public function generate($name, $params, $querydiv = '/', $divider = '/', $equals = '/') 
    329344  { 
    330     $globalDefaults = sfConfig::get('sf_routing_defaults', null); 
    331  
    332345    // named route? 
    333346    if ($name) 
     
    339352 
    340353      list($url, $regexp, $names, $namesHash, $defaults, $requirements, $suffix) = $this->routes[$name]; 
    341       if ($globalDefaults !== null) 
    342       { 
    343         $defaults = array_merge($defaults, $globalDefaults); 
    344       } 
     354      $defaults = array_merge($defaults, $this->defaultParameters); 
    345355 
    346356      // all params must be given 
     
    360370      { 
    361371        list($url, $regexp, $names, $namesHash, $defaults, $requirements, $suffix) = $route; 
    362         if ($globalDefaults !== null) 
    363         { 
    364           $defaults = array_merge($defaults, $globalDefaults); 
    365         } 
     372        $defaults = array_merge($defaults, $this->defaultParameters); 
    366373 
    367374        $tparams = array_merge($defaults, $params); 
     
    481488    // we remove multiple / 
    482489    $url = preg_replace('#/+#', '/', $url); 
    483     foreach ($this->routes as $route_name => $route) 
     490    $out = array(); 
     491    $break = false; 
     492    foreach ($this->routes as $routeName => $route) 
    484493    { 
    485494      $out = array(); 
     
    560569        { 
    561570          // we store route name 
    562           $this->currentRouteName = $route_name; 
     571          $this->currentRouteName = $routeName; 
    563572          $this->currentInternalUri = null; 
    564573 
    565           if (sfConfig::get('sf_logging_enabled')) 
    566           { 
    567             $this->context->getLogger()->info('{sfRouting} match route ['.$route_name.'] "'.$route.'"'); 
     574          if (!is_null($this->logger)) 
     575          { 
     576            $this->logger->info(sprintf('{sfRouting} match route [%s] "%s"', $routeName, $route)); 
    568577          } 
    569578 
     
    572581      } 
    573582    } 
     583 
     584    $this->currentRouteParameters = $out; 
    574585 
    575586    // no route found 
    576587    if (!$break) 
    577588    { 
    578       if (sfConfig::get('sf_logging_enabled')) 
    579       { 
    580         $this->context->getLogger()->info('{sfRouting} no matching route found'); 
    581       } 
    582  
    583       return null; 
    584     } 
    585  
    586     return $out
     589      if (!is_null($this->logger)) 
     590      { 
     591        $this->logger->info('{sfRouting} no matching route found'); 
     592      } 
     593 
     594      $this->currentRouteParameters = null; 
     595    } 
     596 
     597    return $this->currentRouteParameters
    587598  } 
    588599} 
  • trunk/lib/routing/sfRouting.class.php

    r4597 r4892  
    1313 * 
    1414 * @package    symfony 
    15  * @subpackage controller 
     15 * @subpackage routing 
    1616 * @author     Fabien Potencier <fabien.potencier@symfony-project.com> 
    1717 * @version    SVN: $Id$ 
     
    2020{ 
    2121  protected 
    22     $context         = null, 
    23     $parameterHolder = null; 
     22    $logger            = null, 
     23    $defaultParameters = array(), 
     24    $parameterHolder   = null; 
    2425 
    2526  /** 
     
    4849   * Initializes this sfRouting instance. 
    4950   * 
    50    * @param sfContext A sfContext instance. 
    51    * @param array    An associative array of initialization parameters. 
     51   * @param sfLogger A sfLogger instance (or null) 
     52   * @param array    An associative array of initialization parameters. 
    5253   * 
    53    * @return bool true, if initialization completes successfully, otherwise false. 
     54   * @return bool    true, if initialization completes successfully, otherwise false. 
    5455   * 
    5556   * @throws <b>sfInitializationException</b> If an error occurs while initializing this User. 
    5657   */ 
    57   public function initialize($context, $parameters = array()) 
     58  public function initialize(sfLogger $logger = null, $parameters = array()) 
    5859  { 
    59     $this->context = $context; 
     60    $this->logger = $logger; 
     61 
     62    if (!isset($parameters['default_module'])) 
     63    { 
     64      $parameters['default_module'] = 'default'; 
     65    } 
     66 
     67    if (!isset($parameters['default_action'])) 
     68    { 
     69      $parameters['default_action'] = 'index'; 
     70    } 
    6071 
    6172    $this->parameterHolder = new sfParameterHolder(); 
     
    124135 
    125136  /** 
     137   * Sets a default parameter for URL generation. 
     138   * 
     139   * @param string The key 
     140   * @param string The value 
     141   */ 
     142  public function setDefaultParameter($key, $value) 
     143  { 
     144    $this->defaultParameters[$key] = $value; 
     145  } 
     146 
     147  /** 
     148   * Sets the default parameters for URL generation. 
     149   * 
     150   * @param array An array of default parameters 
     151   */ 
     152  public function setDefaultParameters($parameters) 
     153  { 
     154    $this->defaultParameters = $parameters; 
     155  } 
     156 
     157  /** 
    126158   * Execute the shutdown procedure. 
    127159   * 
  • trunk/lib/user/sfUser.class.php

    r4597 r4892  
    131131      } 
    132132 
    133       // add the culture in the routing default parameters 
    134       sfConfig::set('sf_routing_defaults', array_merge((array) sfConfig::get('sf_routing_defaults'), array('sf_culture' => $culture))); 
     133      // change the culture in the routing default parameters 
     134      $this->context->getRouting()->setDefaultParameter('sf_culture', $culture); 
    135135    } 
    136136  } 
  • trunk/test/unit/action/sfComponentTest.php

    r4440 r4892  
    2020 
    2121$context = sfContext::getInstance(array( 
    22   'routing' => 'sfNoRouting', 
    23   'request' => 'sfWebRequest', 
     22  'routing' => 'sfNoRouting', 
     23  'request' => 'sfWebRequest', 
    2424)); 
    2525 
  • trunk/test/unit/response/sfWebResponseTest.php

    r4534 r4892  
    247247 
    248248sfLoader::loadHelpers(array('Helper', 'Tag', 'Url', 'Asset')); 
     249$_SERVER['SCRIPT_NAME'] = ''; 
    249250 
    250251// use and get javascript() 
  • trunk/test/unit/routing/sfNoRoutingTest.php

    r4440 r4892  
    1010 
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    12 require_once($_test_dir.'/unit/sfContextMock.class.php'); 
    1312 
    1413$t = new lime_test(12, new lime_output_color()); 
    1514 
    16 sfConfig::set('sf_default_module', 'main'); 
    17 sfConfig::set('sf_default_action', 'index'); 
    18  
    19 $context = sfContext::getInstance(array('routing' => 'sfNoRouting', 'request' => 'sfWebRequest')); 
    20 $request = $context->request; 
    21 $routing = $context->routing; 
     15$routing = new sfNoRouting(); 
     16$routing->initialize(); 
    2217 
    2318// ->getCurrentInternalUri() 
     
    2520 
    2621$_GET = array(); 
    27 $request->initialize($context); 
    28 $t->is($routing->getCurrentInternalUri(), 'main/index', '->getCurrentInternalUri() returns the current internal URI'); 
     22$t->is($routing->getCurrentInternalUri(), 'default/index', '->getCurrentInternalUri() returns the current internal URI'); 
    2923 
    3024$_GET = array('foo' => 'bar'); 
    31 $request->initialize($context); 
    32 $t->is($routing->getCurrentInternalUri(), 'main/index?foo=bar', '->getCurrentInternalUri() returns the current internal URI'); 
     25$t->is($routing->getCurrentInternalUri(), 'default/index?foo=bar', '->getCurrentInternalUri() returns the current internal URI'); 
    3326 
    3427$_GET = array('module' => 'foo', 'action' => 'bar'); 
    35 $request->initialize($context); 
    3628$t->is($routing->getCurrentInternalUri(), 'foo/bar', '->getCurrentInternalUri() returns the current internal URI'); 
    3729 
    3830$_GET = array('module' => 'foo', 'action' => 'bar', 'foo' => 'bar'); 
    39 $request->initialize($context); 
    4031$t->is($routing->getCurrentInternalUri(), 'foo/bar?foo=bar', '->getCurrentInternalUri() returns the current internal URI'); 
    4132 
    4233// ->parse() 
    4334$t->diag('parse'); 
    44 $t->is($routing->parse(''), array('module' => 'main', 'action' => 'index'), '->parse() parses a URL'); 
    45 $t->is($routing->parse('?foo=bar'), array('foo' => 'bar', 'module' => 'main', 'action' => 'index'), '->parse() parses a URL'); 
    46 $t->is($routing->parse('?module=foo&action=bar'), array('module' => 'foo', 'action' => 'bar'), '->parse() parses a URL'); 
    47 $t->is($routing->parse('?module=foo&action=bar&foo=bar'), array('foo' => 'bar', 'module' => 'foo', 'action' => 'bar'), '->parse() parses a URL'); 
     35$t->is($routing->parse(''), array(), '->parse() parses a URL'); 
     36$t->is($routing->parse('?foo=bar'), array(), '->parse() parses a URL'); 
     37$t->is($routing->parse('?module=foo&action=bar'), array(), '->parse() parses a URL'); 
     38$t->is($routing->parse('?module=foo&action=bar&foo=bar'), array(), '->parse() parses a URL'); 
    4839 
    4940// ->generate() 
  • trunk/test/unit/routing/sfPatternRoutingTest.php

    r4435 r4892  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(61, new lime_output_color()); 
     13$t = new lime_test(63, new lime_output_color()); 
    1414 
    1515class sfPatternRoutingTest extends sfPatternRouting 
     
    2323// public methods 
    2424$r = new sfPatternRoutingTest(); 
     25$r->initialize(); 
    2526foreach (array('clearRoutes', 'connect', 'generate', 'getCurrentInternalUri', 'getCurrentRouteName', 'getRoutes', 'hasRoutes', 'parse', 'setRoutes') as $method) 
    2627{ 
     
    8081// suffix 
    8182$r->clearRoutes(); 
    82 sfConfig::set('sf_suffix', '.html'); 
     83$r->setDefaultSuffix('.html'); 
    8384$r->connect('foo', '/foo/:module/:action/:param.foo', array('module' => 'default', 'action' => 'index')); 
    8485$url  = '/foo/default/index/foo.foo'; 
     
    9394$t->is($r->generate('', array('module' => 'default', 'action' => 'index1', 'param1' => 'foo1')), $url1, '->generate() routes can remove the default suffix'); 
    9495$t->is($r->generate('', array('module' => 'default', 'action' => 'index2', 'param2' => 'foo2')), $url2, '->generate() routes does not have suffix when they end by /'); 
    95 $t->is($r->generate('', array('module' => 'default', 'action' => 'index3', 'param3' => 'foo3')), $url3, '->generate() routes takes a suffix defined by "sf_suffix"'); 
     96$t->is($r->generate('', array('module' => 'default', 'action' => 'index3', 'param3' => 'foo3')), $url3, '->generate() routes takes a suffix defined by the "suffix" parameter'); 
    9697 
    9798$t->is($r->parse($url),  array('module' => 'default', 'action' => 'index',  'param'  => 'foo'),  '->parse() routes can override the default suffix'); 
    9899$t->is($r->parse($url1), array('module' => 'default', 'action' => 'index1', 'param1' => 'foo1'), '->parse() routes can remove the default suffix'); 
    99100$t->is($r->parse($url2), array('module' => 'default', 'action' => 'index2', 'param2' => 'foo2'), '->parse() routes does not have suffix when they end by /'); 
    100 $t->is($r->parse($url3), array('module' => 'default', 'action' => 'index3', 'param3' => 'foo3'), '->parse() routes takes a suffix defined by "sf_suffix"'); 
    101 sfConfig::set('sf_suffix', ''); 
     101$t->is($r->parse($url3), array('module' => 'default', 'action' => 'index3', 'param3' => 'foo3'), '->parse() routes takes a suffix defined by the "suffix" parameter'); 
     102$r->setDefaultSuffix('.'); 
    102103 
    103104// duplicate names 
     
    232233 
    233234// routing defaults parameters 
    234 sfConfig::set('sf_routing_defaults', array('foo' => 'bar')); 
     235$r->setDefaultParameter('foo', 'bar'); 
    235236$r->clearRoutes(); 
    236237$r->connect('test', '/test/:foo/:id', array('module' => 'default', 'action' => 'index')); 
    237238$params = array('module' => 'default', 'action' => 'index', 'id' => 12); 
    238239$url = '/test/bar/12'; 
    239 $t->is($r->generate('', $params), $url, '->generate() merge parameters with defaults from "sf_routing_defaults"'); 
     240$t->is($r->generate('', $params), $url, '->generate() merge parameters with defaults parameters'); 
     241$r->setDefaultParameters(array()); 
    240242 
    241243// ->appendRoute() 
     
    261263$p_route_names = array_keys($r->getRoutes()); 
    262264$t->is(implode('-', $p_route_names), implode('-', array_reverse($route_names)), '->prependRoute() adds new routes at the beginning of the existings ones'); 
     265 
     266// ->getCurrentInternalUri() 
     267$t->diag('->getCurrentInternalUri()'); 
     268$r->clearRoutes(); 
     269$r->connect('test',  '/:module', array('action' => 'index')); 
     270$r->connect('test1', '/:module/:action/*', array()); 
     271$r->parse('/'); 
     272$t->is($r->getCurrentInternalUri(), 'default/index', '->getCurrentInternalUri() returns the internal URI for last parsed URL'); 
     273$r->parse('/foo/bar/bar/foo/a/b'); 
     274$t->is($r->getCurrentInternalUri(), 'foo/bar?a=b&bar=foo', '->getCurrentInternalUri() returns the internal URI for last parsed URL'); 
  • trunk/test/unit/sfContextMock.class.php

    r4890 r4892  
    8080  } 
    8181 
    82   public function inject($type, $class
     82  public function inject($type, $class, $parameters = array()
    8383  { 
    8484    $object = new $class(); 
    8585    if (method_exists($object, 'initialize')) 
    8686    { 
    87       $object->initialize($this); 
     87      in_array($type, array('routing')) ? $object->initialize(null, $parameters) : $object->initialize($this, $parameters); 
    8888    } 
    8989    $this->$type = $object; 
  • trunk/test/unit/user/sfBasicSecurityUserTest.php

    r4440 r4892  
    2626 
    2727$context = sfContext::getInstance(array( 
     28  'routing' => 'sfPatternRouting', 
    2829  'request' => 'myRequest', 
    29   'user' => 'sfBasicSecurityUser', 
     30  'user'    => 'sfBasicSecurityUser', 
    3031)); 
    3132$user = $context->user; 

The Sensio Labs Network

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