Skip to content

Commit 02c3474

Browse files
author
Cosmologist
committed
Inject service container to ContainerStatic via SymfonyCommonBundle::boot instead kernel
1 parent 8faef8a commit 02c3474

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

DependencyInjection/ContainerStatic.php

+22-14
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,30 @@
1313
class ContainerStatic
1414
{
1515
/**
16-
* Gets the current container.
16+
* Instance of container.
1717
*
18-
* @return ContainerInterface A ContainerInterface instance
18+
* @var null|ContainerInterface
1919
*/
20-
static function getContainer(): ContainerInterface
21-
{
22-
global $kernel;
20+
protected static $container = null;
2321

24-
if ($kernel instanceof KernelInterface) {
25-
return $kernel->getContainer();
26-
}
27-
if ($kernel instanceof AppCache) {
28-
return $kernel->getKernel()->getContainer();
29-
}
22+
/**
23+
* Set the service container.
24+
*
25+
* @param ContainerInterface The service container
26+
*/
27+
static function setContainer(ContainerInterface $container)
28+
{
29+
self::$container = $container;
30+
}
3031

31-
throw new RuntimeException('Unsupported kernel (supports KernelInterface of AppCache): ' . get_class($kernel));
32+
/**
33+
* Returns the service container.
34+
*
35+
* @return ContainerInterface|null The service container
36+
*/
37+
static function getContainer(): ?ContainerInterface
38+
{
39+
return self::$container;
3240
}
3341

3442
/**
@@ -46,7 +54,7 @@ static function getContainer(): ContainerInterface
4654
*/
4755
static function get(string $id)
4856
{
49-
return self::getContainer()->get($id);
57+
return self::$container->get($id);
5058
}
5159

5260
/**
@@ -60,6 +68,6 @@ static function get(string $id)
6068
*/
6169
static function getParameter(string $name)
6270
{
63-
return self::getContainer()->getParameter($name);
71+
return self::$container->getParameter($name);
6472
}
6573
}

SymfonyCommonBundle.php

+9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
namespace Cosmologist\Bundle\SymfonyCommonBundle;
44

5+
use Cosmologist\Bundle\SymfonyCommonBundle\DependencyInjection\ContainerStatic;
56
use Symfony\Component\HttpKernel\Bundle\Bundle;
67

78
class SymfonyCommonBundle extends Bundle
89
{
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function boot()
14+
{
15+
ContainerStatic::setContainer($this->container);
16+
}
17+
918
}

0 commit comments

Comments
 (0)