From b4f18109c7352216ac633a644e10b1cede81a5e0 Mon Sep 17 00:00:00 2001 From: Glenn Eggleton Date: Tue, 16 Aug 2016 14:57:51 -0400 Subject: [PATCH 1/3] Related to #1962. Implementation with UnitTest --- Slim/Route.php | 11 +++++++++++ tests/RouteTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/Slim/Route.php b/Slim/Route.php index 457244163..450608f05 100644 --- a/Slim/Route.php +++ b/Slim/Route.php @@ -12,6 +12,7 @@ use InvalidArgumentException; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; +use Slim\Exception\SlimException; use Slim\Handlers\Strategies\RequestResponse; use Slim\Interfaces\InvocationStrategyInterface; use Slim\Interfaces\RouteInterface; @@ -354,4 +355,14 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res return $response; } + + /** + * This method enables you to override the Route's callable + * + * @param string|\Closure $callable + */ + public function setCallable($callable) + { + $this->callable = $callable; + } } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 77b3f7d0e..ed0e9352e 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -427,4 +427,30 @@ public function testPatternCanBeChanged() $route->setPattern('/hola/{nombre}'); $this->assertEquals('/hola/{nombre}', $route->getPattern()); } + + /** + * Ensure that the callable can be changed + */ + public function testChangingCallable() + { + $container = new 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->setCallable('CallableTest2:toCall'); //Then we fix it here. + + $uri = Uri::createFromString('https://example.com:80'); + $body = new Body(fopen('php://temp', 'r+')); + $request = new Request('GET', $uri, new Headers(), [], Environment::mock()->all(), $body); + + $result = $route->callMiddlewareStack($request, new Response); + + $this->assertInstanceOf('Slim\Http\Response', $result); + $this->assertEquals([$container['CallableTest2'], 'toCall'], InvocationStrategyTest::$LastCalledFor); + } } From 103269e49e20527ea4fc99bbb13c54b1692f476a Mon Sep 17 00:00:00 2001 From: Glenn Eggleton Date: Wed, 17 Aug 2016 08:08:08 -0400 Subject: [PATCH 2/3] Moved setter to below getter --- Slim/Route.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Slim/Route.php b/Slim/Route.php index 450608f05..ecdc6fb96 100644 --- a/Slim/Route.php +++ b/Slim/Route.php @@ -121,6 +121,16 @@ public function getCallable() return $this->callable; } + /** + * This method enables you to override the Route's callable + * + * @param string|\Closure $callable + */ + public function setCallable($callable) + { + $this->callable = $callable; + } + /** * Get route methods * @@ -355,14 +365,4 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res return $response; } - - /** - * This method enables you to override the Route's callable - * - * @param string|\Closure $callable - */ - public function setCallable($callable) - { - $this->callable = $callable; - } } From 0b4395dcaaa98a1c843feba9939ca731d84268ba Mon Sep 17 00:00:00 2001 From: Glenn Eggleton Date: Wed, 17 Aug 2016 08:10:12 -0400 Subject: [PATCH 3/3] Adding protected property for callable --- Slim/Route.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Slim/Route.php b/Slim/Route.php index ecdc6fb96..da47b9559 100644 --- a/Slim/Route.php +++ b/Slim/Route.php @@ -70,6 +70,13 @@ class Route extends Routable implements RouteInterface */ protected $arguments = []; + /** + * The callable payload + * + * @var callable + */ + protected $callable; + /** * Create new route *