-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bus.php
172 lines (153 loc) · 4.94 KB
/
Bus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Bus;
use Cubiche\Core\Bus\Exception\InvalidMiddlewareException;
use Cubiche\Core\Bus\Middlewares\MiddlewareInterface;
use Cubiche\Core\Collections\ArrayCollection\ArrayList;
use Cubiche\Core\Collections\ArrayCollection\SortedArrayHashMap;
use Cubiche\Core\Comparable\Comparator;
use Cubiche\Core\Comparable\ReverseComparator;
use Cubiche\Core\Delegate\Delegate;
use Cubiche\Core\Specification\Criteria;
/**
* Bus class.
*
* @author Ivannis Suárez Jerez <[email protected]>
*/
class Bus implements BusInterface
{
/**
* @var SortedArrayHashMap
*/
protected $middlewares;
/**
* Bus constructor.
*
* @param MiddlewareInterface[] $middlewares
*/
public function __construct(array $middlewares = array())
{
$this->middlewares = new SortedArrayHashMap([], new ReverseComparator(new Comparator()));
foreach ($middlewares as $priority => $middleware) {
if (!$middleware instanceof MiddlewareInterface) {
throw InvalidMiddlewareException::forUnknownValue($middleware);
}
$this->addMiddleware($middleware, $priority);
}
}
/**
* Adds a middleware to the middleware list. The higher priority value, the earlier a middleware
* will be triggered in the chain (defaults to 0).
*
* @param MiddlewareInterface $middleware
* @param int $priority
*/
public function addMiddleware(MiddlewareInterface $middleware, $priority = 0)
{
if (!$this->middlewares->containsKey($priority)) {
$this->middlewares->set($priority, new ArrayList());
}
/** @var ArrayList $middlewares */
$middlewares = $this->middlewares->get($priority);
if ($middlewares->findOne(Criteria::eq($middleware)) === null) {
$middlewares->add($middleware);
}
}
/**
* Add a middleware before a given middleware.
*
* @param MiddlewareInterface $middleware
* @param MiddlewareInterface $target
*
* @throws \InvalidArgumentException
*/
public function addMiddlewareBefore(MiddlewareInterface $middleware, MiddlewareInterface $target)
{
$priority = $this->middlewarePriority($target);
if ($priority === null) {
throw new \InvalidArgumentException(
sprintf(
'There is not a middleware of type %s registered.',
get_class($target)
)
);
}
$this->addMiddleware($middleware, $priority + 1);
}
/**
* Add a middleware before a given middleware.
*
* @param MiddlewareInterface $middleware
* @param MiddlewareInterface $target
*
* @throws \InvalidArgumentException
*/
public function addMiddlewareAfter(MiddlewareInterface $middleware, MiddlewareInterface $target)
{
$priority = $this->middlewarePriority($target);
if ($priority === null) {
throw new \InvalidArgumentException(
sprintf(
'There is not a middleware of type %s registered.',
get_class($target)
)
);
}
$this->addMiddleware($middleware, $priority - 1);
}
/**
* @param MiddlewareInterface $middleware
*
* @return int|null
*/
protected function middlewarePriority(MiddlewareInterface $middleware)
{
/** @var ArrayList $collection */
foreach ($this->middlewares as $priority => $collection) {
$targetMiddleware = $collection->findOne(Criteria::eq($middleware));
if ($targetMiddleware !== null) {
return $priority;
}
}
return;
}
/**
* {@inheritdoc}
*/
public function dispatch(MessageInterface $message)
{
$chainedMiddleware = $this->chainedExecution();
return $chainedMiddleware($message);
}
/**
* @return Delegate
*/
private function chainedExecution()
{
$middlewares = [];
foreach ($this->middlewares as $priority => $collection) {
foreach ($collection as $middleware) {
$middlewares[] = $middleware;
}
}
$next = Delegate::fromClosure(function ($message) {
// the final middleware return the same message
return $message;
});
// reverse iteration over middlewares
/** @var MiddlewareInterface $middleware */
while ($middleware = array_pop($middlewares)) {
$next = Delegate::fromClosure(function ($message) use ($middleware, $next) {
return $middleware->handle($message, $next);
});
}
return $next;
}
}