Skip to content

Commit 9b879dc

Browse files
[DependencyInjection] Deprecate ContainerAwareInterface, ContainerAwareTrait and ContainerAwareLoader
1 parent 045f3a4 commit 9b879dc

File tree

11 files changed

+44
-23
lines changed

11 files changed

+44
-23
lines changed

Console/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function get(string $name): Command
115115
$command = parent::get($name);
116116

117117
if ($command instanceof ContainerAwareInterface) {
118+
trigger_deprecation('symfony/dependency-injection', '6.4', 'Relying on "%s" to get the container in "%s" is deprecated, register the command as a service and use dependency injection instead.', ContainerAwareInterface::class, get_debug_type($command));
118119
$command->setContainer($this->kernel->getContainer());
119120
}
120121

Controller/ControllerResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protected function instantiateController(string $class): object
2626
$controller = parent::instantiateController($class);
2727

2828
if ($controller instanceof ContainerAwareInterface) {
29+
trigger_deprecation('symfony/dependency-injection', '6.4', 'Relying on "%s" to get the container in "%s" is deprecated, register the controller as a service and use dependency injection instead.', ContainerAwareInterface::class, get_debug_type($controller));
2930
$controller->setContainer($this->container);
3031
}
3132
if ($controller instanceof AbstractController) {

Tests/Controller/ControllerResolverTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerInterface as Psr11ContainerInterface;
1515
use Psr\Log\LoggerInterface;
16+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1617
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1718
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
1819
use Symfony\Component\DependencyInjection\Container;
@@ -23,31 +24,44 @@
2324

2425
class ControllerResolverTest extends ContainerControllerResolverTest
2526
{
27+
use ExpectDeprecationTrait;
28+
29+
/**
30+
* @group legacy
31+
*/
2632
public function testGetControllerOnContainerAware()
2733
{
2834
$resolver = $this->createControllerResolver();
2935
$request = Request::create('/');
3036
$request->attributes->set('_controller', 'Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController::testAction');
3137

38+
$this->expectDeprecation('Since symfony/dependency-injection 6.4: Relying on "Symfony\Component\DependencyInjection\ContainerAwareInterface" to get the container in "Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController" is deprecated, register the controller as a service and use dependency injection instead.');
3239
$controller = $resolver->getController($request);
3340

3441
$this->assertInstanceOf(ContainerAwareController::class, $controller[0]);
3542
$this->assertInstanceOf(ContainerInterface::class, $controller[0]->getContainer());
3643
$this->assertSame('testAction', $controller[1]);
3744
}
3845

46+
/**
47+
* @group legacy
48+
*/
3949
public function testGetControllerOnContainerAwareInvokable()
4050
{
4151
$resolver = $this->createControllerResolver();
4252
$request = Request::create('/');
4353
$request->attributes->set('_controller', 'Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController');
4454

55+
$this->expectDeprecation('Since symfony/dependency-injection 6.4: Relying on "Symfony\Component\DependencyInjection\ContainerAwareInterface" to get the container in "Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController" is deprecated, register the controller as a service and use dependency injection instead.');
4556
$controller = $resolver->getController($request);
4657

4758
$this->assertInstanceOf(ContainerAwareController::class, $controller);
4859
$this->assertInstanceOf(ContainerInterface::class, $controller->getContainer());
4960
}
5061

62+
/**
63+
* @group legacy
64+
*/
5165
public function testContainerAwareControllerGetsContainerWhenNotSet()
5266
{
5367
class_exists(AbstractControllerTest::class);
@@ -62,6 +76,7 @@ class_exists(AbstractControllerTest::class);
6276
$request = Request::create('/');
6377
$request->attributes->set('_controller', TestAbstractController::class.'::testAction');
6478

79+
$this->expectDeprecation('Since symfony/dependency-injection 6.4: Relying on "Symfony\Component\DependencyInjection\ContainerAwareInterface" to get the container in "Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController" is deprecated, register the controller as a service and use dependency injection instead.');
6580
$this->assertSame([$controller, 'testAction'], $resolver->getController($request));
6681
$this->assertSame($container, $controller->getContainer());
6782
}

Tests/Functional/Bundle/TestBundle/Controller/FragmentController.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1614
use Symfony\Component\HttpFoundation\Request;
1715
use Symfony\Component\HttpFoundation\Response;
1816
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1917
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
2018
use Twig\Environment;
2119

22-
class FragmentController implements ContainerAwareInterface
20+
class FragmentController
2321
{
24-
use ContainerAwareTrait;
25-
2622
public function indexAction(Environment $twig)
2723
{
2824
return new Response($twig->render('fragment.html.twig', ['bar' => new Bar()]));

Tests/Functional/Bundle/TestBundle/Controller/ProfilerController.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1614
use Symfony\Component\HttpFoundation\Response;
1715

18-
class ProfilerController implements ContainerAwareInterface
16+
class ProfilerController
1917
{
20-
use ContainerAwareTrait;
21-
2218
public function indexAction()
2319
{
2420
return new Response('Hello');

Tests/Functional/Bundle/TestBundle/Controller/SessionController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
1615
use Symfony\Component\HttpFoundation\RedirectResponse;
1716
use Symfony\Component\HttpFoundation\Request;
1817
use Symfony\Component\HttpFoundation\Response;
1918

20-
class SessionController implements ContainerAwareInterface
19+
class SessionController
2120
{
22-
use ContainerAwareTrait;
21+
public function __construct(protected ContainerInterface $container)
22+
{
23+
}
2324

2425
public function welcomeAction(Request $request, $name = null)
2526
{

Tests/Functional/Bundle/TestBundle/Controller/SubRequestController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
1615
use Symfony\Component\HttpFoundation\Request;
1716
use Symfony\Component\HttpFoundation\Response;
1817
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1918

20-
class SubRequestController implements ContainerAwareInterface
19+
class SubRequestController
2120
{
22-
use ContainerAwareTrait;
21+
public function __construct(private ContainerInterface $container)
22+
{
23+
}
2324

2425
public function indexAction($handler)
2526
{

Tests/Functional/Bundle/TestBundle/Controller/SubRequestServiceResolutionController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1716
use Symfony\Component\HttpFoundation\Response;
1817
use Symfony\Component\HttpKernel\HttpKernelInterface;
1918

20-
class SubRequestServiceResolutionController implements ContainerAwareInterface
19+
class SubRequestServiceResolutionController
2120
{
22-
use ContainerAwareTrait;
21+
public function __construct(private ContainerInterface $container)
22+
{
23+
}
2324

2425
public function indexAction()
2526
{

Tests/Functional/app/ControllerServiceResolution/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ services:
55
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SubRequestServiceResolutionController:
66
public: true
77
tags: [controller.service_arguments]
8+
arguments: ['@service_container']
89

910
logger: { class: Psr\Log\NullLogger }
1011
Psr\Log\LoggerInterface: '@logger'

Tests/Functional/app/Session/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ imports:
22
- { resource: ./../config/default.yml }
33

44
services:
5+
_defaults:
6+
bind:
7+
Symfony\Component\DependencyInjection\ContainerInterface $container: '@service_container'
8+
9+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController:
10+
tags:
11+
- { name: controller.service_arguments }
12+
513
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SubRequestController:
614
tags:
715
- { name: controller.service_arguments, action: indexAction, argument: handler, id: fragment.handler }

0 commit comments

Comments
 (0)