Skip to content

Commit b8c56e6

Browse files
committed
Reboot & shutdown.
1 parent 7d5c1d9 commit b8c56e6

File tree

3 files changed

+111
-6
lines changed

3 files changed

+111
-6
lines changed

src/ServiceProvider.php

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Symfony\Component\Filesystem\Filesystem;
3838
use Symfony\Component\HttpKernel\Config\FileLocator;
3939
use Symfony\Component\HttpKernel\KernelEvents;
40+
use Symfony\Component\HttpKernel\KernelInterface;
4041
use Throwable;
4142

4243
/**
@@ -215,11 +216,7 @@ public function __construct(
215216

216217
$this->projectRoot = Application::getDocumentRoot();
217218

218-
$result = $this->initContainer($filename);
219-
if (!$result) {
220-
$this->errorHandler->die('Container DI inititalization error.');
221-
throw new Exception('Container DI inititalization error.');
222-
}
219+
$this->boot();
223220
}
224221

225222
/**
@@ -257,6 +254,58 @@ public function setContainer(PsrContainerInterface $container): void
257254
static::$containerBuilder = $container;
258255
}
259256

257+
/**
258+
* Reboot.
259+
*
260+
* @return void
261+
* @throws Exception
262+
*/
263+
public function reboot() : void
264+
{
265+
$this->shutdown();
266+
$this->boot();
267+
}
268+
269+
/**
270+
* Shutdown.
271+
*
272+
* @return void
273+
* @throws Exception
274+
*/
275+
public function shutdown() : void
276+
{
277+
if (static::$containerBuilder === null) {
278+
return;
279+
}
280+
281+
if (static::$containerBuilder->has('kernel')) {
282+
/** @var AppKernel $kernel */
283+
$kernel = static::$containerBuilder->get('kernel');
284+
$kernel->setContainer(null);
285+
}
286+
287+
foreach (BundlesLoader::getBundlesMap() as $bundle) {
288+
$bundle->shutdown();
289+
$bundle->setContainer(null);
290+
}
291+
292+
static::$containerBuilder = null;
293+
}
294+
295+
/**
296+
* Boot.
297+
*
298+
* @throws Exception
299+
*/
300+
private function boot() : void
301+
{
302+
$result = $this->initContainer($this->filename);
303+
if (!$result) {
304+
$this->errorHandler->die('Container DI inititalization error.');
305+
throw new Exception('Container DI inititalization error.');
306+
}
307+
}
308+
260309
/**
261310
* Инициализировать контейнер.
262311
*

src/Services/AppKernel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function getBundlesMetaData() : array
157157
*
158158
* @since 12.12.2020
159159
*/
160-
public function setContainer(ContainerInterface $container = null) : void
160+
public function setContainer(?ContainerInterface $container = null) : void
161161
{
162162
$this->container = $container;
163163
}
@@ -213,6 +213,7 @@ public function registerBundles(): iterable
213213
return [];
214214
}
215215

216+
/* @noinspection PhpIncludeInspection */
216217
$contents = require $bundleConfigPath;
217218

218219
foreach ($contents as $class => $envs) {

tests/Cases/ServiceProviderTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Prokl\BitrixTestingTools\Base\BitrixableTestCase;
77
use Prokl\ServiceProvider\ServiceProvider;
88
use Prokl\ServiceProvider\Services\AppKernel;
9+
use ReflectionProperty;
910
use RuntimeException;
1011

1112
/**
@@ -124,6 +125,60 @@ public function dataProviderDebugEnv() : array
124125
];
125126
}
126127

128+
/**
129+
*
130+
* shutdown().
131+
*
132+
* @return void
133+
* @throws Exception
134+
*
135+
* @runInSeparateProcess
136+
* @preserveGlobalState disabled
137+
*/
138+
public function testShutdown(): void
139+
{
140+
$this->obTestObject = new ServiceProvider($this->pathYamlConfig);
141+
142+
/** @var AppKernel $kernel */
143+
$kernel = $this->obTestObject->get('kernel');
144+
145+
$this->obTestObject->shutdown();
146+
147+
$reflection = new ReflectionProperty(ServiceProvider::class, 'containerBuilder');
148+
$reflection->setAccessible(true);
149+
$value = $reflection->getValue(null);
150+
151+
$this->assertNull($value, 'Контейнер не обнулился');
152+
$this->assertNull($kernel->getContainer(), 'Контейнер в kernel не обнулился');
153+
}
154+
155+
/**
156+
*
157+
* reboot().
158+
*
159+
* @return void
160+
* @throws Exception
161+
*
162+
* @runInSeparateProcess
163+
* @preserveGlobalState disabled
164+
*/
165+
public function testReboot(): void
166+
{
167+
$this->obTestObject = new ServiceProvider($this->pathYamlConfig);
168+
169+
$this->obTestObject->reboot();
170+
171+
/** @var AppKernel $kernel */
172+
$kernel = $this->obTestObject->get('kernel');
173+
174+
$reflection = new ReflectionProperty(ServiceProvider::class, 'containerBuilder');
175+
$reflection->setAccessible(true);
176+
$value = $reflection->getValue(null);
177+
178+
$this->assertNotNull($value, 'Контейнер обнулился');
179+
$this->assertNotNull($kernel->getContainer(), 'Контейнер в kernel обнулился');
180+
}
181+
127182
/**
128183
* @return void
129184
* @throws Exception

0 commit comments

Comments
 (0)