From 709684b0d659abafcab08e31ef0bc061880dd3a6 Mon Sep 17 00:00:00 2001 From: Solvent Date: Wed, 13 Oct 2021 03:14:47 +0500 Subject: [PATCH] Solve https://github.com/PHP-DI/Slim-Bridge/issues/51 --- composer.json | 3 +- src/CallableResolver.php | 4 + tests/CallableResolverTest.php | 18 +++ tests/Mock/Psr_15/Middleware.php | 20 ++++ tests/Mock/Psr_15/Request.php | 161 +++++++++++++++++++++++++++ tests/Mock/Psr_15/RequestHandler.php | 16 +++ tests/Mock/Psr_15/Response.php | 80 +++++++++++++ 7 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 tests/Mock/Psr_15/Middleware.php create mode 100644 tests/Mock/Psr_15/Request.php create mode 100644 tests/Mock/Psr_15/RequestHandler.php create mode 100644 tests/Mock/Psr_15/Response.php diff --git a/composer.json b/composer.json index e78e406..dd9ff3a 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ }, "require": { "php": ">=7.3", - "psr/container": "^1.0|^2.0" + "psr/container": "^1.0|^2.0", + "psr/http-server-middleware": "^1.0" }, "require-dev": { "phpunit/phpunit": "^9.0", diff --git a/src/CallableResolver.php b/src/CallableResolver.php index e3b11b2..a1348c9 100644 --- a/src/CallableResolver.php +++ b/src/CallableResolver.php @@ -6,6 +6,7 @@ use Invoker\Exception\NotCallableException; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\Http\Server\MiddlewareInterface; use ReflectionException; use ReflectionMethod; @@ -38,6 +39,9 @@ public function resolve($callable): callable $callable = $this->resolveFromContainer($callable); if (! is_callable($callable)) { + if ($callable instanceof MiddlewareInterface) { + return fn ($request, $next) => $callable->process($request, $next); + } throw NotCallableException::fromInvalidCallable($callable, true); } diff --git a/tests/CallableResolverTest.php b/tests/CallableResolverTest.php index 26796f6..e7a3f63 100644 --- a/tests/CallableResolverTest.php +++ b/tests/CallableResolverTest.php @@ -6,7 +6,11 @@ use Invoker\Exception\NotCallableException; use Invoker\Test\Mock\ArrayContainer; use Invoker\Test\Mock\CallableSpy; +use Invoker\Test\Mock\Psr_15\Middleware; +use Invoker\Test\Mock\Psr_15\Request; +use Invoker\Test\Mock\Psr_15\RequestHandler; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\ResponseInterface; use stdClass; class CallableResolverTest extends TestCase @@ -123,6 +127,20 @@ public function resolves_string_method_call_with_service() $this->assertTrue($fixture->wasCalled); } + /** + * @test + */ + public function resolves_psr_15_middleware() + { + $this->container->set(Middleware::class, new Middleware()); + + $result = $this->resolver->resolve(Middleware::class); + + $result = $result(new Request(), new RequestHandler()); + + $this->assertInstanceOf(ResponseInterface::class, $result); + } + /** * @test */ diff --git a/tests/Mock/Psr_15/Middleware.php b/tests/Mock/Psr_15/Middleware.php new file mode 100644 index 0000000..77b7abc --- /dev/null +++ b/tests/Mock/Psr_15/Middleware.php @@ -0,0 +1,20 @@ +handle($request); + } +} \ No newline at end of file diff --git a/tests/Mock/Psr_15/Request.php b/tests/Mock/Psr_15/Request.php new file mode 100644 index 0000000..0dd3797 --- /dev/null +++ b/tests/Mock/Psr_15/Request.php @@ -0,0 +1,161 @@ +