Skip to content

Commit 5099f4f

Browse files
authored
Fix #30: Make events optional (#41)
1 parent b5f1066 commit 5099f4f

6 files changed

+18
-14
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"phpunit/phpunit": "^9.5",
3232
"roave/infection-static-analysis-plugin": "^1.8",
3333
"spatie/phpunit-watcher": "^1.23",
34-
"vimeo/psalm": "^4.8",
34+
"vimeo/psalm": "^4.9",
3535
"yiisoft/test-support": "^1.3"
3636
},
3737
"autoload": {

psalm.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
77
>
88
<projectFiles>
9-
<directory name="src" />
9+
<directory name="src"/>
10+
<ignoreFiles>
11+
<directory name="vendor"/>
12+
</ignoreFiles>
1013
</projectFiles>
1114
</psalm>

src/InvalidMiddlewareDefinitionException.php

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static function (&$item, $key) {
6262
}
6363
}
6464
);
65+
/** @var string[] $items */
6566
return '[' . implode(', ', $items) . ']';
6667
}
6768

src/MiddlewareDispatcher.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ final class MiddlewareDispatcher
1919
*/
2020
private ?MiddlewareStack $stack = null;
2121
private MiddlewareFactoryInterface $middlewareFactory;
22-
private EventDispatcherInterface $eventDispatcher;
22+
private ?EventDispatcherInterface $eventDispatcher;
2323

2424
/**
2525
* @var array[]|callable[]|string[]
2626
*/
2727
private array $middlewareDefinitions = [];
2828

29-
public function __construct(MiddlewareFactoryInterface $middlewareFactory, EventDispatcherInterface $eventDispatcher)
29+
public function __construct(MiddlewareFactoryInterface $middlewareFactory, ?EventDispatcherInterface $eventDispatcher = null)
3030
{
3131
$this->middlewareFactory = $middlewareFactory;
3232
$this->eventDispatcher = $eventDispatcher;

src/MiddlewareStack.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class MiddlewareStack implements RequestHandlerInterface
2323
* @var RequestHandlerInterface|null stack of middleware
2424
*/
2525
private ?RequestHandlerInterface $stack = null;
26-
private EventDispatcherInterface $eventDispatcher;
26+
private ?EventDispatcherInterface $eventDispatcher;
2727
private RequestHandlerInterface $fallbackHandler;
2828
private array $middlewares;
2929

@@ -33,7 +33,7 @@ final class MiddlewareStack implements RequestHandlerInterface
3333
* @param EventDispatcherInterface $eventDispatcher Event dispatcher to use for triggering before/after middleware
3434
* events.
3535
*/
36-
public function __construct(array $middlewares, RequestHandlerInterface $fallbackHandler, EventDispatcherInterface $eventDispatcher)
36+
public function __construct(array $middlewares, RequestHandlerInterface $fallbackHandler, ?EventDispatcherInterface $eventDispatcher = null)
3737
{
3838
if ($middlewares === []) {
3939
throw new RuntimeException('Stack is empty.');
@@ -75,12 +75,12 @@ private function wrap(Closure $middlewareFactory, RequestHandlerInterface $handl
7575
private Closure $middlewareFactory;
7676
private ?MiddlewareInterface $middleware = null;
7777
private RequestHandlerInterface $handler;
78-
private EventDispatcherInterface $eventDispatcher;
78+
private ?EventDispatcherInterface $eventDispatcher;
7979

8080
public function __construct(
8181
Closure $middlewareFactory,
8282
RequestHandlerInterface $handler,
83-
EventDispatcherInterface $eventDispatcher
83+
?EventDispatcherInterface $eventDispatcher
8484
) {
8585
$this->middlewareFactory = $middlewareFactory;
8686
$this->handler = $handler;
@@ -94,12 +94,16 @@ public function handle(ServerRequestInterface $request): ResponseInterface
9494
$this->middleware = ($this->middlewareFactory)();
9595
}
9696

97-
$this->eventDispatcher->dispatch(new BeforeMiddleware($this->middleware, $request));
97+
if ($this->eventDispatcher !== null) {
98+
$this->eventDispatcher->dispatch(new BeforeMiddleware($this->middleware, $request));
99+
}
98100

99101
try {
100102
return $response = $this->middleware->process($request, $this->handler);
101103
} finally {
102-
$this->eventDispatcher->dispatch(new AfterMiddleware($this->middleware, $response ?? null));
104+
if ($this->eventDispatcher !== null) {
105+
$this->eventDispatcher->dispatch(new AfterMiddleware($this->middleware, $response ?? null));
106+
}
103107
}
104108
}
105109
};

tests/MiddlewareDispatcherTest.php

-4
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
193193

194194
private function createDispatcher(ContainerInterface $container = null, ?EventDispatcherInterface $eventDispatcher = null): MiddlewareDispatcher
195195
{
196-
if ($eventDispatcher === null) {
197-
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
198-
}
199-
200196
if ($container === null) {
201197
return new MiddlewareDispatcher(
202198
new MiddlewareFactory($this->createContainer()),

0 commit comments

Comments
 (0)