1 | <?php |
||
15 | class Kernel |
||
16 | { |
||
17 | public const VERSION = '2.0.0'; |
||
18 | |||
19 | /** @var Kernel */ |
||
20 | private static $instance; |
||
21 | |||
22 | private $container; |
||
23 | private $terminable; |
||
24 | |||
25 | private $driver; |
||
26 | private $channel; |
||
27 | private $session; |
||
28 | private $context; |
||
29 | |||
30 | 170 | private function __construct(Container $container, bool $terminable = true) |
|
35 | |||
36 | 5 | public function __destruct() |
|
40 | |||
41 | 34 | public static function getInstance(): Kernel |
|
45 | |||
46 | 170 | public static function createInstance(Container $container, bool $terminable = true): Kernel |
|
50 | |||
51 | /** |
||
52 | * Boot kernel. |
||
53 | * |
||
54 | * @param Channel $channel |
||
55 | * @param Driver $driver |
||
56 | */ |
||
57 | 1 | public function boot(Channel $channel, Driver $driver): void |
|
62 | |||
63 | /** |
||
64 | * Perform shutdown tasks. |
||
65 | */ |
||
66 | 7 | public function terminate(): void |
|
67 | { |
||
68 | 7 | if (!$this->terminable) { |
|
69 | 1 | return; |
|
70 | } |
||
71 | |||
72 | // Save session if exists |
||
73 | 6 | if ($this->session !== null) { |
|
74 | 2 | $this->sessionManager()->save($this->session); |
|
75 | } |
||
76 | |||
77 | // Save context if exists |
||
78 | 6 | if ($this->context !== null) { |
|
79 | 2 | $this->contextManager()->save($this->context); |
|
80 | } |
||
81 | 6 | } |
|
82 | |||
83 | /** |
||
84 | * Get current channel. |
||
85 | * |
||
86 | * @return Channel|null |
||
87 | */ |
||
88 | 7 | public function getChannel(): ?Channel |
|
92 | |||
93 | /** |
||
94 | * Set channel. |
||
95 | * |
||
96 | * @param Channel $channel |
||
97 | */ |
||
98 | 7 | public function setChannel(Channel $channel): void |
|
102 | |||
103 | /** |
||
104 | * Get current driver. |
||
105 | * |
||
106 | * @return Driver|null |
||
107 | */ |
||
108 | 7 | public function getDriver(): ?Driver |
|
112 | |||
113 | /** |
||
114 | * Set driver. |
||
115 | * |
||
116 | * @param Driver $driver |
||
117 | */ |
||
118 | 7 | public function setDriver(Driver $driver): void |
|
122 | |||
123 | /** |
||
124 | * Get session. |
||
125 | * |
||
126 | * @return Session|null |
||
127 | */ |
||
128 | 14 | public function getSession(): ?Session |
|
132 | |||
133 | /** |
||
134 | * Set session. |
||
135 | * |
||
136 | * @param Session $session |
||
137 | */ |
||
138 | 14 | public function setSession(Session $session): void |
|
142 | |||
143 | /** |
||
144 | * Close session. |
||
145 | * |
||
146 | * @throws \Psr\Container\ContainerExceptionInterface |
||
147 | */ |
||
148 | 3 | public function closeSession(): void |
|
155 | |||
156 | /** |
||
157 | * Get context. |
||
158 | * |
||
159 | * @return Context|null |
||
160 | */ |
||
161 | 3 | public function getContext(): ?Context |
|
165 | |||
166 | /** |
||
167 | * Set context. |
||
168 | * |
||
169 | * @param Context $context |
||
170 | */ |
||
171 | 6 | public function setContext(Context $context): void |
|
175 | |||
176 | /** |
||
177 | * Clear context. |
||
178 | */ |
||
179 | 3 | public function clearContext(): void |
|
180 | { |
||
181 | 3 | if ($this->context !== null) { |
|
182 | 1 | $this->contextManager()->clear($this->context); |
|
183 | 1 | $this->context = null; |
|
184 | } |
||
185 | 3 | } |
|
186 | |||
187 | /** |
||
188 | * Resolve an alias from container. |
||
189 | * |
||
190 | * @param string $alias |
||
191 | * @param array $args |
||
192 | * |
||
193 | * @return mixed |
||
194 | * |
||
195 | * @throws \Psr\Container\ContainerExceptionInterface |
||
196 | */ |
||
197 | 38 | public function resolve(string $alias, array $args = []) |
|
201 | |||
202 | /** |
||
203 | * Get session manager. |
||
204 | * |
||
205 | * @return SessionManager |
||
206 | * |
||
207 | * @throws \Psr\Container\ContainerExceptionInterface |
||
208 | */ |
||
209 | 6 | private function sessionManager(): SessionManager |
|
213 | |||
214 | /** |
||
215 | * Get context manager. |
||
216 | * |
||
217 | * @return ContextManager |
||
218 | */ |
||
219 | 4 | private function contextManager(): ContextManager |
|
223 | } |
||
224 |
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
SomeClass
to useself
instead: