Skip to content
Merged
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
18 changes: 18 additions & 0 deletions Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,13 @@ class Route extends Routable implements RouteInterface
*/
protected $arguments = [];

/**
* The callable payload
*
* @var callable
*/
protected $callable;

/**
* Create new route
*
Expand Down Expand Up @@ -120,6 +128,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
*
Expand Down
26 changes: 26 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}