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

Refactor callable resolver, router, route, and route group #2098

Merged
merged 13 commits into from Dec 11, 2016

Refactor router, route, route group for new callable resolver

  • Loading branch information...
codeguy committed Dec 10, 2016
commit 3d5fd1f64ebdaf67f9d9302d20fe609384eb6125
@@ -9,6 +9,7 @@
namespace Slim;
use Interop\Container\ContainerInterface;
use Slim\Interfaces\CallableResolverInterface;
/**
* A routable, middleware-aware object
@@ -18,15 +19,18 @@
*/
abstract class Routable
{
use CallableResolverAwareTrait;
/**
* Route callable
*
* @var callable
*/
protected $callable;
/**
* @var \Slim\Interfaces\CallableResolverInterface
*/
protected $callableResolver;
/**
* Container
*
@@ -68,6 +72,26 @@ public function getPattern()
return $this->pattern;
}
/**
* Set callable resolver
*
* @param CallableResolverInterface $resolver
*/
public function setCallableResolver(CallableResolverInterface $resolver)
{
$this->callableResolver = $resolver;
}
/**
* Get callable resolver
*
* @return CallableResolverInterface|null
*/
public function getCallableResolver()
{
return $this->callableResolver;
}
/**
* Set container for use with resolveCallable
*
@@ -90,7 +114,7 @@ public function setContainer(ContainerInterface $container)
*/
public function add($callable)
{
$this->middleware[] = new DeferredCallable($callable, $this->container);
$this->middleware[] = new DeferredCallable($callable, $this->callableResolver);
return $this;
}
@@ -9,6 +9,7 @@
namespace Slim;
use Exception;
use Slim\Interfaces\CallableResolverInterface;
use Throwable;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
@@ -71,13 +72,6 @@ class Route extends Routable implements RouteInterface
*/
protected $arguments = [];
/**
* The callable payload
*
* @var callable
*/
protected $callable;
/**
* Create new route
*
@@ -330,18 +324,27 @@ public function run(ServerRequestInterface $request, ResponseInterface $response
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{
$this->callable = $this->resolveCallable($this->callable);
// Resolve route callable
$callable = $this->callable;
if ($this->callableResolver) {
$callable = $this->callableResolver->resolve($callable);
}
/*if (is_string($callable)) {
var_dump($callable);
var_dump($this->callableResolver);
exit;
}*/
/** @var InvocationStrategyInterface $handler */
$handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();
// invoke route callable
if ($this->outputBuffering === false) {
$newResponse = $handler($this->callable, $request, $response, $this->arguments);
$newResponse = $handler($callable, $request, $response, $this->arguments);
} else {
try {
ob_start();
$newResponse = $handler($this->callable, $request, $response, $this->arguments);
$newResponse = $handler($callable, $request, $response, $this->arguments);
$output = ob_get_clean();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
@@ -8,7 +8,6 @@
*/
namespace Slim;
use Closure;
use Slim\Interfaces\RouteGroupInterface;
/**
@@ -37,8 +36,14 @@ public function __construct($pattern, $callable)
*/
public function __invoke(App $app = null)
{
$callable = $this->resolveCallable($this->callable);
if ($callable instanceof Closure && $app !== null) {
// Resolve route callable
$callable = $this->callable;
if ($this->callableResolver) {
$callable = $this->callableResolver->resolve($callable);
}
// Bind RouteGroup to app instance
if ($callable instanceof \Closure && $app !== null) {
$callable = $callable->bindTo($app);
}
@@ -16,6 +16,7 @@
use FastRoute\RouteCollector;
use FastRoute\RouteParser;
use FastRoute\RouteParser\Std as StdParser;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\RouteGroupInterface;
use Slim\Interfaces\RouterInterface;
use Slim\Interfaces\RouteInterface;
@@ -44,6 +45,13 @@ class Router implements RouterInterface
*/
protected $routeParser;
/**
* Callable resolver
*
* @var \Slim\Interfaces\CallableResolverInterface
*/
protected $callableResolver;
/**
* Base path used in pathFor()
*
@@ -134,6 +142,16 @@ public function setCacheFile($cacheFile)
return $this;
}
/**
* Set callable resolver
*
* @param CallableResolverInterface $resolver
*/
public function setCallableResolver(CallableResolverInterface $resolver)
{
$this->callableResolver = $resolver;
}
/**
* @param ContainerInterface $container
*/
@@ -199,14 +217,17 @@ public function dispatch(ServerRequestInterface $request)
*
* @param string[] $methods Array of HTTP methods
* @param string $pattern The route pattern
* @param callable $handler The route callable
* @param callable $callable The route callable
*
* @return \Slim\Interfaces\RouteInterface
*/
protected function createRoute($methods, $pattern, $callable)
{
$route = new Route($methods, $pattern, $callable, $this->routeGroups, $this->routeCounter);
if (!empty($this->container)) {
if ($this->callableResolver) {
$route->setCallableResolver($this->callableResolver);
}
if ($this->container) {
$route->setContainer($this->container);
}
@@ -8,6 +8,7 @@
*/
namespace Slim\Tests;
use Slim\CallableResolver;
use Slim\Container;
use Slim\DeferredCallable;
use Slim\Http\Body;
@@ -216,12 +217,14 @@ public function testControllerInContainer()
{
$container = new Container();
$resolver = new CallableResolver($container);
$container['CallableTest'] = new CallableTest;
$deferred = new DeferredCallable('CallableTest:toCall', $container);
$deferred = new DeferredCallable('CallableTest:toCall', $resolver);
$route = new Route(['GET'], '/', $deferred);
$route->setContainer($container);
$route->setCallableResolver($resolver);
$uri = Uri::createFromString('https://example.com:80');
$body = new Body(fopen('php://temp', 'r+'));
@@ -400,13 +403,15 @@ public function testInvokeWhenDisablingOutputBuffer()
public function testInvokeDeferredCallable()
{
$container = new Container();
$resolver = new CallableResolver($container);
$container['CallableTest'] = new CallableTest;
$container['foundHandler'] = function () {
return new InvocationStrategyTest();
};
$route = new Route(['GET'], '/', 'CallableTest:toCall');
$route->setContainer($container);
$route->setCallableResolver($resolver);
$uri = Uri::createFromString('https://example.com:80');
$body = new Body(fopen('php://temp', 'r+'));
@@ -434,13 +439,15 @@ public function testPatternCanBeChanged()
public function testChangingCallable()
{
$container = new Container();
$resolver = new CallableResolver($container);
$container['CallableTest2'] = new CallableTest;
$container['foundHandler'] = function () {
return new InvocationStrategyTest();
};
$route = new Route(['GET'], '/', 'CallableTest:toCall'); //Note that this doesn't actually exist
$route->setContainer($container);
$route->setCallableResolver($resolver);
$route->setCallable('CallableTest2:toCall'); //Then we fix it here.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.