@@ -55,7 +55,50 @@ This latter approach ensures that you are only providing problem details for
5555specific API endpoints, which can be useful when you have a mix of APIs and
5656traditional web content in your application.
5757
58- ### Factory
58+ ## Listeners
59+
60+ - Since 0.5.2
61+
62+ The ` ProblemDetailsMiddleware ` allows you to register _ listeners_ to trigger
63+ when it handles a ` Throwable ` . Listeners are PHP callables, and the middleware
64+ triggers them with the following arguments, in the following order:
65+
66+ - ` Throwable $error ` : the throwable/exception caught by the middleware.
67+ - ` ServerRequestInterface $request ` : the request as provided to the
68+ ` ProblemDetailsMiddleware ` .
69+ - ` ResponseInterface $response ` : the response the ` ProblemDetailsMiddleware `
70+ generated based on the ` $error ` .
71+
72+ Note that each of these arguments are immutable; you cannot change the state in
73+ a way that that state will propagate meaningfully. As such, you should use
74+ listeners for reporting purposes only (e.g., logging).
75+
76+ As an example:
77+
78+ ``` php
79+ // Where $logger is a PSR-3 logger implementation
80+ $listener = function (
81+ Throwable $error,
82+ ServerRequestInterface $request,
83+ ResponseInterface $response
84+ ) use ($logger) {
85+ $logger->error('[{status}] {method} {uri}: {message}', [
86+ 'status' => $response->getStatusCode(),
87+ 'method' => $request->getMethod(),
88+ 'uri' => (string) $request->getUri(),
89+ 'message' => $error->getMessage(),
90+ ]);
91+ };
92+ ```
93+
94+ Attach listeners to the ` ProblemDetailsMiddleware ` instance using its
95+ ` attachListener() ` method:
96+
97+ ``` php
98+ $middleware->attachListener($listener);
99+ ```
100+
101+ ## Factory
59102
60103The ` ProblemDetailsMiddleware ` ships with a corresponding PSR-11 compatible factory,
61104` ProblemDetailsMiddlewareFactory ` . This factory looks for a service named
@@ -65,3 +108,35 @@ to instantiate the middleware.
65108For Expressive 2 users, this middleware should be registered automatically with
66109your application on install, assuming you have the zend-component-installer
67110plugin in place (it's shipped by default with the Expressive skeleton).
111+
112+ ### Registering listeners
113+
114+ - Since 0.5.2
115+
116+ In order to register listeners, we recommend using a
117+ [ delegator factory] ( https://docs.zendframework.com/zend-expressive/features/container/delegator-factories/ )
118+ on the ` Zend\ProblemDetails\ProblemDetailsMiddleware ` service.
119+
120+ As an example:
121+
122+ ``` php
123+ class LoggerProblemDetailsListenerDelegator
124+ {
125+ public function __construct(ContainerInterface $container, $serviceName, callable $callback)
126+ {
127+ $middleware = $callback();
128+ $middleware->attachListener($container->get(LoggerProblemDetailsListener::class));
129+ return $middleware;
130+ }
131+ }
132+ ```
133+
134+ You would then register this as a delegator factory in your configuration:
135+
136+ ``` php
137+ 'delegators' => [
138+ ProblemDetailsMiddleware::class => [
139+ LoggerProblemDetailsListenerDelegator::class,
140+ ],
141+ ],
142+ ```
0 commit comments