|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Application; |
|
6
|
|
|
|
|
7
|
|
|
use FondBot\Drivers\Driver; |
|
8
|
|
|
use FondBot\Channels\Channel; |
|
9
|
|
|
use League\Container\Container; |
|
10
|
|
|
use FondBot\Conversation\Session; |
|
11
|
|
|
use FondBot\Conversation\SessionManager; |
|
12
|
|
|
|
|
13
|
|
|
class Kernel |
|
14
|
|
|
{ |
|
15
|
|
|
public const VERSION = '1.0.0'; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Kernel */ |
|
18
|
|
|
private static $instance; |
|
19
|
|
|
|
|
20
|
|
|
private $container; |
|
21
|
|
|
|
|
22
|
|
|
private $driver; |
|
23
|
|
|
private $channel; |
|
24
|
|
|
private $session; |
|
25
|
|
|
|
|
26
|
144 |
|
private function __construct(Container $container) |
|
27
|
|
|
{ |
|
28
|
144 |
|
$this->container = $container; |
|
29
|
144 |
|
} |
|
30
|
|
|
|
|
31
|
32 |
|
public static function getInstance(): Kernel |
|
32
|
|
|
{ |
|
33
|
32 |
|
return static::$instance; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
144 |
|
public static function createInstance(Container $container): Kernel |
|
37
|
|
|
{ |
|
38
|
144 |
|
return static::$instance = new static($container); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get current channel. |
|
43
|
|
|
* |
|
44
|
|
|
* @return Channel|null |
|
45
|
|
|
*/ |
|
46
|
7 |
|
public function getChannel(): ?Channel |
|
47
|
|
|
{ |
|
48
|
7 |
|
return $this->channel; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set channel. |
|
53
|
|
|
* |
|
54
|
|
|
* @param Channel $channel |
|
55
|
|
|
*/ |
|
56
|
11 |
|
public function setChannel(Channel $channel): void |
|
57
|
|
|
{ |
|
58
|
11 |
|
$this->channel = $channel; |
|
59
|
11 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get current driver. |
|
63
|
|
|
* |
|
64
|
|
|
* @return Driver|null |
|
65
|
|
|
*/ |
|
66
|
8 |
|
public function getDriver(): ?Driver |
|
67
|
|
|
{ |
|
68
|
8 |
|
return $this->driver; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set driver. |
|
73
|
|
|
* |
|
74
|
|
|
* @param Driver $driver |
|
75
|
|
|
*/ |
|
76
|
11 |
|
public function setDriver(Driver $driver): void |
|
77
|
|
|
{ |
|
78
|
11 |
|
$this->driver = $driver; |
|
79
|
11 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get session. |
|
83
|
|
|
* |
|
84
|
|
|
* @return Session|null |
|
85
|
|
|
*/ |
|
86
|
13 |
|
public function getSession(): ?Session |
|
87
|
|
|
{ |
|
88
|
13 |
|
return $this->session; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set session. |
|
93
|
|
|
* |
|
94
|
|
|
* @param Session $session |
|
95
|
|
|
*/ |
|
96
|
13 |
|
public function setSession(Session $session): void |
|
97
|
|
|
{ |
|
98
|
13 |
|
$this->session = $session; |
|
99
|
13 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Load session. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Channel $channel |
|
105
|
|
|
* @param Driver $driver |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function loadSession(Channel $channel, Driver $driver): void |
|
108
|
|
|
{ |
|
109
|
3 |
|
$this->session = $this->sessionManager()->load($channel->getName(), $driver); |
|
110
|
3 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Save session. |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function saveSession(): void |
|
116
|
|
|
{ |
|
117
|
1 |
|
if ($this->session !== null) { |
|
118
|
1 |
|
$this->sessionManager()->save($this->session); |
|
119
|
|
|
} |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Close session. |
|
124
|
|
|
* |
|
125
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public function closeSession(): void |
|
128
|
|
|
{ |
|
129
|
3 |
|
if ($this->session !== null) { |
|
130
|
3 |
|
$this->sessionManager()->close($this->session); |
|
131
|
3 |
|
$this->session = null; |
|
132
|
|
|
} |
|
133
|
3 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Resolve an alias from container. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $alias |
|
139
|
|
|
* @param array $args |
|
140
|
|
|
* |
|
141
|
|
|
* @return mixed |
|
142
|
|
|
* |
|
143
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
|
144
|
|
|
*/ |
|
145
|
32 |
|
public function resolve(string $alias, array $args = []) |
|
146
|
|
|
{ |
|
147
|
32 |
|
return $this->container->get($alias, $args); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get session manager. |
|
152
|
|
|
* |
|
153
|
|
|
* @return SessionManager |
|
154
|
|
|
* |
|
155
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
|
156
|
|
|
*/ |
|
157
|
5 |
|
private function sessionManager(): SessionManager |
|
158
|
|
|
{ |
|
159
|
5 |
|
return $this->resolve(SessionManager::class); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
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: