|
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 Illuminate\Contracts\Container\Container; |
|
14
|
|
|
use FondBot\Foundation\Commands\TerminateKernel; |
|
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 Session|null */ |
|
26
|
|
|
private $session; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Context|null */ |
|
29
|
|
|
private $context; |
|
30
|
|
|
|
|
31
|
2 |
|
public function __construct(Container $container, Dispatcher $bus) |
|
|
|
|
|
|
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
|
|
|
$this->channel = $channel; |
|
44
|
|
|
|
|
45
|
|
|
TerminateKernel::dispatch(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get current channel. |
|
50
|
|
|
* |
|
51
|
|
|
* @return Channel|null |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getChannel(): ?Channel |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->channel; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get session. |
|
60
|
|
|
* |
|
61
|
|
|
* @return Session|null |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function getSession(): ?Session |
|
64
|
|
|
{ |
|
65
|
1 |
|
return $this->session; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set session. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Session $session |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function setSession(Session $session): void |
|
74
|
|
|
{ |
|
75
|
1 |
|
$this->session = $session; |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Close session. |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function closeSession(): void |
|
82
|
|
|
{ |
|
83
|
1 |
|
if ($this->session !== null) { |
|
84
|
1 |
|
$this->sessionManager()->close($this->session); |
|
85
|
1 |
|
$this->session = null; |
|
86
|
|
|
} |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get context. |
|
91
|
|
|
* |
|
92
|
|
|
* @return Context|null |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getContext(): ?Context |
|
95
|
|
|
{ |
|
96
|
1 |
|
return $this->context; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set context. |
|
101
|
|
|
* |
|
102
|
|
|
* @param Context $context |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function setContext(Context $context): void |
|
105
|
|
|
{ |
|
106
|
1 |
|
$this->context = $context; |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Clear context. |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function clearContext(): void |
|
113
|
|
|
{ |
|
114
|
1 |
|
if ($this->context !== null) { |
|
115
|
1 |
|
$this->contextManager()->clear($this->context); |
|
116
|
1 |
|
$this->context = null; |
|
117
|
|
|
} |
|
118
|
1 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get session manager. |
|
122
|
|
|
* |
|
123
|
|
|
* @return SessionManager |
|
124
|
|
|
*/ |
|
125
|
1 |
|
private function sessionManager(): SessionManager |
|
126
|
|
|
{ |
|
127
|
1 |
|
return $this->container->make(SessionManager::class); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get context manager. |
|
132
|
|
|
* |
|
133
|
|
|
* @return ContextManager |
|
134
|
|
|
*/ |
|
135
|
1 |
|
private function contextManager(): ContextManager |
|
136
|
|
|
{ |
|
137
|
1 |
|
return $this->container->make(ContextManager::class); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.