From 700eaa11d0900a69f82d7ede107f8dcf075af2dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Zrn=C3=ADk?= Date: Sun, 14 Apr 2024 12:21:44 +0200 Subject: [PATCH] middleware with context --- src/Content/MiddlewareWithContextInterface.php | 2 ++ src/ZweistOpenApiGenerator.php | 4 ++-- src/ZweistRouteService.php | 2 +- .../Controllers/HelloWorldController.php | 9 +++++---- tests/ExampleApplication/ExampleMiddleware.php | 4 +++- tests/MiddlewareAttributeTest.php | 1 - 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Content/MiddlewareWithContextInterface.php b/src/Content/MiddlewareWithContextInterface.php index db30164..2c65bb6 100644 --- a/src/Content/MiddlewareWithContextInterface.php +++ b/src/Content/MiddlewareWithContextInterface.php @@ -1,5 +1,7 @@ _context->namespace, $annotation->_context->class); - $method = (string)$annotation->_context->method; + $method = (string) $annotation->_context->method; $middlewareData = []; diff --git a/src/ZweistRouteService.php b/src/ZweistRouteService.php index 08fd76d..c55e04e 100644 --- a/src/ZweistRouteService.php +++ b/src/ZweistRouteService.php @@ -27,7 +27,7 @@ class ZweistRouteService { public function __construct( private readonly ZweistConfiguration $zweistConfiguration, - private readonly ContainerInterface $container, + private readonly ContainerInterface $container, ) { } diff --git a/tests/ExampleApplication/Controllers/HelloWorldController.php b/tests/ExampleApplication/Controllers/HelloWorldController.php index bbc2080..dbf20a1 100644 --- a/tests/ExampleApplication/Controllers/HelloWorldController.php +++ b/tests/ExampleApplication/Controllers/HelloWorldController.php @@ -15,7 +15,6 @@ use Zrnik\Zweist\Content\JsonContentFacade; use Zrnik\Zweist\Tests\ExampleApplication\ExampleClassMiddleware; use Zrnik\Zweist\Tests\ExampleApplication\ExampleMiddleware; -use Zrnik\Zweist\Tests\ExampleApplication\ExampleMiddlewareWithContext; #[Middleware(ExampleClassMiddleware::class)] class HelloWorldController @@ -46,12 +45,14 @@ public function __construct() content: new JsonContent(ref: TestResponse::class) ), Middleware(ExampleMiddleware::class), - Middleware(ExampleMiddleware::class, ['context' => 'value']), + Middleware(ExampleMiddleware::class, [ + 'context' => 'value', + ]), ] public function sayHello( - RequestInterface $request, + RequestInterface $request, ResponseInterface $response, - array $arguments = [] + array $arguments = [] ): ResponseInterface { return $this->jsonContentFacade->updateResponse( diff --git a/tests/ExampleApplication/ExampleMiddleware.php b/tests/ExampleApplication/ExampleMiddleware.php index 569cacc..d92e3a8 100644 --- a/tests/ExampleApplication/ExampleMiddleware.php +++ b/tests/ExampleApplication/ExampleMiddleware.php @@ -13,7 +13,9 @@ class ExampleMiddleware implements MiddlewareInterface, MiddlewareWithContextInterface { public const CONTEXT_HEADER_NAME = 'X-Test-Middleware-Context'; + public const VALUE_HEADER_NAME = 'X-Test-Middleware-Called'; + public const VALUE_HEADER_VALUE = 'true'; private mixed $context; @@ -24,7 +26,7 @@ public function setContext(mixed $context): void } public function process( - ServerRequestInterface $request, + ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { diff --git a/tests/MiddlewareAttributeTest.php b/tests/MiddlewareAttributeTest.php index 9b1bf8d..169da06 100644 --- a/tests/MiddlewareAttributeTest.php +++ b/tests/MiddlewareAttributeTest.php @@ -14,7 +14,6 @@ use Psr\Log\LoggerInterface; use Slim\App; use Zrnik\Zweist\Tests\ExampleApplication\ExampleMiddleware; -use Zrnik\Zweist\Tests\ExampleApplication\ExampleMiddlewareWithContext; use Zrnik\Zweist\ZweistConfiguration; use Zrnik\Zweist\ZweistOpenApiGenerator; use Zrnik\Zweist\ZweistRouteService;