Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4f197a5

Browse filesBrowse files
feature #27462 [FrameworkBundle] Deprecate auto-injection of the container in AbstractController instances (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [FrameworkBundle] Deprecate auto-injection of the container in AbstractController instances | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Should enhance DX by preventing situations like #27436. Commits ------- e2f344f [FrameworkBundle] Deprecate auto-injection of the container in AbstractController instances
2 parents fa022f0 + e2f344f commit 4f197a5
Copy full SHA for 4f197a5

File tree

3 files changed

+19
-4
lines changed
Filter options

3 files changed

+19
-4
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Allowed configuring taggable cache pools via a new `framework.cache.pools.tags` option (bool|service-id)
8+
* Deprecated auto-injection of the container in AbstractController instances, register them as service subscribers instead
89

910
4.1.0
1011
-----

‎src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php
+10-4Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,22 @@ protected function createController($controller)
5151
*/
5252
protected function instantiateController($class)
5353
{
54-
return $this->configureController(parent::instantiateController($class));
54+
return $this->configureController(parent::instantiateController($class), $class);
5555
}
5656

57-
private function configureController($controller)
57+
private function configureController($controller, string $class)
5858
{
5959
if ($controller instanceof ContainerAwareInterface) {
6060
$controller->setContainer($this->container);
6161
}
62-
if ($controller instanceof AbstractController && null !== $previousContainer = $controller->setContainer($this->container)) {
63-
$controller->setContainer($previousContainer);
62+
if ($controller instanceof AbstractController) {
63+
if (null === $previousContainer = $controller->setContainer($this->container)) {
64+
@trigger_error(sprintf('Auto-injection of the container for "%s" is deprecated since Symfony 4.2. Configure it as a service instead.', $class), E_USER_DEPRECATED);
65+
// To be uncommented on Symfony 5:
66+
//throw new \LogicException(sprintf('"%s" has no container set, did you forget to define it as a service subscriber?', $class));
67+
} else {
68+
$controller->setContainer($previousContainer);
69+
}
6470
}
6571

6672
return $controller;

‎src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class_exists(AbstractControllerTest::class);
9292
$this->assertSame($container, $controller->getContainer());
9393
}
9494

95+
/**
96+
* @group legacy
97+
* @expectedDeprecation Auto-injection of the container for "Symfony\Bundle\FrameworkBundle\Tests\Controller\TestAbstractController" is deprecated since Symfony 4.2. Configure it as a service instead.
98+
*/
9599
public function testAbstractControllerGetsContainerWhenNotSet()
96100
{
97101
class_exists(AbstractControllerTest::class);
@@ -110,6 +114,10 @@ class_exists(AbstractControllerTest::class);
110114
$this->assertSame($container, $controller->setContainer($container));
111115
}
112116

117+
/**
118+
* @group legacy
119+
* @expectedDeprecation Auto-injection of the container for "Symfony\Bundle\FrameworkBundle\Tests\Controller\DummyController" is deprecated since Symfony 4.2. Configure it as a service instead.
120+
*/
113121
public function testAbstractControllerServiceWithFcqnIdGetsContainerWhenNotSet()
114122
{
115123
class_exists(AbstractControllerTest::class);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.