|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the uSilex framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Enrico Fagnoni <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace uSilex\Provider\Psr7; |
|
13
|
|
|
|
|
14
|
|
|
use Pimple\Container; |
|
15
|
|
|
use Pimple\ServiceProviderInterface; |
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
17
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
|
18
|
|
|
use GuzzleHttp\Psr7\Response; |
|
19
|
|
|
use Zend\HttpHandlerRunner\Emitter\SapiStreamEmitter; |
|
20
|
|
|
use Exception; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* This service provider uses Guzzle\PSr7 to resolve uSilex as PSR-7 dependencies. |
|
24
|
|
|
* |
|
25
|
|
|
* As default, uSilex.responseEmitter uses SapiStreamEmitter class. |
|
26
|
|
|
* |
|
27
|
|
|
* As default, uSilex.exceptionHandler uses JsonResponse as custom response with 500 as error code. |
|
28
|
|
|
* |
|
29
|
|
|
* |
|
30
|
|
|
* Add this dependencies to your project: |
|
31
|
|
|
* |
|
32
|
|
|
* composer require guzzlehttp/Psr7 |
|
33
|
|
|
* composer require zendframework/zend-httphandlerrunner |
|
34
|
|
|
* |
|
35
|
|
|
* USAGE: |
|
36
|
|
|
* $app->register( new \uSilex\Provider\Psr7\GuzzleProvider() ); |
|
37
|
|
|
* |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
class GuzzleServiceProvider implements ServiceProviderInterface |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function register(Container $app) |
|
48
|
|
|
{ |
|
49
|
|
|
$app['uSilex.request'] = function () { |
|
50
|
1 |
|
return ServerRequest::fromGlobals(); |
|
51
|
|
|
}; |
|
52
|
|
|
|
|
53
|
|
|
$app['uSilex.responseEmitter'] = $app->protect(function ($response) { |
|
54
|
1 |
|
(new SapiStreamEmitter())->emit($response); |
|
55
|
3 |
|
}); |
|
56
|
|
|
|
|
57
|
|
|
$app['uSilex.exceptionHandler'] = $app->protect(function ($e) { |
|
58
|
1 |
|
return new Response(500, [], $e->getMessage()); |
|
59
|
3 |
|
}); |
|
60
|
3 |
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|