Fixed typo in DocBlock

Router::defaultRouteClass will return the current routeClass if null is
passed
Router::connect and Router::defaultRouteClass both validate the routeClass
now using Router::_validateRouteClass
This commit is contained in:
Tigran Gabrielyan 2012-01-13 12:27:48 -08:00
parent b14072aa6f
commit f5b62eb84c
2 changed files with 73 additions and 13 deletions

View file

@ -160,14 +160,33 @@ class Router {
protected static $_routeClass = 'CakeRoute';
/**
* Set the default rotue class to use
* Set the default route class to use or return the current one
*
* @param sting $routeClass to set as default
* @return void
* @param string $routeClass to set as default
* @return mixed void|string
* @throws RouterException
*/
public static function defaultRouteClass($routeClass) {
self::$_routeClass = $routeClass;
}
public static function defaultRouteClass($routeClass = null) {
if (is_null($routeClass)) {
return self::$_routeClass;
}
self::$_routeClass = self::_validateRouteClass($routeClass);
}
/**
* Validates that the passed route class exists and is a subclass of CakeRoute
*
* @param $routeClass
* @return string
* @throws RouterException
*/
protected static function _validateRouteClass($routeClass) {
if (!class_exists($routeClass) || !is_subclass_of($routeClass, 'CakeRoute')) {
throw new RouterException(__d('cake_dev', 'Route classes must extend CakeRoute'));
}
return $routeClass;
}
/**
* Sets the Routing prefixes.
@ -278,14 +297,11 @@ class Router {
}
$routeClass = self::$_routeClass;
if (isset($options['routeClass'])) {
$routeClass = $options['routeClass'];
if (!is_subclass_of($routeClass, 'CakeRoute')) {
throw new RouterException(__d('cake_dev', 'Route classes must extend CakeRoute'));
}
$routeClass = self::_validateRouteClass($options['routeClass']);
unset($options['routeClass']);
if ($routeClass == 'RedirectRoute' && isset($defaults['redirect'])) {
$defaults = $defaults['redirect'];
}
}
if ($routeClass == 'RedirectRoute' && isset($defaults['redirect'])) {
$defaults = $defaults['redirect'];
}
self::$routes[] = new $routeClass($route, $defaults, $options);
return self::$routes;

View file

@ -2519,4 +2519,48 @@ class RouterTest extends CakeTestCase {
$this->assertInstanceOf('TestDefaultRouteClass', $result[0]);
}
/**
* Test getting the default route class
*
* @return void
*/
public function testDefaultRouteClassGetter() {
$routeClass = 'TestDefaultRouteClass';
Router::defaultRouteClass($routeClass);
$this->assertEqual($routeClass, Router::defaultRouteClass());
$this->assertEqual($routeClass, Router::defaultRouteClass(null));
}
/**
* Test that route classes must extend CakeRoute
*
* @expectedException RouterException
* @return void
*/
public function testDefaultRouteException() {
Router::defaultRouteClass('');
Router::connect('/:controller', array());
}
/**
* Test that route classes must extend CakeRoute
*
* @expectedException RouterException
* @return void
*/
public function testSettingInvalidDefaultRouteException() {
Router::defaultRouteClass('Object');
}
/**
* Test that class must exist
*
* @expectedException RouterException
* @return void
*/
public function testSettingNonExistentDefaultRouteException() {
Router::defaultRouteClass('NonExistentClass');
}
}