Skip to content

Commit

Permalink
Merge 69a823a into 7c33189
Browse files Browse the repository at this point in the history
  • Loading branch information
geggleto committed Oct 4, 2016
2 parents 7c33189 + 69a823a commit d2c5df9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Slim/DefaultServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down
23 changes: 22 additions & 1 deletion Slim/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Slim;

use FastRoute\Dispatcher;
use Interop\Container\ContainerInterface;
use InvalidArgumentException;
use RuntimeException;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -29,6 +30,13 @@
*/
class Router implements RouterInterface
{
/**
* Container Interface
*
* @var ContainerInterface
*/
protected $container;

/**
* Parser
*
Expand Down Expand Up @@ -126,6 +134,14 @@ public function setCacheFile($cacheFile)
return $this;
}

/**
* @param ContainerInterface $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}

/**
* Add route
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Slim\Tests;

use Interop\Container\ContainerInterface;
use Slim\App;
use Slim\Container;
use Slim\Exception\MethodNotAllowedException;
Expand All @@ -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
Expand Down Expand Up @@ -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());
}
}
1 change: 1 addition & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace Slim\Tests;

use Slim\Container;
use Slim\Router;

class RouterTest extends \PHPUnit_Framework_TestCase
Expand Down

0 comments on commit d2c5df9

Please sign in to comment.