Browse Source

Router::parseExtensions() only works at the top of the routes.

The global methods still mostly work like before, but with limitations.
Router::parseExtensions() now should only be used at the top of the
routes file. After that, extensions() should be set on the individual
scopes.
mark_story 11 years ago
parent
commit
e05a9672f4
2 changed files with 9 additions and 16 deletions
  1. 5 2
      src/Routing/Router.php
  2. 4 14
      tests/TestCase/Routing/RouterTest.php

+ 5 - 2
src/Routing/Router.php

@@ -1085,10 +1085,14 @@ class Router {
  *   If you have no parameters, this argument can be a callable.
  * @param callable $callback The callback to invoke with the scoped collection.
  * @throws \InvalidArgumentException When an invalid callable is provided.
- * @return \Cake\Routing\ScopedRouteCollection The scoped collection that
+ * @return null|\Cake\Routing\ScopedRouteCollection The scoped collection that
  *   was created/used.
  */
 	public static function scope($path, $params = [], $callback = null) {
+		if ($params === [] && $callback === null && isset(static::$_pathScopes[$path])) {
+			return static::$_pathScopes[$path];
+		}
+
 		if ($callback === null) {
 			$callback = $params;
 			$params = [];
@@ -1107,7 +1111,6 @@ class Router {
 			static::$_pathScopes[$path]->merge($collection);
 		}
 		static::$_named += $collection->named();
-		return static::$_pathScopes[$path];
 	}
 
 /**

+ 4 - 14
tests/TestCase/Routing/RouterTest.php

@@ -286,11 +286,12 @@ class RouterTest extends TestCase {
  * @return void
  */
 	public function testMapResourcesWithExtension() {
+		Router::parseExtensions(['json', 'xml'], false);
+
 		$resources = Router::mapResources('Posts', ['_ext' => 'json']);
 		$this->assertEquals(['posts'], $resources);
 
 		$_SERVER['REQUEST_METHOD'] = 'GET';
-		Router::parseExtensions(['json', 'xml'], false);
 
 		$expected = array(
 			'plugin' => null,
@@ -317,15 +318,14 @@ class RouterTest extends TestCase {
  */
 	public function testMapResourcesConnectOptions() {
 		Plugin::load('TestPlugin');
-		$collection = new RouteCollection();
-		Router::setRouteCollection($collection);
 		Router::mapResources('Posts', array(
 			'connectOptions' => array(
 				'routeClass' => 'TestPlugin.TestRoute',
 				'foo' => '^(bar)$',
 			),
 		));
-		$route = $collection->get(0);
+		$routes = Router::scope('/');
+		$route = $routes->routes()[0];
 		$this->assertInstanceOf('TestPlugin\Routing\Route\TestRoute', $route);
 		$this->assertEquals('^(bar)$', $route->options['foo']);
 	}
@@ -1632,7 +1632,6 @@ class RouterTest extends TestCase {
  * @return void
  */
 	public function testSetExtensions() {
-		Router::extensions();
 		Router::parseExtensions('rss', false);
 		$this->assertContains('rss', Router::extensions());
 
@@ -1645,15 +1644,6 @@ class RouterTest extends TestCase {
 		$this->assertFalse(isset($result['_ext']));
 
 		Router::parseExtensions(array('xml'));
-		$result = Router::extensions();
-		$this->assertContains('rss', $result);
-		$this->assertContains('xml', $result);
-
-		$result = Router::parse('/posts.xml');
-		$this->assertEquals('xml', $result['_ext']);
-
-		$result = Router::parseExtensions(array('pdf'), false);
-		$this->assertEquals(array('pdf'), $result);
 	}
 
 /**