Fixing Router's constructor.

Adding static visibility to Router methods.
Adding a method for testing Router extensions.
Router::mapResources() now returns the mapped resources
Fixing visibility issues in test cases.
This commit is contained in:
Mark Story 2010-04-19 23:44:45 -04:00
parent ea7b32347d
commit e92e212b8d
2 changed files with 46 additions and 92 deletions
cake
libs
tests/cases/libs

View file

@ -156,7 +156,7 @@ class Router {
* *
* @return void * @return void
*/ */
function Router() { function __construct() {
$this->__setPrefixes(); $this->__setPrefixes();
} }
@ -182,10 +182,8 @@ class Router {
* Gets a reference to the Router object instance * Gets a reference to the Router object instance
* *
* @return Router Instance of the Router. * @return Router Instance of the Router.
* @access public
* @static
*/ */
function &getInstance() { public static function &getInstance() {
static $instance = array(); static $instance = array();
if (!$instance) { if (!$instance) {
@ -198,11 +196,9 @@ class Router {
* Gets the named route elements for use in app/config/routes.php * Gets the named route elements for use in app/config/routes.php
* *
* @return array Named route elements * @return array Named route elements
* @access public
* @see Router::$__named * @see Router::$__named
* @static
*/ */
function getNamedExpressions() { public static function getNamedExpressions() {
$self =& Router::getInstance(); $self =& Router::getInstance();
return $self->__named; return $self->__named;
} }
@ -253,10 +249,8 @@ class Router {
* shifted into the passed arguments. As well as supplying patterns for routing parameters. * shifted into the passed arguments. As well as supplying patterns for routing parameters.
* @see routes * @see routes
* @return array Array of routes * @return array Array of routes
* @access public
* @static
*/ */
function connect($route, $defaults = array(), $options = array()) { public static function connect($route, $defaults = array(), $options = array()) {
$self =& Router::getInstance(); $self =& Router::getInstance();
foreach ($self->__prefixes as $prefix) { foreach ($self->__prefixes as $prefix) {
@ -326,10 +320,8 @@ class Router {
* either regex strings to match, or arrays as seen above. * either regex strings to match, or arrays as seen above.
* @param array $options Allows to control all settings: separator, greedy, reset, default * @param array $options Allows to control all settings: separator, greedy, reset, default
* @return array * @return array
* @access public
* @static
*/ */
function connectNamed($named, $options = array()) { public static function connectNamed($named, $options = array()) {
$self =& Router::getInstance(); $self =& Router::getInstance();
if (isset($options['argSeparator'])) { if (isset($options['argSeparator'])) {
@ -371,10 +363,8 @@ class Router {
* *
* @param boolean $connect Set to true or false depending on whether you want or don't want default routes. * @param boolean $connect Set to true or false depending on whether you want or don't want default routes.
* @return void * @return void
* @access public
* @static
*/ */
function defaults($connect = true) { public static function defaults($connect = true) {
$self =& Router::getInstance(); $self =& Router::getInstance();
$self->__connectDefaults = $connect; $self->__connectDefaults = $connect;
} }
@ -390,11 +380,9 @@ class Router {
* *
* @param mixed $controller A controller name or array of controller names (i.e. "Posts" or "ListItems") * @param mixed $controller A controller name or array of controller names (i.e. "Posts" or "ListItems")
* @param array $options Options to use when generating REST routes * @param array $options Options to use when generating REST routes
* @return void * @return array Array of mapped resources
* @access public
* @static
*/ */
function mapResources($controller, $options = array()) { public static function mapResources($controller, $options = array()) {
$self =& Router::getInstance(); $self =& Router::getInstance();
$options = array_merge(array('prefix' => '/', 'id' => $self->__named['ID'] . '|' . $self->__named['UUID']), $options); $options = array_merge(array('prefix' => '/', 'id' => $self->__named['ID'] . '|' . $self->__named['UUID']), $options);
$prefix = $options['prefix']; $prefix = $options['prefix'];
@ -413,16 +401,15 @@ class Router {
} }
$self->__resourceMapped[] = $urlName; $self->__resourceMapped[] = $urlName;
} }
return $self->__resourceMapped;
} }
/** /**
* Returns the list of prefixes used in connected routes * Returns the list of prefixes used in connected routes
* *
* @return array A list of prefixes used in connected routes * @return array A list of prefixes used in connected routes
* @access public
* @static
*/ */
function prefixes() { public static function prefixes() {
$self =& Router::getInstance(); $self =& Router::getInstance();
return $self->__prefixes; return $self->__prefixes;
} }
@ -433,10 +420,8 @@ class Router {
* *
* @param string $url URL to be parsed * @param string $url URL to be parsed
* @return array Parsed elements from URL * @return array Parsed elements from URL
* @access public
* @static
*/ */
function parse($url) { public static function parse($url) {
$self =& Router::getInstance(); $self =& Router::getInstance();
if (!$self->__defaultsMapped && $self->__connectDefaults) { if (!$self->__defaultsMapped && $self->__connectDefaults) {
$self->__connectDefaultRoutes(); $self->__connectDefaultRoutes();
@ -610,10 +595,8 @@ class Router {
* *
* @param array $params Parameters and path information * @param array $params Parameters and path information
* @return void * @return void
* @access public
* @static
*/ */
function setRequestInfo($params) { public static function setRequestInfo($params) {
$self =& Router::getInstance(); $self =& Router::getInstance();
$defaults = array('plugin' => null, 'controller' => null, 'action' => null); $defaults = array('plugin' => null, 'controller' => null, 'action' => null);
$params[0] = array_merge($defaults, (array)$params[0]); $params[0] = array_merge($defaults, (array)$params[0]);
@ -634,10 +617,8 @@ class Router {
* *
* @param boolean $current Get current request parameter, useful when using requestAction * @param boolean $current Get current request parameter, useful when using requestAction
* @return array Parameter information * @return array Parameter information
* @access public
* @static
*/ */
function getParams($current = false) { public static function getParams($current = false) {
$self =& Router::getInstance(); $self =& Router::getInstance();
if ($current) { if ($current) {
return $self->__params[count($self->__params) - 1]; return $self->__params[count($self->__params) - 1];
@ -654,10 +635,8 @@ class Router {
* @param string $name Parameter name * @param string $name Parameter name
* @param boolean $current Current parameter, useful when using requestAction * @param boolean $current Current parameter, useful when using requestAction
* @return string Parameter value * @return string Parameter value
* @access public
* @static
*/ */
function getParam($name = 'controller', $current = false) { public static function getParam($name = 'controller', $current = false) {
$params = Router::getParams($current); $params = Router::getParams($current);
if (isset($params[$name])) { if (isset($params[$name])) {
return $params[$name]; return $params[$name];
@ -670,10 +649,8 @@ class Router {
* *
* @param boolean $current Current parameter, useful when using requestAction * @param boolean $current Current parameter, useful when using requestAction
* @return array * @return array
* @access public
* @static
*/ */
function getPaths($current = false) { public static function getPaths($current = false) {
$self =& Router::getInstance(); $self =& Router::getInstance();
if ($current) { if ($current) {
return $self->__paths[count($self->__paths) - 1]; return $self->__paths[count($self->__paths) - 1];
@ -688,11 +665,9 @@ class Router {
* Reloads default Router settings. Resets all class variables and * Reloads default Router settings. Resets all class variables and
* removes all connected routes. * removes all connected routes.
* *
* @access public
* @return void * @return void
* @static
*/ */
function reload() { public static function reload() {
$self =& Router::getInstance(); $self =& Router::getInstance();
foreach (get_class_vars('Router') as $key => $val) { foreach (get_class_vars('Router') as $key => $val) {
$self->{$key} = $val; $self->{$key} = $val;
@ -706,10 +681,8 @@ class Router {
* @param $which A zero-based array index representing the route to move. For example, * @param $which A zero-based array index representing the route to move. For example,
* if 3 routes have been added, the last route would be 2. * if 3 routes have been added, the last route would be 2.
* @return boolean Retuns false if no route exists at the position specified by $which. * @return boolean Retuns false if no route exists at the position specified by $which.
* @access public
* @static
*/ */
function promote($which = null) { public static function promote($which = null) {
$self =& Router::getInstance(); $self =& Router::getInstance();
if ($which === null) { if ($which === null) {
$which = count($self->routes) - 1; $which = count($self->routes) - 1;
@ -751,10 +724,8 @@ class Router {
* - escape - used when making urls embedded in html escapes query string '&' * - escape - used when making urls embedded in html escapes query string '&'
* - full - if true the full base URL will be prepended. * - full - if true the full base URL will be prepended.
* @return string Full translated URL with base path. * @return string Full translated URL with base path.
* @access public
* @static
*/ */
function url($url = null, $full = false) { public static function url($url = null, $full = false) {
$self =& Router::getInstance(); $self =& Router::getInstance();
$defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index'); $defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index');
@ -954,10 +925,8 @@ class Router {
* @param string $controller Name of controller being routed. Used in scoping. * @param string $controller Name of controller being routed. Used in scoping.
* @param string $action Name of action being routed. Used in scoping. * @param string $action Name of action being routed. Used in scoping.
* @return array * @return array
* @access public
* @static
*/ */
function getNamedElements($params, $controller = null, $action = null) { public static function getNamedElements($params, $controller = null, $action = null) {
$self =& Router::getInstance(); $self =& Router::getInstance();
$named = array(); $named = array();
@ -982,10 +951,8 @@ class Router {
* @param array $rule The rule(s) to apply, can also be a match string * @param array $rule The rule(s) to apply, can also be a match string
* @param string $context An array with additional context information (controller / action) * @param string $context An array with additional context information (controller / action)
* @return boolean * @return boolean
* @access public
* @static
*/ */
function matchNamed($param, $val, $rule, $context = array()) { public static function matchNamed($param, $val, $rule, $context = array()) {
if ($rule === true || $rule === false) { if ($rule === true || $rule === false) {
return $rule; return $rule;
} }
@ -1014,10 +981,8 @@ class Router {
* @param array $extra Extra querystring parameters. * @param array $extra Extra querystring parameters.
* @param bool $escape Whether or not to use escaped & * @param bool $escape Whether or not to use escaped &
* @return array * @return array
* @access public
* @static
*/ */
function queryString($q, $extra = array(), $escape = false) { public static function queryString($q, $extra = array(), $escape = false) {
if (empty($q) && empty($extra)) { if (empty($q) && empty($extra)) {
return null; return null;
} }
@ -1047,10 +1012,8 @@ class Router {
* *
* @param array $param The params array that needs to be reversed. * @param array $param The params array that needs to be reversed.
* @return string The string that is the reversed result of the array * @return string The string that is the reversed result of the array
* @access public
* @static
*/ */
function reverse($params) { public static function reverse($params) {
$pass = $params['pass']; $pass = $params['pass'];
$named = $params['named']; $named = $params['named'];
$url = $params['url']; $url = $params['url'];
@ -1069,10 +1032,8 @@ class Router {
* *
* @param mixed $url URL to normalize Either an array or a string url. * @param mixed $url URL to normalize Either an array or a string url.
* @return string Normalized URL * @return string Normalized URL
* @access public
* @static
*/ */
function normalize($url = '/') { public static function normalize($url = '/') {
if (is_array($url)) { if (is_array($url)) {
$url = Router::url($url); $url = Router::url($url);
} elseif (preg_match('/^[a-z\-]+:\/\//', $url)) { } elseif (preg_match('/^[a-z\-]+:\/\//', $url)) {
@ -1100,10 +1061,8 @@ class Router {
* Returns the route matching the current request URL. * Returns the route matching the current request URL.
* *
* @return CakeRoute Matching route object. * @return CakeRoute Matching route object.
* @access public
* @static
*/ */
function &requestRoute() { public static function &requestRoute() {
$self =& Router::getInstance(); $self =& Router::getInstance();
return $self->__currentRoute[0]; return $self->__currentRoute[0];
} }
@ -1112,10 +1071,8 @@ class Router {
* Returns the route matching the current request (useful for requestAction traces) * Returns the route matching the current request (useful for requestAction traces)
* *
* @return CakeRoute Matching route object. * @return CakeRoute Matching route object.
* @access public
* @static
*/ */
function &currentRoute() { public static function &currentRoute() {
$self =& Router::getInstance(); $self =& Router::getInstance();
return $self->__currentRoute[count($self->__currentRoute) - 1]; return $self->__currentRoute[count($self->__currentRoute) - 1];
} }
@ -1126,10 +1083,8 @@ class Router {
* @param string $base Base URL * @param string $base Base URL
* @param string $plugin Plugin name * @param string $plugin Plugin name
* @return base url with plugin name removed if present * @return base url with plugin name removed if present
* @access public
* @static
*/ */
function stripPlugin($base, $plugin = null) { public static function stripPlugin($base, $plugin = null) {
if ($plugin != null) { if ($plugin != null) {
$base = preg_replace('/(?:' . $plugin . ')/', '', $base); $base = preg_replace('/(?:' . $plugin . ')/', '', $base);
$base = str_replace('//', '', $base); $base = str_replace('//', '', $base);
@ -1155,11 +1110,9 @@ class Router {
* If no parameters are given, anything after the first . (dot) after the last / in the URL will be * If no parameters are given, anything after the first . (dot) after the last / in the URL will be
* parsed, excluding querystring parameters (i.e. ?q=...). * parsed, excluding querystring parameters (i.e. ?q=...).
* *
* @access public
* @return void * @return void
* @static
*/ */
function parseExtensions() { public static function parseExtensions() {
$self =& Router::getInstance(); $self =& Router::getInstance();
$self->__parseExtensions = true; $self->__parseExtensions = true;
if (func_num_args() > 0) { if (func_num_args() > 0) {
@ -1167,15 +1120,24 @@ class Router {
} }
} }
/**
* Get the list of extensions that can be parsed by Router. To add more
* extensions use Router::parseExtensions()
*
* @return array Array of extensions Router is configured to parse.
*/
public static function extensions() {
$self =& Router::getInstance();
return $self->__validExtensions;
}
/** /**
* Takes an passed params and converts it to args * Takes an passed params and converts it to args
* *
* @param array $params * @param array $params
* @return array Array containing passed and named parameters * @return array Array containing passed and named parameters
* @access public
* @static
*/ */
function getArgs($args, $options = array()) { public static function getArgs($args, $options = array()) {
$self =& Router::getInstance(); $self =& Router::getInstance();
$pass = $named = array(); $pass = $named = array();
$args = explode('/', $args); $args = explode('/', $args);

View file

@ -105,54 +105,47 @@ class RouterTest extends CakeTestCase {
* @return void * @return void
*/ */
function testResourceRoutes() { function testResourceRoutes() {
Router::mapResources('Posts'); $resources = Router::mapResources('Posts');
$_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_METHOD'] = 'GET';
$result = Router::parse('/posts'); $result = Router::parse('/posts');
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => 'GET')); $this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => 'GET'));
$this->assertEqual($this->router->__resourceMapped, array('posts')); $this->assertEqual($resources, array('posts'));
$_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_METHOD'] = 'GET';
$result = Router::parse('/posts/13'); $result = Router::parse('/posts/13');
$this->assertEqual($result, array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => '13', '[method]' => 'GET')); $this->assertEqual($result, array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => '13', '[method]' => 'GET'));
$this->assertEqual($this->router->__resourceMapped, array('posts'));
$_SERVER['REQUEST_METHOD'] = 'POST'; $_SERVER['REQUEST_METHOD'] = 'POST';
$result = Router::parse('/posts'); $result = Router::parse('/posts');
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST')); $this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST'));
$this->assertEqual($this->router->__resourceMapped, array('posts'));
$_SERVER['REQUEST_METHOD'] = 'PUT'; $_SERVER['REQUEST_METHOD'] = 'PUT';
$result = Router::parse('/posts/13'); $result = Router::parse('/posts/13');
$this->assertEqual($result, array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT')); $this->assertEqual($result, array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT'));
$this->assertEqual($this->router->__resourceMapped, array('posts'));
$result = Router::parse('/posts/475acc39-a328-44d3-95fb-015000000000'); $result = Router::parse('/posts/475acc39-a328-44d3-95fb-015000000000');
$this->assertEqual($result, array('pass' => array('475acc39-a328-44d3-95fb-015000000000'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '475acc39-a328-44d3-95fb-015000000000', '[method]' => 'PUT')); $this->assertEqual($result, array('pass' => array('475acc39-a328-44d3-95fb-015000000000'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '475acc39-a328-44d3-95fb-015000000000', '[method]' => 'PUT'));
$this->assertEqual($this->router->__resourceMapped, array('posts'));
$_SERVER['REQUEST_METHOD'] = 'DELETE'; $_SERVER['REQUEST_METHOD'] = 'DELETE';
$result = Router::parse('/posts/13'); $result = Router::parse('/posts/13');
$this->assertEqual($result, array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE')); $this->assertEqual($result, array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE'));
$this->assertEqual($this->router->__resourceMapped, array('posts'));
$_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_METHOD'] = 'GET';
$result = Router::parse('/posts/add'); $result = Router::parse('/posts/add');
$this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add')); $this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add'));
$this->assertEqual($this->router->__resourceMapped, array('posts'));
Router::reload(); Router::reload();
Router::mapResources('Posts', array('id' => '[a-z0-9_]+')); $resources = Router::mapResources('Posts', array('id' => '[a-z0-9_]+'));
$this->assertEqual($resources, array('posts'));
$_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_METHOD'] = 'GET';
$result = Router::parse('/posts/add'); $result = Router::parse('/posts/add');
$this->assertEqual($result, array('pass' => array('add'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => 'add', '[method]' => 'GET')); $this->assertEqual($result, array('pass' => array('add'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => 'add', '[method]' => 'GET'));
$this->assertEqual($this->router->__resourceMapped, array('posts'));
$_SERVER['REQUEST_METHOD'] = 'PUT'; $_SERVER['REQUEST_METHOD'] = 'PUT';
$result = Router::parse('/posts/name'); $result = Router::parse('/posts/name');
$this->assertEqual($result, array('pass' => array('name'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => 'name', '[method]' => 'PUT')); $this->assertEqual($result, array('pass' => array('name'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => 'name', '[method]' => 'PUT'));
$this->assertEqual($this->router->__resourceMapped, array('posts'));
} }
/** /**
@ -1165,11 +1158,10 @@ class RouterTest extends CakeTestCase {
* @return void * @return void
*/ */
function testExtensionParsingSetting() { function testExtensionParsingSetting() {
$router =& Router::getInstance(); $this->assertFalse(Router::extensions());
$this->assertFalse($this->router->__parseExtensions);
$router->parseExtensions(); $router->parseExtensions('rss');
$this->assertTrue($this->router->__parseExtensions); $this->assertEqual(Router::extensions(), array('rss'));
} }
/** /**