|
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
|
|
|
/** |
|
13
|
|
|
* This service provider uses https://docs.zendframework.com/zend-stratigility to define |
|
14
|
|
|
* uSilex.httpHandler service. |
|
15
|
|
|
* |
|
16
|
|
|
* Add this dependency to your project: |
|
17
|
|
|
* |
|
18
|
|
|
* composer require zendframework/zend-stratigility |
|
19
|
|
|
* |
|
20
|
|
|
* USAGE: |
|
21
|
|
|
* you need do define the service handler.queue that contains the list |
|
22
|
|
|
* of middleware to execute. You can use the id of a service that realize a middleware, |
|
23
|
|
|
* a concrete middleware instance or a callable with the signature recognized by relay |
|
24
|
|
|
* |
|
25
|
|
|
* $app->register( new ZenPipeServiceProvider() ); |
|
26
|
|
|
* $app['handler.queue'] = [ |
|
27
|
|
|
* path('/foo', middleware(function ($req, $handler) { |
|
28
|
|
|
* $response = new Response(); |
|
29
|
|
|
* $response->getBody()->write('FOO!'); |
|
30
|
|
|
* |
|
31
|
|
|
* return $response; |
|
32
|
|
|
* })), |
|
33
|
|
|
* |
|
34
|
|
|
* new NotFoundHandler(function () { |
|
35
|
|
|
* return new Response(); |
|
36
|
|
|
* }); |
|
37
|
|
|
* ]; |
|
38
|
|
|
*/ |
|
39
|
|
|
namespace uSilex\Provider\Psr15; |
|
40
|
|
|
|
|
41
|
|
|
use Pimple\Container; |
|
42
|
|
|
use Pimple\ServiceProviderInterface; |
|
43
|
|
|
use Zend\Stratigility\MiddlewarePipe; |
|
44
|
|
|
|
|
45
|
|
|
class ZendPipeServiceProvider implements ServiceProviderInterface |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function register(Container $app) |
|
51
|
|
|
{ |
|
52
|
|
|
$app['piper'] = function () { |
|
53
|
1 |
|
return new MiddlewarePipe(); |
|
54
|
|
|
}; |
|
55
|
|
|
|
|
56
|
1 |
|
$app['handler.queue'] = []; |
|
57
|
|
|
|
|
58
|
|
|
$app['uSilex.httpHandler'] = function ($app) { |
|
59
|
1 |
|
$piper = $app['piper']; |
|
60
|
1 |
|
foreach ($app['handler.queue'] as $middlewareService) { |
|
61
|
|
|
$piper->pipe($app[$middlewareService]); |
|
62
|
|
|
} |
|
63
|
1 |
|
return $piper; |
|
64
|
|
|
}; |
|
65
|
1 |
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|