Skip to content
This repository was archived by the owner on Nov 23, 2019. It is now read-only.

Commit a516d89

Browse files
committed
Merge branch 'develop'
2 parents 34cb64b + 504b7a5 commit a516d89

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

README.md

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
[![Total Downloads](https://poser.pugx.org/acelaya/ze-content-based-error-handler/downloads.png)](https://packagist.org/packages/acelaya/ze-content-based-error-handler)
88
[![License](https://poser.pugx.org/acelaya/ze-content-based-error-handler/license.png)](https://packagist.org/packages/acelaya/ze-content-based-error-handler)
99

10-
A Zend Expressive error handler which allows to implement different strategies to render error responses based on the accepted content-types.
10+
A Zend Expressive error response generator which allows to implement different strategies to render error responses based on the accepted content-types.
1111

1212
### Context
1313

14-
This package has been created following this article https://blog.alejandrocelaya.com/2016/07/29/creating-a-content-based-error-handler-for-zend-expressive/.
14+
This package was created following this article https://blog.alejandrocelaya.com/2016/07/29/creating-a-content-based-error-handler-for-zend-expressive/.
1515

1616
On it, I demonstrate how to implement an strategy-based system which generates different error responses by taking into account the request's `Accept` header.
1717

18-
After writing the article I have decided to create this package, so that everybody can install and use the provided solution in their own projects.
18+
After writing the article I decided to create this package, so that everybody can install and use the provided solution in their own projects.
19+
20+
The package has then evolved to support expressive 2, which completely drops the concept of error handlers. Instead, from v2, this provides error response generators.
1921

2022
### Installation
2123

@@ -25,31 +27,32 @@ Use composer to install this package
2527

2628
### Usage
2729

28-
This package includes an error handler, the `Acelaya\ExpressiveErrorHandler\ErrorHandler\ContentBasedErrorResponseGenerator`, that can be used to replace default Zend Expressive implementations.
30+
This package includes an error response generator, the `Acelaya\ExpressiveErrorHandler\ErrorHandler\ContentBasedErrorResponseGenerator`, that can be used to replace default Zend Expressive implementations.
2931

30-
It composes a plugin manager that fetches a concrete error handler at runtime, based on the Request's `Accept` header. Thus, you can use the Expressive's `TemplatedErrorHandler` to dispatch **text/html** request errors, Stratiglity's `FinalHandler` for **text/plain** errors, etc.
32+
It composes a plugin manager that fetches a concrete error response generator at runtime, based on the Request's `Accept` header. Thus, you can use the Expressive's `ErrorResponseGenerator` to dispatch **text/html** request errors, Stratiglity's `ErrorResponseGenerator` for **text/plain** errors, etc.
3133

3234
You can also provide your own implementations for other content-types, like **application/json** or **text/xml**. The ContentBasedErrorResponseGenerator will automatically use the proper implementation.
3335

3436
### Provided configuration
3537

36-
To get things easily working, a `ConfigProvider` is included, which automatically registers all the dependencies in the service container (including the `Zend\Expressive\ErroHandler` service).
38+
To get things easily working, a `ConfigProvider` is included, which automatically registers all the dependencies in the service container (including the `Zend\Expressive\Middleware\ErrorResponseGenerator` service).
3739

38-
It also preregisters error handlers for html and plain text requests (The `TemplatedErrorHandler` and the `FinalHandler` as mentioned before).
40+
It also preregisters error handlers for html and plain text requests (The `Zend\Expressive\Middleware\ErrorResponseGenerator` and the `Zend\Stratigility\Middleware\ErrorResponseGenerator` as mentioned before).
3941

4042
```php
4143
<?php
44+
use Acelaya\ExpressiveErrorHandler\ErrorHandler\Factory\PlainTextResponseGeneratorFactory;
45+
use Zend\Expressive\Container\ErrorResponseGeneratorFactory;
46+
4247
return [
4348

4449
'error_handler' => [
4550
'default_content_type' => 'text/html',
4651

4752
'plugins' => [
48-
'invokables' => [
49-
'text/plain' => FinalHandler::class,
50-
],
5153
'factories' => [
52-
'text/html' => TemplatedErrorHandlerFactory::class,
54+
'text/plain' => PlainTextResponseGeneratorFactory::class,
55+
'text/html' => ErrorResponseGeneratorFactory::class,
5356
],
5457
'aliases' => [
5558
'application/xhtml+xml' => 'text/html',
@@ -69,32 +72,45 @@ In order to use the built-in ConfigProvider, create a config file with this cont
6972
return (new Acelaya\ExpressiveErrorHandler\ConfigProvider())->__invoke();
7073
```
7174

72-
If your are using the Expressive's ConfigManager ([mtymek/expressive-config-manager](https://github.com/mtymek/expressive-config-manager)), you can just pass the class name to it like this:
75+
If your are using [zend config aggregator](https://github.com/zendframework/zend-config-aggregator), you can just pass the class name to it like this:
7376

7477
```php
75-
return (new Zend\Expressive\ConfigManager([
78+
return (new Zend\ConfigAggregator\ConfigAggregator([
7679
Acelaya\ExpressiveErrorHandler\ConfigProvider::class,
7780
// [...]
78-
new ZendConfigProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
81+
new Zend\ConfigAggregator\ZendConfigProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
7982
], 'data/cache/app_config.php'))->getMergedConfig();
8083
```
8184

85+
Also, if you are using the [zend component installer](https://docs.zendframework.com/zend-component-installer/) package, it will ask you to register the ConfigProvider when installed.
86+
8287
### Override configuration
8388

8489
If you need to override any of the content types, its as easy as defining the same plugin with a different value.
8590

86-
For example, it is very likely that you want to use Expressive's `WhoopsErrorHandler` in development environments.
91+
For example, it is very likely that you want to use Expressive's `WhoopsErrorResponseGenerator` in development environments.
8792

8893
Just define a local configuration file with this content and all the html requests will use it from now on:
8994

9095
```php
9196
<?php
97+
use Zend\Expressive\Container\WhoopsErrorResponseGeneratorFactory;
98+
use Zend\Expressive\Container\WhoopsFactory;
99+
use Zend\Expressive\Container\WhoopsPageHandlerFactory;
100+
92101
return [
93102

103+
'dependencies' => [
104+
'factories' => [
105+
'Zend\Expressive\Whoops' => WhoopsFactory::class,
106+
'Zend\Expressive\WhoopsPageHandler' => WhoopsPageHandlerFactory::class,
107+
]
108+
],
109+
94110
'error_handler' => [
95111
'plugins' => [
96112
'factories' => [
97-
'text/html' => WhoopsErrorHandlerFactory::class,
113+
'text/html' => WhoopsErrorResponseGeneratorFactory::class,
98114
],
99115
],
100116
],
@@ -106,15 +122,18 @@ You will probably need to define other error handlers for different content type
106122

107123
```php
108124
<?php
125+
use App\ErrorHandler\Factory\XmlErrorResponseGeneratorFactory;
126+
use App\ErrorHandler\JsonErrorResponseGenerator;
127+
109128
return [
110129

111130
'error_handler' => [
112131
'plugins' => [
113132
'invokables' => [
114-
'application/json' => JsonErrorHandler::class,
133+
'application/json' => JsonErrorResponseGenerator::class,
115134
],
116135
'factories' => [
117-
'text/xml' => XmlErrorHandlerFactory::class,
136+
'text/xml' => XmlErrorResponseGeneratorFactory::class,
118137
],
119138
'aliases' => [
120139
'application/x-json' => 'application/json',
@@ -126,7 +145,7 @@ return [
126145
];
127146
```
128147

129-
With this configuration, the `ContentBasedErrorResponseGenerator` will create the proper `JsonErroHandler` or `XmlErroHandler` at runtime, to dispatch json or xml errors.
148+
With this configuration, the `ContentBasedErrorResponseGenerator` will create the proper `JsonErrorResponseGenerator` or `XmlErrorResponseGenerator` at runtime, to dispatch json or xml errors.
130149

131150
Similarly, you could need to override the default content type by setting the `default_content_type` property.
132151

0 commit comments

Comments
 (0)