From 1564e35aec1e29e7db71383e172cee05b8690114 Mon Sep 17 00:00:00 2001 From: Rein Van Oyen Date: Mon, 4 May 2020 13:22:32 +0200 Subject: [PATCH] finalised the middelware concept --- src/Http/Controller/BaseController.php | 16 ++++++++++++++++ src/Http/Middleware/CoreRequestHandler.php | 19 +++++++------------ src/Http/Middleware/Error404Middleware.php | 0 .../Middleware/MiddlewareRegisterTrait.php | 3 +++ src/Http/ResponseEmitter.php | 2 ++ src/Http/Routing/Route.php | 4 +++- src/Session/SessionServiceProvider.php | 14 +++----------- 7 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 src/Http/Middleware/Error404Middleware.php diff --git a/src/Http/Controller/BaseController.php b/src/Http/Controller/BaseController.php index d81c688..9a0b24b 100644 --- a/src/Http/Controller/BaseController.php +++ b/src/Http/Controller/BaseController.php @@ -27,4 +27,20 @@ public function __construct(ServerRequestInterface $request, ResponseInterface $ $this->request = $request; $this->response = $response; } + + /** + * @return ResponseInterface + */ + public function getResponse(): ResponseInterface + { + return $this->response; + } + + /** + * @return ServerRequestInterface + */ + public function getRequest(): ServerRequestInterface + { + return $this->request; + } } \ No newline at end of file diff --git a/src/Http/Middleware/CoreRequestHandler.php b/src/Http/Middleware/CoreRequestHandler.php index bb88d80..35e48b9 100644 --- a/src/Http/Middleware/CoreRequestHandler.php +++ b/src/Http/Middleware/CoreRequestHandler.php @@ -10,11 +10,6 @@ class CoreRequestHandler implements RequestHandlerInterface { - /** - * @var ResponseFactoryInterface $responseFactory - */ - private $responseFactory; - /** * @var BaseController $controller */ @@ -32,14 +27,12 @@ class CoreRequestHandler implements RequestHandlerInterface /** * CoreRequestHandler constructor. - * @param ResponseFactoryInterface $responseFactory * @param $controller * @param string $method * @param array $params */ - public function __construct(ResponseFactoryInterface $responseFactory, BaseController $controller, string $method, array $params = []) + public function __construct(BaseController $controller, string $method, array $params = []) { - $this->responseFactory = $responseFactory; $this->controller = $controller; $this->method = $method; $this->params = $params; @@ -53,16 +46,18 @@ public function handle(ServerRequestInterface $request): ResponseInterface { $output = call_user_func_array([$this->controller, $this->method,], $this->params); + // Check if we already have a response if ($output instanceof ResponseInterface) { + + // It's already a response, return it return $output; } - $response = $this->responseFactory->createResponse(200) - ->withHeader('Content-Type', 'text/html') - ; + // Get the response from the controller + $response = $this->controller->getResponse(); + // ...and write to its body $response->getBody()->write($output); - $response->getBody()->rewind(); return $response; } diff --git a/src/Http/Middleware/Error404Middleware.php b/src/Http/Middleware/Error404Middleware.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Http/Middleware/MiddlewareRegisterTrait.php b/src/Http/Middleware/MiddlewareRegisterTrait.php index b99f1fa..4594024 100644 --- a/src/Http/Middleware/MiddlewareRegisterTrait.php +++ b/src/Http/Middleware/MiddlewareRegisterTrait.php @@ -19,7 +19,10 @@ public function middleware(string $name, array $middlewares = []) { if (! isset($this->middlewares[$name])) { $this->middlewareGroups[$name] = $middlewares; + return; } + + $this->middlewareGroups[$name] = array_merge($this->middlewareGroups[$name], $middlewares); } /** diff --git a/src/Http/ResponseEmitter.php b/src/Http/ResponseEmitter.php index 3d2e03a..1c750c6 100644 --- a/src/Http/ResponseEmitter.php +++ b/src/Http/ResponseEmitter.php @@ -14,6 +14,8 @@ class ResponseEmitter implements ResponseEmitterInterface */ public function emit(ResponseInterface $response) { + $response->getBody()->rewind(); + if (! headers_sent()) { // Status diff --git a/src/Http/Routing/Route.php b/src/Http/Routing/Route.php index 9ffad55..3548fad 100644 --- a/src/Http/Routing/Route.php +++ b/src/Http/Routing/Route.php @@ -98,7 +98,9 @@ public function execute(ContainerInterface $app, ServerRequestInterface $request 'response' => $response, ]); - $middlewareStack = new MiddlewareStack($middleware); + $middlewareStack = $app->getWith(MiddlewareStack::class, [ + 'middleware' => $middleware, + ]); $coreRequestHandler = $app->getWith(CoreRequestHandler::class, [ 'controller' => $controller, diff --git a/src/Session/SessionServiceProvider.php b/src/Session/SessionServiceProvider.php index a6b7e95..68ffa96 100644 --- a/src/Session/SessionServiceProvider.php +++ b/src/Session/SessionServiceProvider.php @@ -13,21 +13,13 @@ class SessionServiceProvider extends ServiceProvider public function register(ContainerInterface $app) { $config = $app->get(RepositoryInterface::class); + $app->singleton(\SessionHandlerInterface::class, $config->get('session.handler', FileSessionHandler::class)); $app->singleton(Session::class, Session::class); $app->singleton(SessionIdentifierInterface::class, SessionIdentifier::class); - $app->whenAsksGive( - FileSessionHandler::class, - 'path', - $config->get('session.path', 'sessions') - ); - - $app->whenAsksGive( - Session::class, - 'name', - $config->get('session.name', 'app') - ); + $app->whenAsksGive(FileSessionHandler::class, 'path', $config->get('session.path', 'sessions')); + $app->whenAsksGive(Session::class, 'name', $config->get('session.name', 'app')); } public function boot(ContainerInterface $app)