From a303a4f7e9527994eca9270ed0d54689407853f2 Mon Sep 17 00:00:00 2001 From: Oscar Otero Date: Fri, 26 Oct 2018 00:51:29 +0200 Subject: [PATCH] added support for arrays with strings --- CHANGELOG.md | 8 ++++++-- src/RequestHandler.php | 4 ++++ tests/Controller.php | 24 ++++++++++++++++++++++++ tests/RequestHandlerTest.php | 23 +++++++++++++++++++---- 4 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 tests/Controller.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e093f7f..caf45dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ 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.4.0] - 2018-10-26 + +### Added + +- Support for arrays with 2 strings as request handler ### Fixed @@ -100,7 +104,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). First version -[Unreleased]: https://github.com/middlewares/request-handler/compare/v1.3.0...HEAD +[1.4.0]: https://github.com/middlewares/request-handler/compare/v1.3.0...v1.4.0 [1.3.0]: https://github.com/middlewares/request-handler/compare/v1.2.0...v1.3.0 [1.2.0]: https://github.com/middlewares/request-handler/compare/v1.1.0...v1.2.0 [1.1.0]: https://github.com/middlewares/request-handler/compare/v1.0.1...v1.1.0 diff --git a/src/RequestHandler.php b/src/RequestHandler.php index 7172901..9f5a658 100644 --- a/src/RequestHandler.php +++ b/src/RequestHandler.php @@ -76,6 +76,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler = $this->container->get($requestHandler); } + if (is_array($requestHandler) && count($requestHandler) === 2 && is_string($requestHandler[0])) { + $requestHandler[0] = $this->container->get($requestHandler[0]); + } + if ($requestHandler instanceof MiddlewareInterface) { return $requestHandler->process($request, $handler); } diff --git a/tests/Controller.php b/tests/Controller.php new file mode 100644 index 0000000..463e9f0 --- /dev/null +++ b/tests/Controller.php @@ -0,0 +1,24 @@ +getBody()->write('Ok'); + return $response; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + return $this->run(); + } +} diff --git a/tests/RequestHandlerTest.php b/tests/RequestHandlerTest.php index 7b87780..dd8526d 100644 --- a/tests/RequestHandlerTest.php +++ b/tests/RequestHandlerTest.php @@ -4,6 +4,7 @@ namespace Middlewares\Tests; use Datetime; +use Exception; use Middlewares\RequestHandler; use Middlewares\Utils\Dispatcher; use Middlewares\Utils\Factory; @@ -14,7 +15,6 @@ use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Exception; use RuntimeException; class RequestHandlerTest extends TestCase @@ -79,6 +79,21 @@ public function testCustomContainer() $this->assertSame(200, $response->getStatusCode()); } + public function testArrayHandler() + { + $request = Factory::createServerRequest('GET', '/'); + $request = $request->withAttribute('request-handler', ['Middlewares\\Tests\\Controller', 'run']); + + $response = Dispatcher::run( + [ + new RequestHandler(), + ], + $request + ); + + $this->assertSame('Ok', (string) $response->getBody()); + } + public function testRequestHandler() { $response = Dispatcher::run( @@ -118,7 +133,7 @@ public function testContinueOnEmptyClosure() (new RequestHandler())->continueOnEmpty(), function () { return 'Fallback'; - } + }, ] ); @@ -142,7 +157,7 @@ function ($request, $next) { function () { return 'Fallback'; - } + }, ] ); @@ -165,7 +180,7 @@ function ($request, $next) { function () { return 'Fallback'; - } + }, ], $request = Factory::createServerRequest('GET', '/') ->withAttribute('request-handler', ['--invalid--'])