Skip to content

Commit

Permalink
Bug-fix for router
Browse files Browse the repository at this point in the history
  • Loading branch information
geggleto committed Oct 4, 2016
1 parent 7c33189 commit a38edd9
Show file tree
Hide file tree
Showing 3 changed files with 77 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
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());
}
}

0 comments on commit a38edd9

Please sign in to comment.