|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of GitterBot package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Serafim <[email protected]> |
|
6
|
|
|
* @date 27.01.2016 16:54 |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace App\Gitter\Extensions; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use App\Message; |
|
15
|
|
|
use Gitter\Models\Room as GitterRoom; |
|
16
|
|
|
use App\Gitter\Extensions\Middlewares\Container; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class MiddlewareBuilder |
|
20
|
|
|
* @package App\Gitter\Extensions |
|
21
|
|
|
*/ |
|
22
|
|
|
class MiddlewareBuilder |
|
23
|
|
|
{ |
|
24
|
|
|
const COMMON_GROUP = 'common'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $middlewares = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $rooms = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $resolvedMiddlewares = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var bool |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $built = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $middleware |
|
48
|
|
|
* @param string $group |
|
49
|
|
|
* @return $this |
|
50
|
|
|
*/ |
|
51
|
|
|
public function registerMiddleware(string $middleware, string $group = self::COMMON_GROUP) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!array_key_exists($group, $this->middlewares)) { |
|
54
|
|
|
$this->middlewares[$group] = []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->middlewares[$group][] = $middleware; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @TODO Improve container |
|
64
|
|
|
* @param GitterRoom $room |
|
65
|
|
|
* @param array $groups |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
|
public function registerRoom(GitterRoom $room, array $groups = []) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->rooms[] = (object) [ |
|
71
|
|
|
'room' => $room, |
|
72
|
|
|
'groups' => $groups |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $name |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getGroup($name) |
|
83
|
|
|
{ |
|
84
|
|
|
return array_key_exists($name, $this->middlewares) |
|
85
|
|
|
? $this->middlewares[$name] |
|
86
|
|
|
: []; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function build() |
|
93
|
|
|
{ |
|
94
|
|
|
if (!$this->built) { |
|
95
|
|
|
foreach ($this->rooms as $data) { |
|
96
|
|
|
/** @var Container $extension */ |
|
97
|
|
|
$extension = $this->getExtensionByRoom($data->room); |
|
98
|
|
|
|
|
99
|
|
|
foreach ($data->groups as $group) { |
|
100
|
|
|
$middlewares = $this->getGroup($group); |
|
101
|
|
|
|
|
102
|
|
|
foreach ($middlewares as $middleware) { |
|
103
|
|
|
$extension->register($middleware); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->resolvedMiddlewares[$data->room->id] = $extension; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->built = true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param GitterRoom $room |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getExtensionByRoom(GitterRoom $room) |
|
121
|
|
|
{ |
|
122
|
|
|
if (!array_key_exists($room->id, $this->resolvedMiddlewares)) { |
|
123
|
|
|
/** @var Container $extension */ |
|
124
|
|
|
$extension = app(Container::class, ['room' => $room]); |
|
125
|
|
|
foreach ($this->getGroup(static::COMMON_GROUP) as $middleware) { |
|
126
|
|
|
$extension->register($middleware); |
|
127
|
|
|
} |
|
128
|
|
|
$this->resolvedMiddlewares[$room->id] = $extension; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->resolvedMiddlewares[$room->id]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param GitterRoom $room |
|
136
|
|
|
* @param Message $message |
|
137
|
|
|
* @return $this |
|
138
|
|
|
*/ |
|
139
|
|
|
public function fire(GitterRoom $room, Message $message) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->build(); |
|
142
|
|
|
|
|
143
|
|
|
$extension = $this->getExtensionByRoom($room); |
|
144
|
|
|
$extension->fire($message); |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|