Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Bug-fix for router
  • Loading branch information
geggleto committed Oct 4, 2016
commit a38edd9c46c7a9a6c8b16a334f7ceef6a42293b9
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
33 changes: 32 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,24 @@ public function setCacheFile($cacheFile)
return $this;
}

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

/**
* @return ContainerInterface
*/
public function getContainer()
{
return $this->container;
}



/**
* Add route
*
Expand Down Expand Up @@ -189,7 +215,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
38 changes: 38 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,40 @@ 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());
}
}