Skip to content

Commit f4ef751

Browse files
[DependencyInjection] Deprecate ContainerAwareInterface, ContainerAwareTrait and ContainerAwareLoader
1 parent 52a9292 commit f4ef751

File tree

25 files changed

+158
-51
lines changed

25 files changed

+158
-51
lines changed

UPGRADE-6.4.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
UPGRADE FROM 6.3 to 6.4
22
=======================
33

4+
DependencyInjection
5+
-------------------
6+
7+
* Deprecate `ContainerAwareInterface` and `ContainerAwareTrait`, use dependency injection instead
8+
9+
Before:
10+
```php
11+
class MyService implements ContainerAwareInterface
12+
{
13+
use ContainerAwareTrait;
14+
15+
// ...
16+
}
17+
```
18+
19+
After:
20+
```php
21+
class MyService
22+
{
23+
// Inject the container through the constructor...
24+
public function __construct(private ContainerInterface $container)
25+
{
26+
$this->container = $container;
27+
}
28+
29+
// ... or by using the #[Required] attribute
30+
#[Required]
31+
public function setContainer(ContainerInterface $container): void
32+
{
33+
$this->container = $container;
34+
}
35+
}
36+
```
37+
438
DoctrineBridge
539
--------------
640

741
* Deprecate `DbalLogger`, use a middleware instead
842
* Deprecate not constructing `DoctrineDataCollector` with an instance of `DebugDataHolder`
943
* Deprecate `DoctrineDataCollector::addLogger()`, use a `DebugDataHolder` instead
44+
* Deprecate `ContainerAwareLoader`, use dependency injection in your fixtures instead
1045

1146
HttpFoundation
1247
--------------

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Deprecate `DbalLogger`, use a middleware instead
88
* Deprecate not constructing `DoctrineDataCollector` with an instance of `DebugDataHolder`
99
* Deprecate `DoctrineDataCollector::addLogger()`, use a `DebugDataHolder` instead
10+
* Deprecate `ContainerAwareLoader`, use dependency injection in your fixtures instead
1011

1112
6.3
1213
---

src/Symfony/Bridge/Doctrine/DataFixtures/ContainerAwareLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1717
use Symfony\Component\DependencyInjection\ContainerInterface;
1818

19+
trigger_deprecation('symfony/dependency-injection', '6.4', '"%s" is deprecated, use dependency injection in your fixtures instead.', ContainerAwareLoader::class);
20+
1921
/**
2022
* Doctrine data fixtures loader that injects the service container into
2123
* fixture objects that implement ContainerAwareInterface.
2224
*
2325
* Note: Use of this class requires the Doctrine data fixtures extension, which
2426
* is a suggested dependency for Symfony.
27+
*
28+
* @deprecated since Symfony 6.4, use dependency injection in your fixtures instead
2529
*/
2630
class ContainerAwareLoader extends Loader
2731
{

src/Symfony/Bridge/Doctrine/Tests/DataFixtures/ContainerAwareLoaderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Symfony\Bridge\Doctrine\Tests\Fixtures\ContainerAwareFixture;
1717
use Symfony\Component\DependencyInjection\ContainerInterface;
1818

19+
/**
20+
* @group legacy
21+
*/
1922
class ContainerAwareLoaderTest extends TestCase
2023
{
2124
public function testShouldSetContainerOnContainerAwareFixture()

src/Symfony/Bridge/Doctrine/Tests/Fixtures/ContainerAwareFixture.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1717
use Symfony\Component\DependencyInjection\ContainerInterface;
1818

19+
/**
20+
* @deprecated since Symfony 6.4, to be removed in 7.0
21+
*/
1922
class ContainerAwareFixture implements FixtureInterface, ContainerAwareInterface
2023
{
2124
public ?ContainerInterface $container = null;

src/Symfony/Bundle/FrameworkBundle/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', sprintf('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

src/Symfony/Bundle/FrameworkBundle/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', sprintf('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) {

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Bundle/FrameworkBundle/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()]));

src/Symfony/Bundle/FrameworkBundle/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');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy