Skip to content

Commit

Permalink
Merge pull request #2 from sagikazarmark/resolvable_arrays
Browse files Browse the repository at this point in the history
Add support for resolvable array callables
  • Loading branch information
oscarotero authored Feb 5, 2017
2 parents 33fa7c2 + da909bf commit dba9e97
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## UNRELEASED

## Changed

* Updated to `middlewares/utils#~0.9`
* Improved route target resolution

## 0.3.0 - 2016-12-26

### Changed
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
"php": "^5.6 || ^7.0",
"http-interop/http-middleware": "^0.4",
"nikic/fast-route": "^1.0",
"middlewares/utils": "~0.8"
"middlewares/utils": "~0.9"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
"zendframework/zend-diactoros": "^1.3",
"friendsofphp/php-cs-fixer": "^2.0",
"squizlabs/php_codesniffer": "^2.7"
"squizlabs/php_codesniffer": "^2.7",
"container-interop/container-interop": "^1.1"
},
"suggest": {
"container-interop/container-interop": "Can be used to automatically resolve route actions"
},
"autoload": {
"psr-4": {
Expand Down
26 changes: 17 additions & 9 deletions src/FastRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Middlewares;

use Middlewares\Utils\CallableResolver\CallableResolverInterface;
use Middlewares\Utils\CallableResolver\ContainerResolver;
use Middlewares\Utils\CallableResolver\ReflectionResolver;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Interop\Container\ContainerInterface;
Expand All @@ -22,7 +25,7 @@ class FastRoute implements MiddlewareInterface
private $arguments = [];

/**
* @var ContainerInterface Used to resolve the controllers
* @var CallableResolverInterface Used to resolve the controllers
*/
private $resolver;

Expand All @@ -39,13 +42,13 @@ public function __construct(Dispatcher $router)
/**
* Set the resolver used to create the controllers.
*
* @param ContainerInterface $resolver
* @param ContainerInterface $container
*
* @return self
*/
public function resolver(ContainerInterface $resolver)
public function resolver(ContainerInterface $container)
{
$this->resolver = $resolver;
$this->resolver = new ContainerResolver($container);

return $this;
}
Expand Down Expand Up @@ -88,12 +91,17 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele

$arguments = array_merge([$request], $this->arguments);

if ($this->resolver) {
$callable = $this->resolver->get($route[1]);
} else {
$callable = Utils\CallableHandler::resolve($route[1], $arguments);
}
$callable = $this->getResolver()->resolve($route[1], $arguments);

return Utils\CallableHandler::execute($callable, $arguments);
}

private function getResolver()
{
if (!isset($this->resolver)) {
$this->resolver = new ReflectionResolver();
}

return $this->resolver;
}
}
32 changes: 32 additions & 0 deletions tests/FastRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Middlewares\Tests;

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

class FastRouteTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -73,4 +76,33 @@ public function testFastRouteNotAllowed()
$this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response);
$this->assertEquals(405, $response->getStatusCode());
}

public function testFastRouteContainerResolve()
{
$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) {
return sprintf(
'Hello %s (%s)',
$request->getAttribute('name'),
$request->getAttribute('id')
);
});

$middleware = new FastRoute($dispatcher);
$middleware->resolver($resolver->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 dba9e97

Please sign in to comment.