13
13
class ContainerStatic
14
14
{
15
15
/**
16
- * Gets the current container.
16
+ * Instance of container.
17
17
*
18
- * @return ContainerInterface A ContainerInterface instance
18
+ * @var null| ContainerInterface
19
19
*/
20
- static function getContainer (): ContainerInterface
21
- {
22
- global $ kernel ;
20
+ protected static $ container = null ;
23
21
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
+ }
30
31
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 ;
32
40
}
33
41
34
42
/**
@@ -46,7 +54,7 @@ static function getContainer(): ContainerInterface
46
54
*/
47
55
static function get (string $ id )
48
56
{
49
- return self ::getContainer () ->get ($ id );
57
+ return self ::$ container ->get ($ id );
50
58
}
51
59
52
60
/**
@@ -60,6 +68,6 @@ static function get(string $id)
60
68
*/
61
69
static function getParameter (string $ name )
62
70
{
63
- return self ::getContainer () ->getParameter ($ name );
71
+ return self ::$ container ->getParameter ($ name );
64
72
}
65
73
}
0 commit comments