Skip to content

Commit

Permalink
added support for closures, release v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 24, 2018
1 parent 85ce6af commit 0519f8e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,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]
## [1.0.0] - 2018-01-24

### Added

- Improved testing and added code coverage reporting
- Added tests for PHP 7.2
- Added support for `Closure` handlers

### Changed

Expand Down Expand Up @@ -69,7 +70,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

First version

[UNRELEASED]: https://github.com/middlewares/request-handler/compare/v0.5.0...HEAD
[1.0.0]: https://github.com/middlewares/request-handler/compare/v0.5.0...v1.0.0
[0.5.0]: https://github.com/middlewares/request-handler/compare/v0.4.0...v0.5.0
[0.4.0]: https://github.com/middlewares/request-handler/compare/v0.3.0...v0.4.0
[0.3.0]: https://github.com/middlewares/request-handler/compare/v0.2.0...v0.3.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ $dispatcher = new Dispatcher([
$response = $dispatcher->dispatch(new ServerRequest('/hello/world'));
```

When the request handler is invoked, it expects a request attribute to be defined that contains a reference to the handler. The handler must be a string or an object implementing `MiddlewareInterface` or `RequestHandlerInterface`. If it's a string, a `ContainerInterface` will be used to resolve it and get the `MiddlewareInterface` or `RequestHandlerInterface` to use.
When the request handler is invoked, it expects a request attribute to be defined that contains a reference to the handler. The handler must be a string, a `Closure` or an object implementing `MiddlewareInterface` or `RequestHandlerInterface`. If it's a string, a `ContainerInterface` will be used to resolve it and get the `MiddlewareInterface` or `RequestHandlerInterface` to use. If it's a `Closure`, will be converted automatically to `MiddlewareInterface` using the [middlewares/utils CallableHandler](https://github.com/middlewares/utils#callablehandler)

```php
// Use a PSR-11 container to create the intances of the request handlers
Expand Down
6 changes: 6 additions & 0 deletions src/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Middlewares;

use Closure;
use Middlewares\Utils\CallableHandler;
use Middlewares\Utils\RequestHandlerContainer;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -61,6 +63,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $requestHandler->handle($request);
}

if ($requestHandler instanceof Closure) {
return (new CallableHandler($requestHandler))->process($request, $handler);
}

throw new RuntimeException(sprintf('Invalid request handler: %s', gettype($requestHandler)));
}
}
16 changes: 16 additions & 0 deletions tests/RequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,20 @@ public function testRequestHandler()
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('Bar', $response->getHeaderLine('X-Foo'));
}

public function testClosure()
{
$response = Dispatcher::run(
[
new RequestHandler(),
],
$request = Factory::createServerRequest()
->withAttribute('request-handler', function () {
return Factory::createResponse()->withHeader('X-Foo', 'Bar');
})
);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('Bar', $response->getHeaderLine('X-Foo'));
}
}

0 comments on commit 0519f8e

Please sign in to comment.