|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Northwoods\Broker; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface; |
|
6
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface; |
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use SplObjectStorage; |
|
10
|
|
|
|
|
11
|
|
|
class Broker implements MiddlewareInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ContainerInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $container; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var SplObjectStorage |
|
20
|
|
|
*/ |
|
21
|
|
|
private $middleware; |
|
22
|
|
|
|
|
23
|
4 |
|
public function __construct(ContainerInterface $container = null) |
|
24
|
|
|
{ |
|
25
|
4 |
|
$this->container = $container; |
|
26
|
4 |
|
$this->middleware = new SplObjectStorage(); |
|
27
|
4 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function handle(ServerRequestInterface $request, callable $default) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$delegate = new CallableDelegate($default); |
|
35
|
|
|
|
|
36
|
1 |
|
return $this->process($request, $delegate); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// MiddlewareInterface |
|
40
|
4 |
|
public function process(ServerRequestInterface $request, DelegateInterface $delegate) |
|
41
|
3 |
|
{ |
|
42
|
4 |
|
$delegate = new Delegate($this->middleware, $delegate); |
|
43
|
|
|
|
|
44
|
4 |
|
return $delegate->process($request); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array|string|MiddlewareInterface $middleware |
|
49
|
|
|
* @return self |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function always($middleware) |
|
52
|
|
|
{ |
|
53
|
3 |
|
return $this->when($this->alwaysTrue(), $middleware); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array|string|MiddlewareInterface $middleware |
|
58
|
|
|
* @return self |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function when(callable $condition, $middleware) |
|
61
|
|
|
{ |
|
62
|
3 |
|
if (!is_array($middleware)) { |
|
63
|
2 |
|
$middleware = [$middleware]; |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
foreach ($middleware as $mw) { |
|
67
|
3 |
|
$this->addConditionalMiddleware($condition, $this->resolveMiddleware($mw)); |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
3 |
|
private function addConditionalMiddleware(callable $condition, MiddlewareInterface $middleware) |
|
77
|
|
|
{ |
|
78
|
3 |
|
$this->middleware[$middleware] = $condition; |
|
79
|
3 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string|MiddlewareInterface $middleware |
|
83
|
|
|
* @return MiddlewareInterface |
|
84
|
|
|
*/ |
|
85
|
3 |
|
private function resolveMiddleware($middleware) |
|
86
|
|
|
{ |
|
87
|
3 |
|
if ($middleware instanceof MiddlewareInterface) { |
|
88
|
2 |
|
return $middleware; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
return $this->container->get($middleware); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return callable |
|
96
|
|
|
*/ |
|
97
|
|
|
private function alwaysTrue() |
|
98
|
|
|
{ |
|
99
|
3 |
|
return static function () { |
|
100
|
3 |
|
return true; |
|
101
|
3 |
|
}; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|