Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Respect\Rest\Router as target. #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 51 additions & 14 deletions library/Respect/Rest/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public function __call($method, $args)
} elseif ($routeTarget instanceof Routable) {
return $this->instanceRoute($method, $path, $routeTarget);

} elseif ($routeTarget instanceof Router) {
return $this->instanceSubRouter($method, $path, $routeTarget);

//static returns the argument itself
} elseif (!is_string($routeTarget)) {
return $this->staticRoute($method, $path, $routeTarget);
Expand Down Expand Up @@ -479,6 +482,24 @@ public function instanceRoute($method, $path, $instance)
return $route;
}


/**
* Creates and returns an router-based route
*
* @param string $method The HTTP metod (GET, POST, etc)
* @param string $path The URI Path (/foo/bar...)
* @param string $intance An instance of Router
*
* @return Respect\Rest\Routes\SubRouter The route created
*/
public function instanceSubRouter($method, $path, $instance)
{
$route = new Routes\SubRouter($method, $path, $instance);
$this->appendRoute($route);

return $route;
}

/**
* Checks if request is a global OPTIONS (OPTIONS * HTTP/1.1)
*
Expand Down Expand Up @@ -756,20 +777,36 @@ protected function sortRoutesByComplexity()
function ($a, $b) {
$a = $a->pattern;
$b = $b->pattern;
$pi = AbstractRoute::PARAM_IDENTIFIER;

//Compare similarity and ocurrences of "/"
if (Router::compareRoutePatterns($a, $b, '/')) {
return 1;

//Compare similarity and ocurrences of /*
} elseif (Router::compareRoutePatterns($a, $b, $pi)) {
return -1;

//Hard fallback for consistency
} else {
return 1;
}
$elementsa = preg_split('#/#', $a, 0, PREG_SPLIT_NO_EMPTY);
$elementsb = preg_split('#/#', $b, 0, PREG_SPLIT_NO_EMPTY);

if(end($elementsa) == '**' && end($elementsb) == '**')
return count($elementsa) < count($elementsb);
if(end($elementsa) == '**')
return 1;
if(end($elementsb) == '**')
return -1;

if(count($elementsa) < count($elementsb)){
return -1;
}
if(count($elementsa) > count($elementsb)){
return 1;
}
$keysa = array_keys($elementsa, '*');
$keysb = array_keys($elementsb, '*');
if(count($keysa) < count($keysb)){
return -1;
}
if(count($keysa) > count($keysb)){
return 1;
}

for($index=0; $index < count($keysa); $index++){
if($keysa[$index] == $keysb[$index])
continue;
return $keysa[$index]<$keysb[$index];
}
}
);
}
Expand Down
60 changes: 60 additions & 0 deletions library/Respect/Rest/Routes/SubRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Respect\Rest package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Respect\Rest\Routes;

use ReflectionMethod;
use InvalidArgumentException;
use Respect\Rest\Router;
use Respect\Rest\Request;

class SubRouter extends AbstractRoute
{
protected $uri = null;
protected $instance = null;
/** @var ReflectionMethod */
protected $reflection;

public function __construct($method, $pattern, $instance)
{
$this->instance = $instance;
parent::__construct($method, $pattern);
}

public function getReflection($method)
{
if (empty($this->reflection)) {
$this->reflection = new ReflectionMethod(
$this,
'runTarget'
);
}

return $this->reflection;
}

public function match(Request $request, &$params = array()){
#Store the request
$this->uri = $request->uri;
$params=array();
if(preg_match("#^".$this->pattern."#", $request->uri)){
$this->uri = preg_replace("#^".$this->pattern."#", '', $this->uri);
return true;
}
return false;
}

public function runTarget($method, &$params)
{
if (!$this->instance instanceof Router) {
throw new InvalidArgumentException('Route target must be an instance of Respect\Rest\Router');
}

return (string)$this->instance->run(new Request($method, $this->uri));
}
}