1
|
|
|
<?php |
2
|
|
|
namespace App\Gitter; |
3
|
|
|
|
4
|
|
|
use App\Message; |
5
|
|
|
use App\Room; |
6
|
|
|
use Gitter\Client; |
7
|
|
|
use Gitter\Models\Room as GitterRoom; |
8
|
|
|
use Illuminate\Contracts\Container\Container; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class MiddlewaresExtension |
12
|
|
|
* @package App\Gitter\Middleware |
13
|
|
|
*/ |
14
|
|
|
class MiddlewaresExtension implements ExtensionInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $items = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Container |
23
|
|
|
*/ |
24
|
|
|
protected $container; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var GitterRoom |
28
|
|
|
*/ |
29
|
|
|
protected $room; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Room |
33
|
|
|
*/ |
34
|
|
|
protected $roomObject; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Repository constructor. |
38
|
|
|
* @param GitterRoom $room |
39
|
|
|
* @param Container $container |
40
|
|
|
*/ |
41
|
|
|
public function __construct(GitterRoom $room, Container $container) |
42
|
|
|
{ |
43
|
|
|
$this->room = $room; |
44
|
|
|
$this->roomObject = Room::make($this->room); |
45
|
|
|
$this->container = $container; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $middleware |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function register(string $middleware) |
53
|
|
|
{ |
54
|
|
|
$this->items[] = $this->container->make($middleware, [ |
55
|
|
|
'room' => $this->roomObject, |
56
|
|
|
'gitter' => $this->room, |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Message $message |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function fire(Message $message) |
67
|
|
|
{ |
68
|
|
|
if (count($this->items)) { |
69
|
|
|
$response = new Response($this->fireMiddleware(0, $message)); |
70
|
|
|
|
71
|
|
|
if (!$response->isEmpty()) { |
72
|
|
|
$content = $response->getContent(); |
73
|
|
|
$this->room->sendMessage($content); |
74
|
|
|
|
75
|
|
|
if (app(Client::class)->isFallbackMode()) { |
76
|
|
|
$this->room->sendMessage( |
77
|
|
|
'_Gitter Stream API недоступен. Я работаю с некоторой задержкой, простите =(_' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $number |
86
|
|
|
* @param Message $message |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
protected function fireMiddleware(int $number, Message $message) |
90
|
|
|
{ |
91
|
|
|
if (!array_key_exists($number, $this->items)) { |
92
|
|
|
return new Response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$middleware = $this->items[$number]; |
96
|
|
|
|
97
|
|
|
return $this->container->call([$middleware, 'handle'], [ |
98
|
|
|
'message' => $message, |
99
|
|
|
'next' => function (Message $message) use ($number) { |
100
|
|
|
$result = $this->fireMiddleware(++$number, $message); |
101
|
|
|
|
102
|
|
|
if (!($result instanceof Response)) { |
103
|
|
|
return new Response($result); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $result; |
107
|
|
|
}, |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|