Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ncou committed Jun 13, 2021
1 parent cf19443 commit 34938bc
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 41 deletions.
45 changes: 45 additions & 0 deletions src/Event/AfterHandlerEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Chiron\Pipeline\Event;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* AfterHandlerEvent is raised after the handler was executed.
*/
final class AfterHandlerEvent
{
/** @var RequestHandlerInterface */
private $handler;
/** @var ResponseInterface|null */
private $response;

/**
* @param RequestHandlerInterface $handler Handler that was executed.
* @param ResponseInterface|null $response Response generated by the handler or null in case there was an error.
*/
public function __construct(RequestHandlerInterface $handler, ?ResponseInterface $response)
{
$this->handler = $handler;
$this->response = $response;
}

/**
* @return RequestHandlerInterface
*/
public function getHandler(): RequestHandlerInterface
{
return $this->handler;
}

/**
* @return ResponseInterface|null
*/
public function getResponse(): ?ResponseInterface
{
return $this->response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
use Psr\Http\Server\MiddlewareInterface;

/**
* AfterMiddleware event is raised after a middleware was executed.
* AfterMiddlewareEvent is raised after the middleware was executed.
*/
final class AfterMiddleware
final class AfterMiddlewareEvent
{
/** @var MiddlewareInterface */
private $middleware;
/** @var ?ResponseInterface */
/** @var ResponseInterface|null */
private $response;

/**
* @param MiddlewareInterface $middleware Middleware that was executed.
* @param ResponseInterface|null $response Response generated by middleware or null in case there was an error.
* @param ResponseInterface|null $response Response generated by the middleware or null in case there was an error.
*/
public function __construct(MiddlewareInterface $middleware, ?ResponseInterface $response)
{
Expand All @@ -28,15 +28,15 @@ public function __construct(MiddlewareInterface $middleware, ?ResponseInterface
}

/**
* @return MiddlewareInterface Middleware that was executed.
* @return MiddlewareInterface
*/
public function getMiddleware(): MiddlewareInterface
{
return $this->middleware;
}

/**
* @return ResponseInterface|null Response generated by middleware or null in case there was an error.
* @return ResponseInterface|null
*/
public function getResponse(): ?ResponseInterface
{
Expand Down
45 changes: 45 additions & 0 deletions src/Event/BeforeHandlerEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Chiron\Pipeline\Event;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* BeforeHandlerEvent is raised before executing the fallback handler.
*/
final class BeforeHandlerEvent
{
/** @var RequestHandlerInterface */
private $handler;
/** @var ServerRequestInterface */
private $request;

/**
* @param RequestHandlerInterface $handler
* @param ServerRequestInterface $request
*/
public function __construct(RequestHandlerInterface $handler, ServerRequestInterface $request)
{
$this->handler = $handler;
$this->request = $request;
}

/**
* @return RequestHandlerInterface
*/
public function getHandler(): RequestHandlerInterface
{
return $this->handler;
}

/**
* @return ServerRequestInterface
*/
public function getRequest(): ServerRequestInterface
{
return $this->request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
use Psr\Http\Server\MiddlewareInterface;

/**
* AfterMiddleware event is raised before executing a middleware.
* BeforeMiddlewareEvent is raised before executing a middleware.
*/
final class BeforeMiddleware
final class BeforeMiddlewareEvent
{
/** @var MiddlewareInterface */
private $middleware;
/** @var ServerRequestInterface */
private $request;

/**
* @param MiddlewareInterface $middleware Middleware to be executed.
* @param ServerRequestInterface $request Request to be passed to the middleware.
* @param MiddlewareInterface $middleware
* @param ServerRequestInterface $request
*/
public function __construct(MiddlewareInterface $middleware, ServerRequestInterface $request)
{
Expand All @@ -28,15 +28,15 @@ public function __construct(MiddlewareInterface $middleware, ServerRequestInterf
}

/**
* @return MiddlewareInterface Middleware to be executed.
* @return MiddlewareInterface
*/
public function getMiddleware(): MiddlewareInterface
{
return $this->middleware;
}

/**
* @return ServerRequestInterface Request to be passed to the middleware.
* @return ServerRequestInterface
*/
public function getRequest(): ServerRequestInterface
{
Expand Down
47 changes: 18 additions & 29 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Chiron\Pipeline\Event\BeforeMiddleware;
use Chiron\Pipeline\Event\AfterMiddleware;
use Chiron\Pipeline\Event\BeforeMiddlewareEvent;
use Chiron\Pipeline\Event\AfterMiddlewareEvent;
use Chiron\Pipeline\Event\BeforeHandlerEvent;
use Chiron\Pipeline\Event\AfterHandlerEvent;

// TODO : exemple avec les événements !!!!
//https://github.com/yiisoft/middleware-dispatcher/blob/master/src/MiddlewareStack.php#L98

//https://github.com/middlewares/utils/blob/master/src/Dispatcher.php#L43

// TODO : vérifier qu'on a pas de doublons de middlewares : https://github.com/illuminate/routing/blob/f0908784ce618438be1a8b99f4613f62d18d8013/Router.php#L1256

/**
Expand All @@ -34,6 +38,7 @@
* @see https://www.php-fig.org/psr/psr-15/meta/
*/
// TODO : actualiser la phpDoc et ajouter un @see sur les specs psr14 pour les events !!!!
// TODO : ajouter des tests pour les events
final class Pipeline implements RequestHandlerInterface
{
/** @var EventDispatcherInterface */
Expand Down Expand Up @@ -89,51 +94,35 @@ public function fallback(RequestHandlerInterface $handler): self
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
// TODO : remplacer cette appel au binding par un événement "BeforeMiddleware" : exemple : https://github.com/yiisoft/middleware-dispatcher/blob/master/src/MiddlewareStack.php#L85
// Attach a fresh instance of the request in the container.
//$this->bindRequestInstance($request); // TODO : transformer ce bout de code en une propriété public de classe "$onHandle" de type callable qu'on executerait avant chaque Handle. Cela permettrait au package pipeline de ne plus avoir de dépendance avec le package container.
// TODO : ajouter des tests pour ce package notamment sur les événements et le comportement en cas de throw d'exception s'assurer que le finaly fonctionne correctement !!!

// TODO : code temporaire le temps d'ajouter un event dispatcher !!!!
/*
if ($this->beforeMiddleware !== null) {
call_user_func_array($this->beforeMiddleware, [$request]);
}*/

$middleware = $this->middlewares[$this->position] ?? null;

//if (isset($this->middlewares[$this->position])) {
if ($middleware !== null) {
$this->dispatcher->dispatch(new BeforeMiddleware($middleware, $request));

$this->dispatcher->dispatch(new BeforeMiddlewareEvent($middleware, $request));
try {
return $response = $middleware->process($request, $this->nextHandler());
} finally {
$this->dispatcher->dispatch(new AfterMiddleware($middleware, $response ?? null));
$this->dispatcher->dispatch(new AfterMiddlewareEvent($middleware, $response ?? null));
}
}

return $this->fallback->handle($request);
// TODO : eventuellement permettre de laisser le fallback à null et dans ce cas lever l'exception qui est présente dans la classe EmptyPipelineHandler directement à la fin de cette méthode. Ca évitera de conserver la classe EmptyPipelineHandler qui sera inutile !!!!
$this->dispatcher->dispatch(new BeforeHandlerEvent($this->fallback, $request));
try {
return $response = $this->fallback->handle($request);;
} finally {
$this->dispatcher->dispatch(new AfterHandlerEvent($this->fallback, $response ?? null));
}
}

/**
* Store a "fresh" Request instance in the container.
* Usefull if you need to retrieve some request attributes.
*
* Ex: in the CookieCollectionMiddleware with the CookieCollection::ATTRIBUTE
*
* @param ServerRequestInterface $request
*/
/*
private function bindRequestInstance(ServerRequestInterface $request): void
{
$this->container->bind(ServerRequestInterface::class, $request);
}*/

/**
* Get a handler pointing to the next middleware position.
*
* @return RequestHandlerInterface New Pipeline instance used as handler.
*/
// TODO : vérifier si le clone de cette classe ne posera pas de probléme lors du clone de la propriété $this->dispatcher qui est un objet !!!
private function nextHandler(): RequestHandlerInterface
{
$copy = clone $this;
Expand Down

0 comments on commit 34938bc

Please sign in to comment.