File tree Expand file tree Collapse file tree 7 files changed +51
-5
lines changed
SecurityBundle/Tests/Functional/app Expand file tree Collapse file tree 7 files changed +51
-5
lines changed Original file line number Diff line number Diff line change @@ -124,10 +124,14 @@ protected static function createKernel(array $options = [])
124
124
protected static function ensureKernelShutdown ()
125
125
{
126
126
if (null !== static ::$ kernel ) {
127
- $ container = static ::$ kernel ->getContainer ();
128
- static ::$ kernel ->shutdown ();
129
- if ($ container instanceof ResetInterface) {
130
- $ container ->reset ();
127
+ $ isBooted = (new \ReflectionClass (static ::$ kernel ))->getProperty ('booted ' );
128
+ $ isBooted ->setAccessible (true );
129
+ if ($ isBooted ->getValue (static ::$ kernel )) {
130
+ $ container = static ::$ kernel ->getContainer ();
131
+ static ::$ kernel ->shutdown ();
132
+ if ($ container instanceof ResetInterface) {
133
+ $ container ->reset ();
134
+ }
131
135
}
132
136
}
133
137
static ::$ container = null ;
Original file line number Diff line number Diff line change @@ -96,4 +96,13 @@ protected function getKernelParameters()
96
96
97
97
return $ parameters ;
98
98
}
99
+
100
+ public function getContainer ()
101
+ {
102
+ if (false === $ this ->booted ) {
103
+ throw new \LogicException ('Cannot access the container on a non-booted kernel. Did you forget to boot it? ' );
104
+ }
105
+
106
+ return parent ::getContainer ();
107
+ }
99
108
}
Original file line number Diff line number Diff line change @@ -98,4 +98,12 @@ protected function getKernelParameters()
98
98
99
99
return $ parameters ;
100
100
}
101
+
102
+ public function getContainer ()
103
+ {
104
+ if (false === $ this ->booted ) {
105
+ throw new \LogicException ('Cannot access the container on a non-booted kernel. Did you forget to boot it? ' );
106
+ }
107
+ return parent ::getContainer ();
108
+ }
101
109
}
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ CHANGELOG
25
25
* renamed ` PostResponseEvent ` to ` TerminateEvent `
26
26
* added ` HttpClientKernel ` for handling requests with an ` HttpClientInterface ` instance
27
27
* added ` trace_header ` and ` trace_level ` configuration options to ` HttpCache `
28
+ * Getting the container from a non-booted kernel is deprecated.
28
29
29
30
4.2.0
30
31
-----
Original file line number Diff line number Diff line change @@ -361,6 +361,10 @@ public function getProjectDir()
361
361
*/
362
362
public function getContainer ()
363
363
{
364
+ if (null === $ this ->container ) {
365
+ @trigger_error ('Getting the container from a non-booted kernel is deprecated since Symfony 4.3 and will throw an exceptions in Symfony 5.0. ' , E_USER_DEPRECATED );
366
+ }
367
+
364
368
return $ this ->container ;
365
369
}
366
370
Original file line number Diff line number Diff line change @@ -133,7 +133,7 @@ public function getRootDir();
133
133
/**
134
134
* Gets the current container.
135
135
*
136
- * @return ContainerInterface|null A ContainerInterface instance or null when the Kernel is shutdown
136
+ * @return ContainerInterface A ContainerInterface instance or null when the Kernel is shutdown
137
137
*/
138
138
public function getContainer ();
139
139
Original file line number Diff line number Diff line change @@ -36,6 +36,10 @@ public static function tearDownAfterClass()
36
36
$ fs ->remove (__DIR__ .'/Fixtures/var ' );
37
37
}
38
38
39
+ /**
40
+ * @group legacy
41
+ * @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.3 and will throw an exceptions in Symfony 5.0.
42
+ */
39
43
public function testConstructor ()
40
44
{
41
45
$ env = 'test_env ' ;
@@ -49,6 +53,10 @@ public function testConstructor()
49
53
$ this ->assertNull ($ kernel ->getContainer ());
50
54
}
51
55
56
+ /**
57
+ * @group legacy
58
+ * @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.3 and will throw an exceptions in Symfony 5.0.
59
+ */
52
60
public function testClone ()
53
61
{
54
62
$ env = 'test_env ' ;
@@ -486,6 +494,18 @@ public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
486
494
$ kernel ->terminate (Request::create ('/ ' ), new Response ());
487
495
}
488
496
497
+ /**
498
+ * @group legacy
499
+ * @expectedDeprecation Getting the container from a non-booted kernel is deprecated since Symfony 4.3 and will throw an exceptions in Symfony 5.0.
500
+ */
501
+ public function testDeprecatedNullKernel ()
502
+ {
503
+ $ kernel = $ this ->getKernel ();
504
+ $ kernel ->shutdown ();
505
+
506
+ $ this ->assertNull ($ kernel ->getContainer ());
507
+ }
508
+
489
509
public function testTerminateDelegatesTerminationOnlyForTerminableInterface ()
490
510
{
491
511
// does not implement TerminableInterface
You can’t perform that action at this time.
0 commit comments