Skip to content

Commit

Permalink
Merge pull request #3 from shadowhand/feature/resolver-injection
Browse files Browse the repository at this point in the history
Add support for injecting a callable resolver
  • Loading branch information
oscarotero committed Apr 13, 2017
2 parents 4433959 + 51d4b72 commit c5f4265
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ If you want to change this behaviour, use a container implementing the [PSR-11 s

The dispatcher instance to use.

#### `resolver(Psr\Container\ContainerInterface $resolver)`
#### `resolver(Middlewares\Utils\CallableResolver\CallableResolverInterface $resolver)`

The resolver implementing [CallableResolverInterface]() to resolve the route handlers.

#### `container(Psr\Container\ContainerInterface $container)`

To use a container implementing [PSR-11 interface](https://github.com/php-fig/container) to resolve the route handlers.

Expand Down
16 changes: 15 additions & 1 deletion src/FastRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,25 @@ public function __construct(Dispatcher $router)
/**
* Set the resolver used to create the controllers.
*
* @param CallableResolverInterface $resolver
*
* @return self
*/
public function resolver(CallableResolverInterface $resolver)
{
$this->resolver = $resolver;

return $this;
}

/**
* Set the container used to create the controllers.
*
* @param ContainerInterface $container
*
* @return self
*/
public function resolver(ContainerInterface $container)
public function container(ContainerInterface $container)
{
$this->resolver = new ContainerResolver($container);

Expand Down
39 changes: 35 additions & 4 deletions tests/FastRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Psr\Container\ContainerInterface;
use Middlewares\FastRoute;
use Middlewares\Utils\CallableResolver\CallableResolverInterface;
use Middlewares\Utils\Dispatcher;
use Middlewares\Utils\Factory;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;

Expand Down Expand Up @@ -77,17 +79,17 @@ public function testFastRouteNotAllowed()
$this->assertEquals(405, $response->getStatusCode());
}

public function testFastRouteContainerResolve()
public function testFastRouteResolver()
{
$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
$r->addRoute('POST', '/user/{name}/{id:[0-9]+}', 'controller');
});

$request = Factory::createServerRequest([], 'POST', 'http://domain.com/user/oscarotero/35');

/** @var ContainerInterface|ObjectProphecy $resolver */
$resolver = $this->prophesize(ContainerInterface::class);
$resolver->get('controller')->willReturn(function ($request) {
/** @var CallableResolverInterface|ObjectProphecy $resolver */
$resolver = $this->prophesize(CallableResolverInterface::class);
$resolver->resolve('controller', Argument::cetera())->willReturn(function ($request) {
return sprintf(
'Hello %s (%s)',
$request->getAttribute('name'),
Expand All @@ -105,4 +107,33 @@ public function testFastRouteContainerResolve()
$this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response);
$this->assertEquals('Hello oscarotero (35)', (string) $response->getBody());
}

public function testFastRouteContainerResolver()
{
$dispatcher = \FastRoute\simpleDispatcher(function (\FastRoute\RouteCollector $r) {
$r->addRoute('POST', '/user/{name}/{id:[0-9]+}', 'controller');
});

$request = Factory::createServerRequest([], 'POST', 'http://domain.com/user/oscarotero/35');

/** @var ContainerInterface|ObjectProphecy $container */
$container = $this->prophesize(ContainerInterface::class);
$container->get('controller')->willReturn(function ($request) {
return sprintf(
'Hello %s (%s)',
$request->getAttribute('name'),
$request->getAttribute('id')
);
});

$middleware = new FastRoute($dispatcher);
$middleware->container($container->reveal());

$response = Dispatcher::run([
$middleware,
], $request);

$this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response);
$this->assertEquals('Hello oscarotero (35)', (string) $response->getBody());
}
}

0 comments on commit c5f4265

Please sign in to comment.