Skip to content

Commit

Permalink
Make extensions propagate.
Browse files Browse the repository at this point in the history
Calling parseExtensions() after routes are connected should
be reflected in those routes.
  • Loading branch information
markstory committed Jul 4, 2012
1 parent 2f7ab5e commit fd097cb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 56 deletions.
10 changes: 10 additions & 0 deletions lib/Cake/Routing/Route/Route.php
Expand Up @@ -121,6 +121,16 @@ public function __construct($template, $defaults = array(), $options = array())
}
}

/**
* Sets the supported extensions for this route.
*
* @param array $extensions The extensions to set.
* @return void
*/
public function setExtensions(array $extensions) {
$this->_extensions = $extensions;
}

/**
* Check if a Route has been compiled into a regular expression.
*
Expand Down
18 changes: 14 additions & 4 deletions lib/Cake/Routing/RouteCollection.php
Expand Up @@ -139,10 +139,8 @@ protected function _getNames($url) {
*/
public function parse($url) {
$out = array();
for ($i = 0, $len = count($this->_routes); $i < $len; $i++) {
$route = $this->_routes[$i];

if (($r = $route->parse($url)) !== false) {
for ($i = 0, $len = count($this); $i < $len; $i++) {
if (($r = $this->_routes[$i]->parse($url)) !== false) {
$out = $r;
break;
}
Expand Down Expand Up @@ -218,4 +216,16 @@ public function setContext(Request $request) {
);
}

/**
* Sets which extensions routes will use.
*
* @param array $extensions The extensions for routes to use.
* @return void
*/
public function setExtensions(array $extensions) {
foreach ($this->_routes as $route) {
$route->setExtensions($extensions);
}
}

}
64 changes: 18 additions & 46 deletions lib/Cake/Routing/Router.php
Expand Up @@ -58,13 +58,6 @@ class Router {
*/
protected static $_prefixes = array();

/**
* Directive for Router to parse out file extensions for mapping to Content-types.
*
* @var boolean
*/
protected static $_parseExtensions = false;

/**
* List of valid extensions to parse from a URL. If null, any extension is allowed.
*
Expand Down Expand Up @@ -292,7 +285,7 @@ public static function connect($route, $defaults = array(), $options = array())
$defaults += array('action' => 'index');
}
if (empty($options['_ext'])) {
$options['_ext'] = self::$_validExtensions;
$options['_ext'] = static::$_validExtensions;
}
$routeClass = static::$_routeClass;
if (isset($options['routeClass'])) {
Expand Down Expand Up @@ -439,7 +432,7 @@ public static function parse($url) {
* @return void
*/
public static function setRouteCollection(RouteCollection $routes) {
self::$_routes = $routes;
static::$_routes = $routes;
}

/**
Expand Down Expand Up @@ -481,8 +474,8 @@ public static function setRequestInfo($request) {
* @return void
*/
public static function pushRequest(Request $request) {
self::$_requests[] = $request;
self::$_routes->setContext($request);
static::$_requests[] = $request;
static::$_routes->setContext($request);
}

/**
Expand Down Expand Up @@ -575,8 +568,8 @@ public static function promote($which = null) {
* @param callable $function The function to add
* @return void
*/
public static function addUrlFilter($function) {
self::$_urlFilters[] = $function;
public static function addUrlFilter(callable $function) {
static::$_urlFilters[] = $function;
}

/**
Expand All @@ -588,8 +581,8 @@ public static function addUrlFilter($function) {
* @see Router::addUrlFilter()
*/
protected static function _applyUrlFilters($url) {
$request = self::getRequest(true);
foreach (self::$_urlFilters as $filter) {
$request = static::getRequest(true);
foreach (static::$_urlFilters as $filter) {
$url = $filter($url, $request);
}
return $url;
Expand Down Expand Up @@ -654,7 +647,7 @@ public static function url($url = null, $options = array()) {
strpos($url, '/') === false &&
!$plainString
) {
$url = self::_splitName($url, $options);
$url = static::_splitName($url, $options);
$urlType = 'array';
}

Expand Down Expand Up @@ -730,16 +723,16 @@ public static function url($url = null, $options = array()) {
'controller' => $params['controller'],
'plugin' => $params['plugin']
);
$url = self::_applyUrlFilters($url);
$output = self::$_routes->match($url);
$url = static::_applyUrlFilters($url);
$output = static::$_routes->match($url);
} elseif (
$urlType === 'string' &&
!$hasLeadingSlash &&
!$hasColonSlash &&
!$plainString
) {
// named route.
$route = self::$_routes->get($url);
$route = static::$_routes->get($url);
if (!$route) {
throw new Error\Exception(__d(
'cake_dev',
Expand All @@ -750,8 +743,8 @@ public static function url($url = null, $options = array()) {
$url = $options +
$route->defaults +
array('_name' => $url);
$url = self::_applyUrlFilters($url);
$output = self::$_routes->match($url);
$url = static::_applyUrlFilters($url);
$output = static::$_routes->match($url);
} else {
// String urls.
if ($hasColonSlash || $plainString) {
Expand All @@ -767,9 +760,6 @@ public static function url($url = null, $options = array()) {
if ($full && defined('FULL_BASE_URL')) {
$output = FULL_BASE_URL . $output;
}
if (!empty($extension)) {
$output = rtrim($output, '/');
}
}
return $output . $frag;
}
Expand Down Expand Up @@ -880,7 +870,6 @@ public static function normalize($url = '/') {
* @see RequestHandler::startup()
*/
public static function parseExtensions() {
static::$_parseExtensions = true;
if (func_num_args() > 0) {
static::setExtensions(func_get_args(), false);
}
Expand All @@ -898,10 +887,11 @@ public static function setExtensions($extensions, $merge = true) {
if (!is_array($extensions)) {
return static::$_validExtensions;
}
if (!$merge) {
return static::$_validExtensions = $extensions;
if ($merge) {
$extensions = array_merge(static::$_validExtensions, $extensions);
}
return static::$_validExtensions = array_merge(static::$_validExtensions, $extensions);
static::$_routes->setExtensions($extensions);
return static::$_validExtensions = $extensions;
}

/**
Expand All @@ -915,24 +905,6 @@ public static function extensions() {
return static::$_validExtensions;
}

/**
* Set/add valid extensions.
* To have the extensions parsed you still need to call `Router::parseExtensions()`
*
* @param array $extensions List of extensions to be added as valid extension
* @param boolean $merge Default true will merge extensions. Set to false to override current extensions
* @return array
*/
public static function setExtensions($extensions, $merge = true) {
if (!is_array($extensions)) {
return static::$_validExtensions;
}
if (!$merge) {
return static::$_validExtensions = $extensions;
}
return static::$_validExtensions = array_merge(static::$_validExtensions, $extensions);
}

/**
* Provides legacy support for named parameters on incoming URLs.
*
Expand Down
4 changes: 4 additions & 0 deletions lib/Cake/Test/TestCase/Routing/Route/RouteTest.php
Expand Up @@ -110,6 +110,10 @@ public function testRouteParsingWithExtensions() {
$result = $route->parse('/posts/index.pdf');
$this->assertFalse(isset($result['_ext']));

$route->setExtensions(array('pdf', 'json', 'xml'));
$result = $route->parse('/posts/index.pdf');
$this->assertEquals('pdf', $result['_ext']);

$result = $route->parse('/posts/index.json');
$this->assertEquals('json', $result['_ext']);

Expand Down
9 changes: 3 additions & 6 deletions lib/Cake/Test/TestCase/Routing/RouterTest.php
Expand Up @@ -1185,22 +1185,19 @@ public function testSetExtensions() {
$this->assertEquals(array('rss'), Router::extensions());

require CAKE . 'Config' . DS . 'routes.php';
$result = Router::parse('/posts.rss');
$this->assertFalse(isset($result['ext']));

Router::parseExtensions();
$result = Router::parse('/posts.rss');
$this->assertEquals('rss', $result['ext']);
$this->assertEquals('rss', $result['_ext']);

$result = Router::parse('/posts.xml');
$this->assertFalse(isset($result['ext']));
$this->assertFalse(isset($result['_ext']));

Router::setExtensions(array('xml'));
$result = Router::extensions();
$this->assertEquals(array('rss', 'xml'), $result);

$result = Router::parse('/posts.xml');
$this->assertEquals('xml', $result['ext']);
$this->assertEquals('xml', $result['_ext']);

$result = Router::setExtensions(array('pdf'), false);
$this->assertEquals(array('pdf'), $result);
Expand Down

0 comments on commit fd097cb

Please sign in to comment.