Skip to content

Commit

Permalink
added support for arrays with strings
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 25, 2018
1 parent b8b0f1e commit a303a4f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/RequestHandler.php
Expand Up @@ -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);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Controller.php
@@ -0,0 +1,24 @@
<?php
declare(strict_types = 1);

namespace Middlewares\Tests;

use Middlewares\Utils\Factory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class Controller implements RequestHandlerInterface
{
public function run(): ResponseInterface
{
$response = Factory::createResponse();
$response->getBody()->write('Ok');
return $response;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
return $this->run();
}
}
23 changes: 19 additions & 4 deletions tests/RequestHandlerTest.php
Expand Up @@ -4,6 +4,7 @@
namespace Middlewares\Tests;

use Datetime;
use Exception;
use Middlewares\RequestHandler;
use Middlewares\Utils\Dispatcher;
use Middlewares\Utils\Factory;
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -118,7 +133,7 @@ public function testContinueOnEmptyClosure()
(new RequestHandler())->continueOnEmpty(),
function () {
return 'Fallback';
}
},
]
);

Expand All @@ -142,7 +157,7 @@ function ($request, $next) {

function () {
return 'Fallback';
}
},
]
);

Expand All @@ -165,7 +180,7 @@ function ($request, $next) {

function () {
return 'Fallback';
}
},
],
$request = Factory::createServerRequest('GET', '/')
->withAttribute('request-handler', ['--invalid--'])
Expand Down

0 comments on commit a303a4f

Please sign in to comment.