Skip to content
Permalink
Browse files

Pass default route class as an option to RouteBuilder constructor.

  • Loading branch information...
ADmad committed Sep 1, 2014
1 parent 06d0552 commit 8933c770b81fdb6d9bd8490e572a7d270bce3612
Showing with 41 additions and 25 deletions.
  1. +26 −17 src/Routing/RouteBuilder.php
  2. +4 −1 src/Routing/Router.php
  3. +11 −7 tests/TestCase/Routing/RouteBuilderTest.php
@@ -57,18 +57,18 @@ class RouteBuilder {
];
/**
* Route class to use if none is provided in connect() options.
* Default route class to use if none is provided in connect() options.
*
* @var string
*/
protected $_routeClass;
protected $_routeClass = 'Cake\Routing\Route\Route';
/**
* The extensions that should be set into the routes connected.
*
* @var array
*/
protected $_extensions;
protected $_extensions = [];
/**
* The path prefix scope that this collection uses.
@@ -97,14 +97,21 @@ class RouteBuilder {
* @param \Cake\Routing\RouteCollection $collection The route collection to append routes into.
* @param string $path The path prefix the scope is for.
* @param array $params The scope's routing parameters.
* @param array $extensions The extensions to connect when adding routes.
* @param array $options Options list. Valid keys are:
*
* - `routeClass` - The default route class to use when adding routes.
* - `extensions` - The extensions to connect when adding routes.
*/
public function __construct($collection, $path, array $params = [], array $extensions = []) {
public function __construct($collection, $path, array $params = [], array $options = []) {
$this->_collection = $collection;
$this->_path = $path;
$this->_params = $params;
$this->_extensions = $extensions;
$this->_routeClass = Router::defaultRouteClass();
if (isset($options['routeClass'])) {
$this->_routeClass = $options['routeClass'];
}
if (isset($options['extensions'])) {
$this->_extensions = $options['extensions'];
}
}
/**
@@ -344,6 +351,10 @@ public function connect($route, array $defaults = [], $options = []) {
$options['_ext'] = $this->_extensions;
}
if (empty($options['routeClass'])) {
$options['routeClass'] = $this->_routeClass;
}
$route = $this->_makeRoute($route, $defaults, $options);
$this->_collection->add($route, $options);
}
@@ -360,16 +371,11 @@ public function connect($route, array $defaults = [], $options = []) {
*/
protected function _makeRoute($route, $defaults, $options) {
if (is_string($route)) {
$routeClass = $this->_routeClass;
if (isset($options['routeClass'])) {
$routeClass = $options['routeClass'];
unset($options['routeClass']);
}
$class = App::className($routeClass, 'Routing/Route');
if ($class === false) {
$routeClass = App::className($options['routeClass'], 'Routing/Route');
if ($routeClass === false) {
throw new InvalidArgumentException(sprintf(
'Cannot find route class %s',
$routeClass
$options['routeClass']
));
}
@@ -392,7 +398,7 @@ protected function _makeRoute($route, $defaults, $options) {
$defaults += $this->_params;
$defaults += ['plugin' => null];
$route = new $class($route, $defaults, $options);
$route = new $routeClass($route, $defaults, $options);
}
if ($route instanceof Route) {
@@ -528,7 +534,10 @@ public function scope($path, $params, $callback = null) {
$path = $this->_path . $path;
}
$params = $params + $this->_params;
$builder = new static($this->_collection, $path, $params, $this->_extensions);
$builder = new static($this->_collection, $path, $params, [
'routeClass' => $this->_routeClass,
'extensions' => $this->_extensions
]);
$callback($builder);
}
@@ -842,7 +842,10 @@ public static function parseNamedParams(Request $request, $options = []) {
* was created/used.
*/
public static function scope($path, $params = [], $callback = null) {
$builder = new RouteBuilder(static::$_collection, '/', [], static::$_collection->extensions());
$builder = new RouteBuilder(static::$_collection, '/', [], [
'routeClass' => static::defaultRouteClass(),
'extensions' => static::$_collection->extensions()
]);
$builder->scope($path, $params, $callback);
}
@@ -86,8 +86,9 @@ public function testRoutes() {
* @return void
*/
public function testRouteClass() {
$routes = new RouteBuilder($this->collection, '/l');
$routes->routeClass('InflectedRoute');
$routes = new RouteBuilder($this->collection, '/l', [],
['routeClass' => 'InflectedRoute']
);
$routes->connect('/:controller', ['action' => 'index']);
$routes->connect('/:controller/:action/*');
@@ -96,9 +97,8 @@ public function testRouteClass() {
$this->assertInstanceOf('Cake\Routing\Route\InflectedRoute', $all[1]);
$this->collection = new RouteCollection();
Router::defaultRouteClass('TestApp\Routing\Route\DashedRoute');
$routes = new RouteBuilder($this->collection, '/l');
$this->assertEquals('TestApp\Routing\Route\DashedRoute', $routes->routeClass());
$routes->routeClass('TestApp\Routing\Route\DashedRoute');
$routes->connect('/:controller', ['action' => 'index']);
$all = $this->collection->routes();
@@ -157,7 +157,9 @@ public function testConnectTrimTrailingSlash() {
* @return void
*/
public function testConnectExtensions() {
$routes = new RouteBuilder($this->collection, '/l', [], ['json']);
$routes = new RouteBuilder($this->collection, '/l', [],
['extensions' => ['json']]
);
$this->assertEquals(['json'], $routes->extensions());
$routes->connect('/:controller');
@@ -192,7 +194,9 @@ public function testExtensionsString() {
* @return void
*/
public function testConnectErrorInvalidRouteClass() {
$routes = new RouteBuilder($this->collection, '/l', [], ['json']);
$routes = new RouteBuilder($this->collection, '/l', [],
['extensions' => ['json']]
);
$routes->connect('/:controller', [], ['routeClass' => '\StdClass']);
}
@@ -204,7 +208,7 @@ public function testConnectErrorInvalidRouteClass() {
* @return void
*/
public function testConnectConflictingParameters() {
$routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin'], []);
$routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
$routes->connect('/', ['prefix' => 'manager', 'controller' => 'Dashboard', 'action' => 'view']);
}

0 comments on commit 8933c77

Please sign in to comment.
You can’t perform that action at this time.