Skip to content

Commit

Permalink
Merge remote-tracking branch 'renatbilalov/master' into release-2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Lockhart committed Nov 29, 2013
2 parents 8956a5f + fc816fb commit b33f084
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
35 changes: 20 additions & 15 deletions Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -194,7 +199,7 @@ public function getName()
*/
public function setName($name)
{
$this->name = (string) $name;
$this->name = (string)$name;
}

/**
Expand All @@ -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
*/
Expand All @@ -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)
Expand Down Expand Up @@ -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 .= '?';
Expand All @@ -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]);
Expand All @@ -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] . '>.+)';
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}
}
17 changes: 17 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit b33f084

Please sign in to comment.