Skip to content

Commit 0b1ab26

Browse files
committed
Merge pull request #18 from zfcampus/feature/readme-documentation
README Documentation
2 parents 35fd871 + 6ce52a0 commit 0b1ab26

File tree

2 files changed

+195
-4
lines changed

2 files changed

+195
-4
lines changed

README.md

Lines changed: 194 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,201 @@
1-
Api-Problem: ZF2 Module for API-Problem representations
2-
=======================================================
1+
ZF Api Problem
2+
==============
33

44
[![Build Status](https://travis-ci.org/zfcampus/zf-api-problem.png)](https://travis-ci.org/zfcampus/zf-api-problem)
5-
[![Coverage Status](https://coveralls.io/repos/zfcampus/zf-api-problem/badge.png?branch=master)](https://coveralls.io/r/zfcampus/zf-api-problem)
5+
6+
Introduction
7+
------------
68

79
This module provides data structures and rendering for the API-Problem format.
810

911
- [Problem API](http://tools.ietf.org/html/draft-nottingham-http-problem-05),
1012
used for reporting API problems
13+
14+
15+
Installation
16+
------------
17+
18+
Run the following `composer` command:
19+
20+
```console
21+
$ composer require "zfcampus/zf-api-problem:~1.0-dev"
22+
```
23+
24+
Alternately, manually add the following to your `composer.json`, in the `require` section:
25+
26+
```javascript
27+
"require": {
28+
"zfcampus/zf-api-problem": "~1.0-dev"
29+
}
30+
```
31+
32+
And then run `composer update` to ensure the module is installed.
33+
34+
Finally, add the module name to your project's `config/application.config.php` under the `modules`
35+
key:
36+
37+
```php
38+
return array(
39+
/* ... */
40+
'modules' => array(
41+
/* ... */
42+
'ZF\ApiProblem',
43+
),
44+
/* ... */
45+
);
46+
```
47+
48+
Configuration
49+
-------------
50+
51+
### User Configuration
52+
53+
The top-level configuration key for user configuration of this module is `zf-api-problem`.
54+
55+
#### Key: `accept_filters`
56+
57+
// Accept types that should allow ApiProblem responses
58+
59+
#### Key: `render_error_controllers`
60+
61+
// Array of controller service names that should enable the ApiProblem render.error listener
62+
63+
64+
### System Configuration
65+
66+
The following configuration is provided in `config/module.config.php` to enable the module to
67+
function:
68+
69+
```php
70+
'service_manager' => array(
71+
'aliases' => array(
72+
'ZF\ApiProblem\ApiProblemListener' => 'ZF\ApiProblem\Listener\ApiProblemListener',
73+
'ZF\ApiProblem\RenderErrorListener' => 'ZF\ApiProblem\Listener\RenderErrorListener',
74+
'ZF\ApiProblem\ApiProblemRenderer' => 'ZF\ApiProblem\View\ApiProblemRenderer',
75+
'ZF\ApiProblem\ApiProblemStrategy' => 'ZF\ApiProblem\View\ApiProblemStrategy',
76+
),
77+
'factories' => array(
78+
'ZF\ApiProblem\Listener\ApiProblemListener' => 'ZF\ApiProblem\Factory\ApiProblemListenerFactory',
79+
'ZF\ApiProblem\Listener\RenderErrorListener' => 'ZF\ApiProblem\Factory\RenderErrorListenerFactory',
80+
'ZF\ApiProblem\Listener\SendApiProblemResponseListener' => 'ZF\ApiProblem\Factory\SendApiProblemResponseListenerFactory',
81+
'ZF\ApiProblem\View\ApiProblemRenderer' => 'ZF\ApiProblem\Factory\ApiProblemRendererFactory',
82+
'ZF\ApiProblem\View\ApiProblemStrategy' => 'ZF\ApiProblem\Factory\ApiProblemStrategyFactory',
83+
)
84+
),
85+
'view_manager' => array(
86+
// Enable this in your application configuration in order to get full
87+
// exception stack traces in your API-Problem responses.
88+
'display_exceptions' => false,
89+
),
90+
```
91+
92+
ZF2 Events
93+
----------
94+
95+
### Listeners
96+
97+
#### `ZF\ApiProblem\Listener\ApiProblemListener`
98+
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.
127+
128+
#### `ZF\ApiProblem\Listener\SendApiProblemResponseListener`
129+
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.
135+
136+
ZF2 Services
137+
------------
138+
139+
### Event Services
140+
141+
- `ZF\ApiProblem\Listener\ApiProblemListener`
142+
- `ZF\ApiProblem\Listener\RenderErrorListener`
143+
- `ZF\ApiProblem\Listener\SendApiProblemResponseListener`
144+
145+
### View Services
146+
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.
151+
152+
#### `ZF\ApiProblem\View\ApiProblemStrategy`
153+
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)