diff --git a/Slim/Route.php b/Slim/Route.php index 113603554..ae87e27f6 100644 --- a/Slim/Route.php +++ b/Slim/Route.php @@ -92,8 +92,8 @@ class Route /** * Constructor - * @param string $pattern The URL pattern (e.g. "/books/:id") - * @param mixed $callable Anything that returns TRUE for is_callable() + * @param string $pattern The URL pattern (e.g. "/books/:id") + * @param mixed $callable Anything that returns TRUE for is_callable() */ public function __construct($pattern, $callable) { @@ -154,6 +154,11 @@ public function getCallable() */ public function setCallable($callable) { + $matches = array(); + if (is_string($callable) && preg_match('!^([^\:]+)\:([[:alnum:]]+)$!', $callable, $matches)) { + $callable = array(new $matches[1], $matches[2]); + } + if (!is_callable($callable)) { throw new \InvalidArgumentException('Route callable must be callable'); } @@ -194,7 +199,7 @@ public function getName() */ public function setName($name) { - $this->name = (string) $name; + $this->name = (string)$name; } /** @@ -217,7 +222,7 @@ public function setParams($params) /** * Get route parameter value - * @param string $index Name of URL parameter + * @param string $index Name of URL parameter * @return string * @throws \InvalidArgumentException If route parameter does not exist at index */ @@ -232,8 +237,8 @@ public function getParam($index) /** * Set route parameter value - * @param string $index Name of URL parameter - * @param mixed $value The new parameter value + * @param string $index Name of URL parameter + * @param mixed $value The new parameter value * @throws \InvalidArgumentException If route parameter does not exist at index */ public function setParam($index, $value) @@ -351,7 +356,7 @@ public function matches($resourceUri) $patternAsRegex = preg_replace_callback( '#:([\w]+)\+?#', array($this, 'matchesCallback'), - str_replace(')', ')?', (string) $this->pattern) + str_replace(')', ')?', (string)$this->pattern) ); if (substr($this->pattern, -1) === '/') { $patternAsRegex .= '?'; @@ -363,7 +368,7 @@ public function matches($resourceUri) } foreach ($this->paramNames as $name) { if (isset($paramValues[$name])) { - if (isset($this->paramNamesPath[ $name ])) { + if (isset($this->paramNamesPath[$name])) { $this->params[$name] = explode('/', urldecode($paramValues[$name])); } else { $this->params[$name] = urldecode($paramValues[$name]); @@ -376,17 +381,17 @@ public function matches($resourceUri) /** * Convert a URL parameter (e.g. ":id", ":id+") into a regular expression - * @param array $m URL parameters + * @param array $m URL parameters * @return string Regular expression for URL parameter */ protected function matchesCallback($m) { $this->paramNames[] = $m[1]; - if (isset($this->conditions[ $m[1] ])) { - return '(?P<' . $m[1] . '>' . $this->conditions[ $m[1] ] . ')'; + if (isset($this->conditions[$m[1]])) { + return '(?P<' . $m[1] . '>' . $this->conditions[$m[1]] . ')'; } if (substr($m[0], -1) === '+') { - $this->paramNamesPath[ $m[1] ] = 1; + $this->paramNamesPath[$m[1]] = 1; return '(?P<' . $m[1] . '>.+)'; } @@ -396,7 +401,7 @@ protected function matchesCallback($m) /** * Set route name - * @param string $name The name of the route + * @param string $name The name of the route * @return \Slim\Route */ public function name($name) @@ -408,7 +413,7 @@ public function name($name) /** * Merge route conditions - * @param array $conditions Key-value array of URL parameter conditions + * @param array $conditions Key-value array of URL parameter conditions * @return \Slim\Route */ public function conditions(array $conditions) @@ -434,6 +439,6 @@ public function dispatch() } $return = call_user_func_array($this->getCallable(), array_values($this->getParams())); - return ($return === false)? false : true; + return ($return === false) ? false : true; } } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 0e67d229e..61e60446d 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -67,6 +67,23 @@ public function testGetCallable() $this->assertSame($callable, $route->getCallable()); } + public function testGetCallableAsClass() + { + $route = new \Slim\Route('/foo', '\Slim\Router:getCurrentRoute'); + + $callable = $route->getCallable(); + $this->assertInstanceOf('\Slim\Router', $callable[0]); + $this->assertEquals('getCurrentRoute', $callable[1]); + } + + public function testGetCallableAsStaticMethod() + { + $route = new \Slim\Route('/bar', '\Slim\Slim::getInstance'); + + $callable = $route->getCallable(); + $this->assertEquals('\Slim\Slim::getInstance', $callable); + } + public function testSetCallable() { $callable = function () {