diff --git a/Slim/DefaultServicesProvider.php b/Slim/DefaultServicesProvider.php index 5e1398d10..486d4b281 100644 --- a/Slim/DefaultServicesProvider.php +++ b/Slim/DefaultServicesProvider.php @@ -91,8 +91,14 @@ public function register($container) if (isset($container->get('settings')['routerCacheFile'])) { $routerCacheFile = $container->get('settings')['routerCacheFile']; } + - return (new Router)->setCacheFile($routerCacheFile); + $router = (new Router)->setCacheFile($routerCacheFile); + if (method_exists($router, 'setContainer')) { + $router->setContainer($container); + } + + return $router; }; } diff --git a/Slim/Router.php b/Slim/Router.php index b5566dbf7..41fec40c6 100644 --- a/Slim/Router.php +++ b/Slim/Router.php @@ -9,6 +9,7 @@ namespace Slim; use FastRoute\Dispatcher; +use Interop\Container\ContainerInterface; use InvalidArgumentException; use RuntimeException; use Psr\Http\Message\ServerRequestInterface; @@ -29,6 +30,13 @@ */ class Router implements RouterInterface { + /** + * Container Interface + * + * @var ContainerInterface + */ + protected $container; + /** * Parser * @@ -126,6 +134,14 @@ public function setCacheFile($cacheFile) return $this; } + /** + * @param ContainerInterface $container + */ + public function setContainer(ContainerInterface $container) + { + $this->container = $container; + } + /** * Add route * @@ -189,7 +205,12 @@ public function dispatch(ServerRequestInterface $request) */ protected function createRoute($methods, $pattern, $callable) { - return new Route($methods, $pattern, $callable, $this->routeGroups, $this->routeCounter); + $route = new Route($methods, $pattern, $callable, $this->routeGroups, $this->routeCounter); + if (!empty($this->container)) { + $route->setContainer($this->container); + } + + return $route; } /** diff --git a/tests/AppTest.php b/tests/AppTest.php index 5a7336cc9..d80c5c2de 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -9,6 +9,7 @@ namespace Slim\Tests; +use Interop\Container\ContainerInterface; use Slim\App; use Slim\Container; use Slim\Exception\MethodNotAllowedException; @@ -22,6 +23,7 @@ use Slim\Http\RequestBody; use Slim\Http\Response; use Slim\Http\Uri; +use Slim\Router; use Slim\Tests\Mocks\MockAction; class AppTest extends \PHPUnit_Framework_TestCase @@ -1938,4 +1940,41 @@ public function testForUnexpectedDataInOutputBuffer() $container['settings']['addContentLengthHeader'] = true; $response = $method->invoke($app, $response); } + + + public function testContainerSetToRoute() + { + // Prepare request and response objects + $env = Environment::mock([ + 'SCRIPT_NAME' => '/index.php', + 'REQUEST_URI' => '/foo', + 'REQUEST_METHOD' => 'GET', + ]); + $uri = Uri::createFromEnvironment($env); + $headers = Headers::createFromEnvironment($env); + $cookies = []; + $serverParams = $env->all(); + $body = new RequestBody(); + $req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body); + $res = new Response(); + + $mock = new MockAction(); + + $app = new App(); + $container = $app->getContainer(); + $container['foo'] = function () use ($mock, $res) { + return $mock; + }; + + /** @var $router Router */ + $router = $container['router']; + + $router->map(['get'], '/foo', 'foo:bar'); + + // Invoke app + $resOut = $app($req, $res); + + $this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $resOut); + $this->assertEquals(json_encode(['name'=>'bar', 'arguments' => []]), (string)$res->getBody()); + } } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 3f21e8c81..9f4a0bb87 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -8,6 +8,7 @@ */ namespace Slim\Tests; +use Slim\Container; use Slim\Router; class RouterTest extends \PHPUnit_Framework_TestCase