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

Bug-fix for router #2007

Merged
merged 2 commits into from
Oct 11, 2016
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
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