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 signatur 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
|
|
|
|
46
|
|
|
class ZendPipeServiceProvider implements ServiceProviderInterface |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
1 |
|
public function register(Container $app) |
52
|
|
|
{ |
53
|
|
|
$app['piper'] = function() { |
54
|
1 |
|
return new MiddlewarePipe(); |
55
|
|
|
}; |
56
|
|
|
|
57
|
1 |
|
$app['handler.queue'] = []; |
58
|
|
|
|
59
|
|
|
$app['uSilex.httpHandler'] = function($app) { |
60
|
1 |
|
$piper = $app['piper']; |
61
|
1 |
|
foreach ($app['handler.queue'] as $middlewareService){ |
62
|
|
|
$piper->pipe( $app[$middlewareService]); |
63
|
|
|
} |
64
|
1 |
|
return $piper; |
65
|
|
|
}; |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|