|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Foundation; |
|
6
|
|
|
|
|
7
|
|
|
use FondBot\Drivers\Driver; |
|
8
|
|
|
use FondBot\Channels\Channel; |
|
9
|
|
|
use FondBot\Conversation\Context; |
|
10
|
|
|
use FondBot\Conversation\Session; |
|
11
|
|
|
use FondBot\Drivers\DriverManager; |
|
12
|
|
|
use FondBot\Conversation\ContextManager; |
|
13
|
|
|
use FondBot\Conversation\SessionManager; |
|
14
|
|
|
use Illuminate\Contracts\Container\Container; |
|
15
|
|
|
|
|
16
|
|
|
class Kernel |
|
17
|
|
|
{ |
|
18
|
|
|
public const VERSION = '2.0'; |
|
19
|
|
|
|
|
20
|
|
|
private $container; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Channel */ |
|
23
|
|
|
private $channel; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Driver */ |
|
26
|
|
|
private $driver; |
|
27
|
|
|
|
|
28
|
|
|
private $session; |
|
29
|
|
|
private $context; |
|
30
|
|
|
|
|
31
|
2 |
|
public function __construct(Container $container) |
|
32
|
|
|
{ |
|
33
|
2 |
|
$this->container = $container; |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Initialize kernel. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Channel $channel |
|
40
|
|
|
*/ |
|
41
|
|
|
public function initialize(Channel $channel): void |
|
42
|
|
|
{ |
|
43
|
|
|
// Set channel |
|
44
|
|
|
$this->channel = $channel; |
|
45
|
|
|
|
|
46
|
|
|
// Resolve channel driver and initialize it |
|
47
|
|
|
$this->driver = $this->driverManager()->driver($channel->getDriver()); |
|
48
|
|
|
$this->driver->initialize($channel->getParameters()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Perform shutdown tasks. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function terminate(): void |
|
55
|
|
|
{ |
|
56
|
|
|
// Save session if exists |
|
57
|
|
|
if ($this->session !== null) { |
|
58
|
|
|
$this->sessionManager()->save($this->session); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Save context if exists |
|
62
|
|
|
if ($this->context !== null) { |
|
63
|
|
|
$this->contextManager()->save($this->context); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get current channel. |
|
69
|
|
|
* |
|
70
|
|
|
* @return Channel|null |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getChannel(): ?Channel |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->channel; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get current driver. |
|
79
|
|
|
* |
|
80
|
|
|
* @return Driver|null |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getDriver(): ?Driver |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->driver; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get session. |
|
89
|
|
|
* |
|
90
|
|
|
* @return Session|null |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getSession(): ?Session |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->session; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set session. |
|
99
|
|
|
* |
|
100
|
|
|
* @param Session $session |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function setSession(Session $session): void |
|
103
|
|
|
{ |
|
104
|
1 |
|
$this->session = $session; |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Close session. |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function closeSession(): void |
|
111
|
|
|
{ |
|
112
|
1 |
|
if ($this->session !== null) { |
|
113
|
1 |
|
$this->sessionManager()->close($this->session); |
|
114
|
1 |
|
$this->session = null; |
|
115
|
|
|
} |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get context. |
|
120
|
|
|
* |
|
121
|
|
|
* @return Context|null |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function getContext(): ?Context |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->context; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set context. |
|
130
|
|
|
* |
|
131
|
|
|
* @param Context $context |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public function setContext(Context $context): void |
|
134
|
|
|
{ |
|
135
|
1 |
|
$this->context = $context; |
|
136
|
1 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Clear context. |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function clearContext(): void |
|
142
|
|
|
{ |
|
143
|
1 |
|
if ($this->context !== null) { |
|
144
|
1 |
|
$this->contextManager()->clear($this->context); |
|
145
|
1 |
|
$this->context = null; |
|
146
|
|
|
} |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get session manager. |
|
151
|
|
|
* |
|
152
|
|
|
* @return SessionManager |
|
153
|
|
|
*/ |
|
154
|
1 |
|
private function sessionManager(): SessionManager |
|
155
|
|
|
{ |
|
156
|
1 |
|
return $this->container->make(SessionManager::class); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get context manager. |
|
161
|
|
|
* |
|
162
|
|
|
* @return ContextManager |
|
163
|
|
|
*/ |
|
164
|
1 |
|
private function contextManager(): ContextManager |
|
165
|
|
|
{ |
|
166
|
1 |
|
return $this->container->make(ContextManager::class); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get driver manager. |
|
171
|
|
|
* |
|
172
|
|
|
* @return DriverManager |
|
173
|
|
|
*/ |
|
174
|
|
|
private function driverManager(): DriverManager |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->container->make(DriverManager::class); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|