|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Foundation; |
|
6
|
|
|
|
|
7
|
|
|
use FondBot\Drivers\Driver; |
|
8
|
|
|
use FondBot\Drivers\Command; |
|
9
|
|
|
use FondBot\Channels\Channel; |
|
10
|
|
|
use FondBot\Conversation\Context; |
|
11
|
|
|
use FondBot\Conversation\Session; |
|
12
|
|
|
use FondBot\Conversation\ContextManager; |
|
13
|
|
|
use FondBot\Conversation\SessionManager; |
|
14
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
|
15
|
|
|
use Illuminate\Contracts\Container\Container; |
|
16
|
|
|
|
|
17
|
|
|
class Kernel |
|
18
|
|
|
{ |
|
19
|
|
|
public const VERSION = '2.0'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Kernel */ |
|
22
|
|
|
private static $instance; |
|
23
|
|
|
|
|
24
|
|
|
private $container; |
|
25
|
|
|
private $terminable; |
|
26
|
|
|
|
|
27
|
|
|
private $driver; |
|
28
|
|
|
private $channel; |
|
29
|
|
|
private $session; |
|
30
|
|
|
private $context; |
|
31
|
|
|
|
|
32
|
117 |
|
private function __construct(Container $container, bool $terminable = true) |
|
33
|
|
|
{ |
|
34
|
117 |
|
$this->container = $container; |
|
35
|
117 |
|
$this->terminable = $terminable; |
|
36
|
117 |
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
public function __destruct() |
|
39
|
|
|
{ |
|
40
|
4 |
|
$this->terminate(); |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
22 |
|
public static function getInstance(): Kernel |
|
44
|
|
|
{ |
|
45
|
22 |
|
return static::$instance; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
117 |
|
public static function createInstance(Container $container, bool $terminable = true): Kernel |
|
49
|
|
|
{ |
|
50
|
117 |
|
return static::$instance = new static($container, $terminable); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Perform shutdown tasks. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function terminate(): void |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$this->terminable) { |
|
59
|
1 |
|
return; |
|
60
|
|
|
} |
|
61
|
1 |
|
|
|
62
|
1 |
|
// Save session if exists |
|
63
|
1 |
|
if ($this->session !== null) { |
|
64
|
|
|
$this->sessionManager()->save($this->session); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Save context if exists |
|
68
|
6 |
|
if ($this->context !== null) { |
|
69
|
|
|
$this->contextManager()->save($this->context); |
|
70
|
6 |
|
} |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get current channel. |
|
75
|
5 |
|
* |
|
76
|
2 |
|
* @return Channel|null |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getChannel(): ?Channel |
|
79
|
|
|
{ |
|
80
|
5 |
|
return $this->channel; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
5 |
|
/** |
|
84
|
|
|
* Set channel. |
|
85
|
|
|
* |
|
86
|
|
|
* @param Channel $channel |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setChannel(Channel $channel): void |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->channel = $channel; |
|
91
|
|
|
} |
|
92
|
1 |
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get current driver. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Driver|null |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getDriver(): ?Driver |
|
99
|
|
|
{ |
|
100
|
7 |
|
return $this->driver; |
|
101
|
|
|
} |
|
102
|
7 |
|
|
|
103
|
7 |
|
/** |
|
104
|
|
|
* Set driver. |
|
105
|
|
|
* |
|
106
|
|
|
* @param Driver $driver |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setDriver(Driver $driver): void |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->driver = $driver; |
|
111
|
|
|
} |
|
112
|
1 |
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get session. |
|
115
|
|
|
* |
|
116
|
|
|
* @return Session|null |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getSession(): ?Session |
|
119
|
|
|
{ |
|
120
|
7 |
|
return $this->session; |
|
121
|
|
|
} |
|
122
|
7 |
|
|
|
123
|
7 |
|
/** |
|
124
|
|
|
* Set session. |
|
125
|
|
|
* |
|
126
|
|
|
* @param Session $session |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setSession(Session $session): void |
|
129
|
|
|
{ |
|
130
|
14 |
|
$this->session = $session; |
|
131
|
|
|
} |
|
132
|
14 |
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Close session. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function closeSession(): void |
|
137
|
|
|
{ |
|
138
|
|
|
if ($this->session !== null) { |
|
139
|
|
|
$this->sessionManager()->close($this->session); |
|
140
|
14 |
|
$this->session = null; |
|
141
|
|
|
} |
|
142
|
14 |
|
} |
|
143
|
14 |
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get context. |
|
146
|
|
|
* |
|
147
|
|
|
* @return Context|null |
|
148
|
3 |
|
*/ |
|
149
|
|
|
public function getContext(): ?Context |
|
150
|
3 |
|
{ |
|
151
|
3 |
|
return $this->context; |
|
152
|
3 |
|
} |
|
153
|
|
|
|
|
154
|
3 |
|
/** |
|
155
|
|
|
* Set context. |
|
156
|
|
|
* |
|
157
|
|
|
* @param Context $context |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setContext(Context $context): void |
|
160
|
|
|
{ |
|
161
|
3 |
|
$this->context = $context; |
|
162
|
|
|
} |
|
163
|
3 |
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Clear context. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function clearContext(): void |
|
168
|
|
|
{ |
|
169
|
|
|
if ($this->context !== null) { |
|
170
|
|
|
$this->contextManager()->clear($this->context); |
|
171
|
6 |
|
$this->context = null; |
|
172
|
|
|
} |
|
173
|
6 |
|
} |
|
174
|
6 |
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Resolve an alias from container. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $alias |
|
179
|
3 |
|
* |
|
180
|
|
|
* @return mixed |
|
181
|
3 |
|
*/ |
|
182
|
1 |
|
public function resolve(string $alias) |
|
183
|
1 |
|
{ |
|
184
|
|
|
return $this->container->make($alias); |
|
185
|
3 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Dispatch command to driver. |
|
189
|
|
|
* |
|
190
|
|
|
* @param Command $command |
|
191
|
|
|
*/ |
|
192
|
|
|
public function dispatch(Command $command): void |
|
193
|
|
|
{ |
|
194
|
18 |
|
$dispatcher = $this->resolve(Dispatcher::class); |
|
195
|
|
|
|
|
196
|
18 |
|
$dispatcher->dispatch($command); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Get session manager. |
|
201
|
|
|
* |
|
202
|
|
|
* @return SessionManager |
|
203
|
|
|
*/ |
|
204
|
6 |
|
private function sessionManager(): SessionManager |
|
205
|
|
|
{ |
|
206
|
6 |
|
return $this->resolve(SessionManager::class); |
|
207
|
|
|
} |
|
208
|
6 |
|
|
|
209
|
6 |
|
/** |
|
210
|
|
|
* Get context manager. |
|
211
|
|
|
* |
|
212
|
|
|
* @return ContextManager |
|
213
|
|
|
*/ |
|
214
|
|
|
private function contextManager(): ContextManager |
|
215
|
|
|
{ |
|
216
|
6 |
|
return $this->resolve(ContextManager::class); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: