Skip to content
Permalink
Browse files

Allow setting default route class for RouteBuilder.

  • Loading branch information...
ADmad committed Aug 31, 2014
1 parent a15c2c4 commit 06d0552a75969283794cbda4ccca210a6ce77c3b
Showing with 64 additions and 3 deletions.
  1. +24 −3 src/Routing/RouteBuilder.php
  2. +40 −0 tests/TestCase/Routing/RouteBuilderTest.php
@@ -56,6 +56,13 @@ class RouteBuilder {
'delete' => ['action' => 'delete', 'method' => 'DELETE', 'path' => ':id'],
];
/**
* Route class to use if none is provided in connect() options.
*
* @var string
*/
protected $_routeClass;
/**
* The extensions that should be set into the routes connected.
*
@@ -97,6 +104,20 @@ public function __construct($collection, $path, array $params = [], array $exten
$this->_path = $path;
$this->_params = $params;
$this->_extensions = $extensions;
$this->_routeClass = Router::defaultRouteClass();
}
/**
* Get or set default route class.
*
* @param string|null $routeClass Class name.
* @return string|void
*/
public function routeClass($routeClass = null) {
if ($routeClass == null) {
return $this->_routeClass;
}
$this->_routeClass = $routeClass;
}
/**
@@ -339,9 +360,10 @@ public function connect($route, array $defaults = [], $options = []) {
*/
protected function _makeRoute($route, $defaults, $options) {
if (is_string($route)) {
$routeClass = Router::defaultRouteClass();
$routeClass = $this->_routeClass;
if (isset($options['routeClass'])) {
$routeClass = $options['routeClass'];
unset($options['routeClass']);
}
$class = App::className($routeClass, 'Routing/Route');
if ($class === false) {
@@ -350,7 +372,6 @@ protected function _makeRoute($route, $defaults, $options) {
$routeClass
));
}
unset($options['routeClass']);
$route = str_replace('//', '/', $this->_path . $route);
$route = $route === '/' ? $route : rtrim($route, '/');
@@ -519,7 +540,7 @@ public function scope($path, $params, $callback = null) {
* @return void
*/
public function fallbacks() {
$routeClass = Router::defaultRouteClass();
$routeClass = $this->_routeClass;
if ($routeClass === 'Cake\Routing\Route\Route') {
$routeClass = 'InflectedRoute';
}
@@ -80,6 +80,31 @@ public function testRoutes() {
$this->assertInstanceOf('Cake\Routing\Route\Route', $all[1]);
}
/**
* Test setting default route class
*
* @return void
*/
public function testRouteClass() {
$routes = new RouteBuilder($this->collection, '/l');
$routes->routeClass('InflectedRoute');
$routes->connect('/:controller', ['action' => 'index']);
$routes->connect('/:controller/:action/*');
$all = $this->collection->routes();
$this->assertInstanceOf('Cake\Routing\Route\InflectedRoute', $all[0]);
$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->connect('/:controller', ['action' => 'index']);
$all = $this->collection->routes();
$this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
}
/**
* Test connecting an instance routes.
*
@@ -406,6 +431,21 @@ public function testFallbacks() {
$all = $this->collection->routes();
$this->assertEquals('/api/:controller', $all[0]->template);
$this->assertEquals('/api/:controller/:action/*', $all[1]->template);
$this->assertInstanceOf('Cake\Routing\Route\InflectedRoute', $all[0]);
}
/**
* Test connecting fallback routes after setting default route class.
*
* @return void
*/
public function testDefaultRouteClassFallbacks() {
$routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
$routes->routeClass('TestApp\Routing\Route\DashedRoute');
$routes->fallbacks();
$all = $this->collection->routes();
$this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
}
/**

0 comments on commit 06d0552

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