|
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 Delegate implements DelegateInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var SplObjectStorage |
|
15
|
|
|
*/ |
|
16
|
|
|
private $middleware; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var DelegateInterface $nextDelegate |
|
20
|
|
|
*/ |
|
21
|
|
|
private $nextDelegate; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ContainerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $container; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private $index = 0; |
|
32
|
|
|
|
|
33
|
4 |
|
public function __construct(array $middleware, DelegateInterface $nextDelegate, ContainerInterface $container = null) |
|
34
|
|
|
{ |
|
35
|
4 |
|
$this->middleware = $middleware; |
|
|
|
|
|
|
36
|
4 |
|
$this->nextDelegate = $nextDelegate; |
|
37
|
4 |
|
$this->container = $container; |
|
38
|
4 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
// DelegateInterface |
|
41
|
4 |
|
public function process(ServerRequestInterface $request) |
|
42
|
|
|
{ |
|
43
|
4 |
|
if (empty($this->middleware[$this->index])) { |
|
44
|
4 |
|
return $this->nextDelegate->process($request); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** @var callable */ |
|
48
|
3 |
|
$condition = $this->middleware[$this->index][0]; |
|
49
|
|
|
|
|
50
|
|
|
/** @var MiddlewareInterface|string */ |
|
51
|
3 |
|
$middleware = $this->middleware[$this->index][1]; |
|
52
|
|
|
|
|
53
|
|
|
/** @var DelegateInterface */ |
|
54
|
3 |
|
$delegate = $this->nextDelegate(); |
|
55
|
|
|
|
|
56
|
3 |
|
if ($condition($request)) { |
|
57
|
3 |
|
return $this->resolveMiddleware($middleware)->process($request, $delegate); |
|
58
|
|
|
} else { |
|
59
|
1 |
|
return $delegate->process($request); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return DelegateInterface |
|
65
|
|
|
*/ |
|
66
|
3 |
|
private function nextDelegate() |
|
67
|
|
|
{ |
|
68
|
3 |
|
$copy = clone $this; |
|
69
|
3 |
|
$copy->index++; |
|
70
|
|
|
|
|
71
|
3 |
|
return $copy; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string|MiddlewareInterface $middleware |
|
76
|
|
|
* @return MiddlewareInterface |
|
77
|
|
|
*/ |
|
78
|
3 |
|
private function resolveMiddleware($middleware) |
|
79
|
|
|
{ |
|
80
|
3 |
|
if ($middleware instanceof MiddlewareInterface) { |
|
81
|
2 |
|
return $middleware; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
return $this->container->get($middleware); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..