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