Skip to content

Commit 6ce52a0

Browse files
Additional documentation added
1 parent 0ab25af commit 6ce52a0

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

README.md

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,44 @@ ZF2 Events
9494

9595
### Listeners
9696

97-
#### `ZF\ApiProblem\ApiProblemListener` (a.k.a. `ZF\ApiProblem\Listener\ApiProblemListener`)
97+
#### `ZF\ApiProblem\Listener\ApiProblemListener`
9898

99-
#### `ZF\ApiProblem\RenderErrorListener` (a.k.a. `ZF\ApiProblem\Listener\RenderErrorListener`)
99+
The `ApiProblemListener` attaches to three events in the MVC lifecycle:
100+
101+
- `MvcEvent::EVENT_RENDER` with a priority of 1000
102+
- `MvcEvent::EVENT_DISPATCH_ERROR` with a priority of 100
103+
- `MvcEvent::EVENT_DISPATCH` as a _shared_ event on `Zend\Stdlib\DispatchableInterface` with a
104+
priority of 100
105+
106+
If the current Accept media type does not match the configured api-problem media types,
107+
then this listener passes on taking any action.
108+
109+
When this listener does take action, the purposes are threefold:
110+
111+
- Before dispatching, the `render_error_controllers` configuration value is consulted to determine
112+
if the `ZF\ApiProblem\Listener\RenderErrorListener` should be attached, see
113+
`RenderErrorListener` for more information.
114+
- After dispatching, detect the type of response from the controller, if it is already an
115+
ApiProblem model, continue. If not, but there was an exception throw during dispatch,
116+
convert the response to an api-problem with some information as to what the exception is.
117+
- If a dispatch error has occurred, and is a valid Accept type, attempt to cast the dispatch
118+
exception into an API problem response.
119+
120+
#### `ZF\ApiProblem\Listener\RenderErrorListener`
121+
122+
This listener is attached to `MvcEvent::EVENT_RENDER_ERROR` at priority `100`. This listener
123+
is conditionally attached by `ZF\ApiProblem\Listener\ApiProblemListener` for controllers that
124+
require API Problem responses. With a priority of `100`, this ensures that this Render Error
125+
time listener will run before the ZF2 one. In cases when it does run, it will attempt to
126+
take exceptions and inject an api-problem response in the HTTP response.
100127

101128
#### `ZF\ApiProblem\Listener\SendApiProblemResponseListener`
102129

130+
This listener is attached to the `SendResponseEvent::EVENT_SEND_RESPONSE` at priority `-500`.
131+
The primary purpose of this listener is to, when an ApiProblem response is to be sent, to
132+
send the headers and content body. This differs from the way ZF2 typically sends responses
133+
in that inside this listener it takes into account if the Response should include an
134+
exception trace as part of the serialized response or not.
103135

104136
ZF2 Services
105137
------------
@@ -112,7 +144,58 @@ ZF2 Services
112144

113145
### View Services
114146

115-
#### `ZF\ApiProblem\ApiProblemRenderer` (a.k.a. `ZF\ApiProblem\View\ApiProblemRenderer`)
147+
#### `ZF\ApiProblem\View\ApiProblemRenderer`
148+
149+
This service extends the JsonRenderer service from the ZF2 MVC layer. It's primary responsibility
150+
is to decorate JSON rendering with the ability to optionally output stack traces.
116151

117-
#### `ZF\ApiProblem\ApiProblemStrategy` (a.k.a. `ZF\ApiProblem\View\ApiProblemStrategy`)
152+
#### `ZF\ApiProblem\View\ApiProblemStrategy`
118153

154+
This service is a view strategy that allows any ApiProblemModels details to be injected into the
155+
MVC's HTTP response object. This is similar in nature to the `JsonStrategy`.
156+
157+
### Models
158+
159+
#### `ZF\ApiProblem\ApiProblem`
160+
161+
An instance of `ZF\ApiProblem\ApiProblem` serves the purpose of modeling the kind of problem that
162+
is encountered. An instance of `ApiProblem` is typically wrapped in an `ApiProblemResponse`
163+
(see `ApiProblemResponse` below). Most information can be passed into the constructor:
164+
165+
```php
166+
class ApiProblem {
167+
public function __construct($status, $detail, $type = null, $title = null, array $additional = array()) {}
168+
}
169+
```
170+
171+
For example:
172+
173+
```php
174+
new ApiProblem(404, 'Entity not found');
175+
// or
176+
new ApiProblem(424, $exceptionInstance);
177+
```
178+
179+
#### `ZF\ApiProblem\ApiProblemResponse`
180+
181+
An instance of `ZF\ApiProblem\ApiProblem` can be returned from any controller service or ZF2
182+
MVC event. When it is, the HTTP response will be converted to the proper JSON based HTTP response.
183+
184+
For example:
185+
186+
```php
187+
use ZF\ApiProblem\ApiProblem;
188+
use ZF\ApiProblem\ApiProblemResponse;
189+
class MyController extends Zend\Mvc\Controller\AbstractActionController
190+
{
191+
/* ... */
192+
public function fetch($id)
193+
{
194+
$entity = $this->model->fetch($id);
195+
if (!$entity) {
196+
return new ApiProblemResponse(ApiProblem(404, 'Entity not found'));
197+
}
198+
return $entity;
199+
}
200+
}
201+
```

src/Listener/ApiProblemListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function onDispatch(MvcEvent $e)
143143
/**
144144
* Handle render errors
145145
*
146-
* If the event representes an error, and has an exception composed, marshals an ApiProblem
146+
* If the event represents an error, and has an exception composed, marshals an ApiProblem
147147
* based on the exception, stops event propagation, and returns an ApiProblemResponse.
148148
*
149149
* @param MvcEvent $e

0 commit comments

Comments
 (0)