Skip to content

Commit

Permalink
finalised the middelware concept
Browse files Browse the repository at this point in the history
  • Loading branch information
reinvanoyen committed May 4, 2020
1 parent 423a2e4 commit 1564e35
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
16 changes: 16 additions & 0 deletions src/Http/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 7 additions & 12 deletions src/Http/Middleware/CoreRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

class CoreRequestHandler implements RequestHandlerInterface
{
/**
* @var ResponseFactoryInterface $responseFactory
*/
private $responseFactory;

/**
* @var BaseController $controller
*/
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions src/Http/Middleware/MiddlewareRegisterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Http/ResponseEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class ResponseEmitter implements ResponseEmitterInterface
*/
public function emit(ResponseInterface $response)
{
$response->getBody()->rewind();

if (! headers_sent()) {

// Status
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 3 additions & 11 deletions src/Session/SessionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1564e35

Please sign in to comment.