Development

Changeset 8806

You must first sign up to be able to contribute.

Changeset 8806

Show
Ignore:
Timestamp:
05/06/08 15:52:58 (5 months ago)
Author:
FabianLange
Message:

split sfPatternRouting::parse into two methods to allow side-effect free retrieval of route that might match a url. Useful for link rewriting that might want to check if a link will match an internal route, without actually "switching" there. Added PHPdoc for the new sfPatternRouting::findRoute().
Updated PHPdoc on sfRouting::parse, mentioning that this does modify internal state.
Added unit tests to show issue of #3423. no separate tests for findRoute() needed as its implicitly tested by parse() tests. Added test to ensure that it is side effect free. Closes #3423, #3424

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/routing/sfPatternRouting.class.php

    r8093 r8806  
    595595  public function parse($url) 
    596596  { 
     597    if (null !== $routeInfo = $this->findRoute($url)) 
     598    { 
     599      // store the route name 
     600      $this->currentRouteName = $routeInfo['name']; 
     601      $this->currentRouteParameters = $routeInfo['parameters']; 
     602      $this->currentInternalUri = array(); 
     603 
     604      if ($this->options['logging']) 
     605      { 
     606        $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Match route [%s] for "%s"', $routeInfo['name'], $routeInfo['route'])))); 
     607      } 
     608    } 
     609    else 
     610    { 
     611      throw new sfError404Exception(sprintf('No matching route found for "%s"', $url)); 
     612    } 
     613 
     614    return $this->currentRouteParameters; 
     615  } 
     616   
     617   
     618  /** 
     619  * Finds a matching route for given URL. 
     620  * Returned array contains: 
     621  *  - name       : name or alias of the route that matched 
     622  *  - route      : the actual matching route 
     623  *  - parameters : array containing key value pairs of the request parameters including defaults 
     624  * 
     625  * @param  string URL to be parsed 
     626  * 
     627  * @return array  An array with routing information or null if no route matched 
     628  */ 
     629  public function findRoute($url) 
     630  { 
    597631    // an URL should start with a '/', mod_rewrite doesn't respect that, but no-mod_rewrite version does. 
    598632    if ('/' != $url[0]) 
     
    619653    } 
    620654 
    621     $found = false
     655    $routeInfo = null
    622656    foreach ($this->routes as $routeName => $route) 
    623657    { 
     
    629663 
    630664      $defaults = array_merge($defaults, $this->defaultParameters); 
    631       $found    = true; 
    632665      $out      = array(); 
    633666 
     
    651684      } 
    652685 
    653       // store the route name 
    654       $this->currentRouteName = $routeName; 
    655       $this->currentInternalUri = array(); 
    656  
    657       if ($this->options['logging']) 
    658       { 
    659         $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Match route [%s] for "%s"', $routeName, $route)))); 
    660       } 
    661  
     686      $routeInfo['name'] = $routeName; 
     687      $routeInfo['route'] = $route; 
     688      $routeInfo['parameters'] = $this->fixDefaults($out); 
     689      if (!is_null($this->cache)) 
     690      { 
     691        $this->cacheChanged = true; 
     692        $this->cacheData[$cacheKey] = $routeInfo; 
     693      } 
    662694      break; 
    663695    } 
    664696 
    665     // no route found 
    666     if (!$found) 
    667     { 
    668       throw new sfError404Exception(sprintf('No matching route found for "%s"', $url)); 
    669     } 
    670  
    671     $this->currentRouteParameters = $this->fixDefaults($out); 
    672  
    673     if (!is_null($this->cache)) 
    674     { 
    675       $this->cacheChanged = true; 
    676       $this->cacheData[$cacheKey] = $this->currentRouteParameters; 
    677     } 
    678  
    679     return $this->currentRouteParameters; 
     697    return $routeInfo; 
    680698  } 
    681699 
  • branches/1.1/lib/routing/sfRouting.class.php

    r8464 r8806  
    148148 
    149149 /** 
    150   * Parses a URL to find a matching route
     150  * Parses a URL to find a matching route and sets internal state
    151151  * 
    152152  * Throws a sfError404Exception if no route match the URL. 
  • branches/1.1/test/unit/routing/sfPatternRoutingTest.php

    r7901 r8806  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(129, new lime_output_color()); 
     13$t = new lime_test(135, new lime_output_color()); 
    1414 
    1515class sfPatternRoutingTest extends sfPatternRouting 
     
    453453$t->is($r->getCurrentInternalUri(false), 'foo/bar?id=2', '->getCurrentInternalUri() returns the internal URI for last parsed URL'); 
    454454 
     455// regression for ticket #3423  occuring when cache is used. (for the test its enough to have it non null) 
     456$rCached = new sfPatternRoutingTest(new sfEventDispatcher(),new sfNoCache()); 
     457$rCached->connect('test',  '/:module', array('action' => 'index')); 
     458$rCached->connect('test2', '/', array()); 
     459$rCached->parse('/'); 
     460$t->is($rCached->getCurrentInternalUri(), 'default/index', '->getCurrentInternalUri() returns the internal URI for last parsed URL using cache'); 
     461$rCached->parse('/test'); 
     462$t->is($rCached->getCurrentInternalUri(), 'test/index', '->getCurrentInternalUri() returns the internal URI for last parsed URL using cache'); 
     463$rCached->parse('/'); 
     464$t->is($rCached->getCurrentInternalUri(), 'default/index', '->getCurrentInternalUri() returns the internal URI for last parsed URL using cache'); 
     465// findRoute was added to be the side effectless version to check an uri 
     466$t->is($rCached->findRoute('/test'), 
     467       array('name' => 'test', 'route' => '/:module', 'parameters' => array('action' => 'index', 'module' => 'test')), 
     468       '->findRoute() returns information about matching route'); 
     469$t->is($rCached->getCurrentInternalUri(), 'default/index', '->findRoute() does not change the internal URI of sfPatternRouting'); 
     470$t->is($rCached->findRoute('/no/match/found'), null, '->findRoute() returns null on non-matching route'); 
     471 
    455472// defaults 
    456473$t->diag('defaults');