Skip to content

Commit 34938bc

Browse files
committed
update
1 parent cf19443 commit 34938bc

5 files changed

+120
-41
lines changed

Diff for: src/Event/AfterHandlerEvent.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chiron\Pipeline\Event;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Server\RequestHandlerInterface;
9+
10+
/**
11+
* AfterHandlerEvent is raised after the handler was executed.
12+
*/
13+
final class AfterHandlerEvent
14+
{
15+
/** @var RequestHandlerInterface */
16+
private $handler;
17+
/** @var ResponseInterface|null */
18+
private $response;
19+
20+
/**
21+
* @param RequestHandlerInterface $handler Handler that was executed.
22+
* @param ResponseInterface|null $response Response generated by the handler or null in case there was an error.
23+
*/
24+
public function __construct(RequestHandlerInterface $handler, ?ResponseInterface $response)
25+
{
26+
$this->handler = $handler;
27+
$this->response = $response;
28+
}
29+
30+
/**
31+
* @return RequestHandlerInterface
32+
*/
33+
public function getHandler(): RequestHandlerInterface
34+
{
35+
return $this->handler;
36+
}
37+
38+
/**
39+
* @return ResponseInterface|null
40+
*/
41+
public function getResponse(): ?ResponseInterface
42+
{
43+
return $this->response;
44+
}
45+
}

Diff for: src/Event/AfterMiddleware.php renamed to src/Event/AfterMiddlewareEvent.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
use Psr\Http\Server\MiddlewareInterface;
99

1010
/**
11-
* AfterMiddleware event is raised after a middleware was executed.
11+
* AfterMiddlewareEvent is raised after the middleware was executed.
1212
*/
13-
final class AfterMiddleware
13+
final class AfterMiddlewareEvent
1414
{
1515
/** @var MiddlewareInterface */
1616
private $middleware;
17-
/** @var ?ResponseInterface */
17+
/** @var ResponseInterface|null */
1818
private $response;
1919

2020
/**
2121
* @param MiddlewareInterface $middleware Middleware that was executed.
22-
* @param ResponseInterface|null $response Response generated by middleware or null in case there was an error.
22+
* @param ResponseInterface|null $response Response generated by the middleware or null in case there was an error.
2323
*/
2424
public function __construct(MiddlewareInterface $middleware, ?ResponseInterface $response)
2525
{
@@ -28,15 +28,15 @@ public function __construct(MiddlewareInterface $middleware, ?ResponseInterface
2828
}
2929

3030
/**
31-
* @return MiddlewareInterface Middleware that was executed.
31+
* @return MiddlewareInterface
3232
*/
3333
public function getMiddleware(): MiddlewareInterface
3434
{
3535
return $this->middleware;
3636
}
3737

3838
/**
39-
* @return ResponseInterface|null Response generated by middleware or null in case there was an error.
39+
* @return ResponseInterface|null
4040
*/
4141
public function getResponse(): ?ResponseInterface
4242
{

Diff for: src/Event/BeforeHandlerEvent.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chiron\Pipeline\Event;
6+
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use Psr\Http\Server\RequestHandlerInterface;
9+
10+
/**
11+
* BeforeHandlerEvent is raised before executing the fallback handler.
12+
*/
13+
final class BeforeHandlerEvent
14+
{
15+
/** @var RequestHandlerInterface */
16+
private $handler;
17+
/** @var ServerRequestInterface */
18+
private $request;
19+
20+
/**
21+
* @param RequestHandlerInterface $handler
22+
* @param ServerRequestInterface $request
23+
*/
24+
public function __construct(RequestHandlerInterface $handler, ServerRequestInterface $request)
25+
{
26+
$this->handler = $handler;
27+
$this->request = $request;
28+
}
29+
30+
/**
31+
* @return RequestHandlerInterface
32+
*/
33+
public function getHandler(): RequestHandlerInterface
34+
{
35+
return $this->handler;
36+
}
37+
38+
/**
39+
* @return ServerRequestInterface
40+
*/
41+
public function getRequest(): ServerRequestInterface
42+
{
43+
return $this->request;
44+
}
45+
}

Diff for: src/Event/BeforeMiddleware.php renamed to src/Event/BeforeMiddlewareEvent.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
use Psr\Http\Server\MiddlewareInterface;
99

1010
/**
11-
* AfterMiddleware event is raised before executing a middleware.
11+
* BeforeMiddlewareEvent is raised before executing a middleware.
1212
*/
13-
final class BeforeMiddleware
13+
final class BeforeMiddlewareEvent
1414
{
1515
/** @var MiddlewareInterface */
1616
private $middleware;
1717
/** @var ServerRequestInterface */
1818
private $request;
1919

2020
/**
21-
* @param MiddlewareInterface $middleware Middleware to be executed.
22-
* @param ServerRequestInterface $request Request to be passed to the middleware.
21+
* @param MiddlewareInterface $middleware
22+
* @param ServerRequestInterface $request
2323
*/
2424
public function __construct(MiddlewareInterface $middleware, ServerRequestInterface $request)
2525
{
@@ -28,15 +28,15 @@ public function __construct(MiddlewareInterface $middleware, ServerRequestInterf
2828
}
2929

3030
/**
31-
* @return MiddlewareInterface Middleware to be executed.
31+
* @return MiddlewareInterface
3232
*/
3333
public function getMiddleware(): MiddlewareInterface
3434
{
3535
return $this->middleware;
3636
}
3737

3838
/**
39-
* @return ServerRequestInterface Request to be passed to the middleware.
39+
* @return ServerRequestInterface
4040
*/
4141
public function getRequest(): ServerRequestInterface
4242
{

Diff for: src/Pipeline.php

+18-29
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
use Psr\Http\Server\MiddlewareInterface;
1212
use Psr\Http\Server\RequestHandlerInterface;
1313
use Psr\EventDispatcher\EventDispatcherInterface;
14-
use Chiron\Pipeline\Event\BeforeMiddleware;
15-
use Chiron\Pipeline\Event\AfterMiddleware;
14+
use Chiron\Pipeline\Event\BeforeMiddlewareEvent;
15+
use Chiron\Pipeline\Event\AfterMiddlewareEvent;
16+
use Chiron\Pipeline\Event\BeforeHandlerEvent;
17+
use Chiron\Pipeline\Event\AfterHandlerEvent;
1618

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

22+
//https://github.com/middlewares/utils/blob/master/src/Dispatcher.php#L43
23+
2024
// TODO : vérifier qu'on a pas de doublons de middlewares : https://github.com/illuminate/routing/blob/f0908784ce618438be1a8b99f4613f62d18d8013/Router.php#L1256
2125

2226
/**
@@ -34,6 +38,7 @@
3438
* @see https://www.php-fig.org/psr/psr-15/meta/
3539
*/
3640
// TODO : actualiser la phpDoc et ajouter un @see sur les specs psr14 pour les events !!!!
41+
// TODO : ajouter des tests pour les events
3742
final class Pipeline implements RequestHandlerInterface
3843
{
3944
/** @var EventDispatcherInterface */
@@ -89,51 +94,35 @@ public function fallback(RequestHandlerInterface $handler): self
8994
*/
9095
public function handle(ServerRequestInterface $request): ResponseInterface
9196
{
92-
// TODO : remplacer cette appel au binding par un événement "BeforeMiddleware" : exemple : https://github.com/yiisoft/middleware-dispatcher/blob/master/src/MiddlewareStack.php#L85
93-
// Attach a fresh instance of the request in the container.
94-
//$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.
97+
// 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 !!!
9598

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

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

104-
//if (isset($this->middlewares[$this->position])) {
105102
if ($middleware !== null) {
106-
$this->dispatcher->dispatch(new BeforeMiddleware($middleware, $request));
107-
103+
$this->dispatcher->dispatch(new BeforeMiddlewareEvent($middleware, $request));
108104
try {
109105
return $response = $middleware->process($request, $this->nextHandler());
110106
} finally {
111-
$this->dispatcher->dispatch(new AfterMiddleware($middleware, $response ?? null));
107+
$this->dispatcher->dispatch(new AfterMiddlewareEvent($middleware, $response ?? null));
112108
}
113109
}
114110

115-
return $this->fallback->handle($request);
111+
// 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 !!!!
112+
$this->dispatcher->dispatch(new BeforeHandlerEvent($this->fallback, $request));
113+
try {
114+
return $response = $this->fallback->handle($request);;
115+
} finally {
116+
$this->dispatcher->dispatch(new AfterHandlerEvent($this->fallback, $response ?? null));
117+
}
116118
}
117119

118-
/**
119-
* Store a "fresh" Request instance in the container.
120-
* Usefull if you need to retrieve some request attributes.
121-
*
122-
* Ex: in the CookieCollectionMiddleware with the CookieCollection::ATTRIBUTE
123-
*
124-
* @param ServerRequestInterface $request
125-
*/
126-
/*
127-
private function bindRequestInstance(ServerRequestInterface $request): void
128-
{
129-
$this->container->bind(ServerRequestInterface::class, $request);
130-
}*/
131-
132120
/**
133121
* Get a handler pointing to the next middleware position.
134122
*
135123
* @return RequestHandlerInterface New Pipeline instance used as handler.
136124
*/
125+
// 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 !!!
137126
private function nextHandler(): RequestHandlerInterface
138127
{
139128
$copy = clone $this;

0 commit comments

Comments
 (0)