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