Skip to content

Commit 7aa8e65

Browse files
committed
[DependencyInjection] Deprecate autowiring service auto-registration
1 parent 531b294 commit 7aa8e65

File tree

6 files changed

+49
-20
lines changed

6 files changed

+49
-20
lines changed

UPGRADE-3.4.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ UPGRADE FROM 3.3 to 3.4
44
DependencyInjection
55
-------------------
66

7+
* Autowiring service auto-registration is deprecated and won't be supported
8+
in Symfony 4.0. Explicitely inject your dependencies or create services
9+
whose ids are their fully-qualified class name.
10+
711
* Top-level anonymous services in XML are deprecated and will throw an exception in Symfony 4.0.
812

913
Debug
@@ -30,13 +34,13 @@ FrameworkBundle
3034
require symfony/stopwatch` in your `dev` environment.
3135

3236
* Using the `KERNEL_DIR` environment variable or the automatic guessing based
33-
on the `phpunit.xml` / `phpunit.xml.dist` file location is deprecated since 3.4.
37+
on the `phpunit.xml` / `phpunit.xml.dist` file location is deprecated since 3.4.
3438
Set the `KERNEL_CLASS` environment variable to the fully-qualified class name
35-
of your Kernel instead. Not setting the `KERNEL_CLASS` environment variable
36-
will throw an exception on 4.0 unless you override the `KernelTestCase::createKernel()`
39+
of your Kernel instead. Not setting the `KERNEL_CLASS` environment variable
40+
will throw an exception on 4.0 unless you override the `KernelTestCase::createKernel()`
3741
or `KernelTestCase::getKernelClass()` method.
38-
39-
* The `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()`
42+
43+
* The `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()`
4044
methods are deprecated since 3.4 and will be removed in 4.0.
4145

4246
* The `--no-prefix` option of the `translation:update` command is deprecated and
@@ -83,7 +87,7 @@ TwigBridge
8387
* deprecated the `Symfony\Bridge\Twig\Form\TwigRenderer` class, use the `FormRenderer`
8488
class from the Form component instead
8589

86-
* deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
90+
* deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
8791
to pass a command name as first argument
8892

8993
* deprecated `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability
@@ -95,7 +99,7 @@ TwigBundle
9599
* deprecated the `Symfony\Bundle\TwigBundle\Command\DebugCommand` class, use the `DebugCommand`
96100
class from the Twig bridge instead
97101

98-
* deprecated relying on the `ContainerAwareInterface` implementation for
102+
* deprecated relying on the `ContainerAwareInterface` implementation for
99103
`Symfony\Bundle\TwigBundle\Command\LintCommand`
100104

101105
Validator

UPGRADE-4.0.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ Debug
7777
DependencyInjection
7878
-------------------
7979

80+
* Autowiring service auto-registration is not supported anymore.
81+
Explicitely inject your dependencies or create services whose ids are
82+
their fully-qualified class name.
83+
8084
* Autowiring services based on the types they implement is not supported anymore. Rename (or alias) your services to their FQCN id to make them autowirable.
8185

8286
* `_defaults` and `_instanceof` are now reserved service names in Yaml configurations. Please rename any services with that names.
@@ -338,9 +342,9 @@ FrameworkBundle
338342
class instead.
339343

340344
* Using the `KERNEL_DIR` environment variable and the automatic guessing based
341-
on the `phpunit.xml` file location have been removed from the `KernelTestCase::getKernelClass()`
345+
on the `phpunit.xml` file location have been removed from the `KernelTestCase::getKernelClass()`
342346
method implementation. Set the `KERNEL_CLASS` environment variable to the
343-
fully-qualified class name of your Kernel or override the `KernelTestCase::createKernel()`
347+
fully-qualified class name of your Kernel or override the `KernelTestCase::createKernel()`
344348
or `KernelTestCase::getKernelClass()` method instead.
345349

346350
* The `Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory` class has been removed.
@@ -349,10 +353,10 @@ FrameworkBundle
349353
* The `--no-prefix` option of the `translation:update` command has
350354
been removed.
351355

352-
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass` class has been removed.
356+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass` class has been removed.
353357
Use the `Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass` class instead.
354358

355-
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass` class has been removed.
359+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass` class has been removed.
356360
Use the `Symfony\Component\HttpKernel\DependencyInjection\AddCacheWarmerPass` class instead.
357361

358362
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass`
@@ -554,7 +558,7 @@ TwigBridge
554558
* The `TwigRendererEngine::setEnvironment()` method has been removed.
555559
Pass the Twig Environment as second argument of the constructor instead.
556560

557-
* Removed `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
561+
* Removed `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
558562
to pass a command name as first argument.
559563

560564
* Removed `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ private function createAutowiredDefinition($type)
477477
$this->currentId = $currentId;
478478
}
479479

480+
@trigger_error(sprintf('Using autowiring service auto-registration for type "%s" is deprecated since version 3.4 and won\'t be supported in 4.0. Create a service named "%s" instead.', $type, $type), E_USER_DEPRECATED);
481+
480482
$this->container->log($this, sprintf('Type "%s" has been auto-registered for service "%s".', $type, $this->currentId));
481483

482484
return new TypedReference($argumentId, $type);

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ public function testWithTypeSet()
287287
$this->assertEquals(CollisionInterface::class, (string) $container->getDefinition('a')->getArgument(0));
288288
}
289289

290+
/**
291+
* @group legacy
292+
* @expectedDeprecation Using autowiring service auto-registration for type "Symfony\Component\DependencyInjection\Tests\Compiler\Lille" is deprecated since version 3.4 and won't be supported in 4.0. Create a service named "Symfony\Component\DependencyInjection\Tests\Compiler\Lille" instead.
293+
* @expectedDeprecation Using autowiring service auto-registration for type "Symfony\Component\DependencyInjection\Tests\Compiler\Dunglas" is deprecated since version 3.4 and won't be supported in 4.0. Create a service named "Symfony\Component\DependencyInjection\Tests\Compiler\Dunglas" instead.
294+
*/
290295
public function testCreateDefinition()
291296
{
292297
$container = new ContainerBuilder();
@@ -368,6 +373,8 @@ public function testClassNotFoundThrowsException()
368373
$aDefinition = $container->register('a', __NAMESPACE__.'\BadTypeHintedArgument');
369374
$aDefinition->setAutowired(true);
370375

376+
$container->register(Dunglas::class, Dunglas::class);
377+
371378
$pass = new AutowirePass();
372379
$pass->process($container);
373380
}
@@ -383,6 +390,8 @@ public function testParentClassNotFoundThrowsException()
383390
$aDefinition = $container->register('a', __NAMESPACE__.'\BadParentTypeHintedArgument');
384391
$aDefinition->setAutowired(true);
385392

393+
$container->register(Dunglas::class, Dunglas::class);
394+
386395
$pass = new AutowirePass();
387396
$pass->process($container);
388397
}
@@ -595,6 +604,10 @@ public function testExplicitMethodInjection()
595604
);
596605
}
597606

607+
/**
608+
* @group legacy
609+
* @expectedDeprecation Using autowiring service auto-registration for type "Symfony\Component\DependencyInjection\Tests\Compiler\A" is deprecated since version 3.4 and won't be supported in 4.0. Create a service named "Symfony\Component\DependencyInjection\Tests\Compiler\A" instead.
610+
*/
598611
public function testTypedReference()
599612
{
600613
$container = new ContainerBuilder();
@@ -653,6 +666,8 @@ public function testIgnoreServiceWithClassNotExisting()
653666
$barDefinition = $container->register('bar', __NAMESPACE__.'\Bar');
654667
$barDefinition->setAutowired(true);
655668

669+
$container->register(Foo::class, Foo::class);
670+
656671
$pass = new AutowirePass();
657672
$pass->process($container);
658673

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\DependencyInjection\Definition;
2929
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
3030
use Symfony\Component\DependencyInjection\ServiceLocator;
31+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
3132
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
3233
use Symfony\Component\DependencyInjection\Variable;
3334
use Symfony\Component\ExpressionLanguage\Expression;
@@ -560,6 +561,9 @@ public function testServiceSubscriber()
560561
))
561562
;
562563
$container->register(TestServiceSubscriber::class, TestServiceSubscriber::class);
564+
565+
$container->register(CustomDefinition::class, CustomDefinition::class)
566+
->setPublic(false);
563567
$container->compile();
564568

565569
$dumper = new PhpDumper($container);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ public function __construct()
2828
{
2929
$this->services = array();
3030
$this->normalizedIds = array(
31-
'autowired.symfony\\component\\dependencyinjection\\tests\\fixtures\\customdefinition' => 'autowired.Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
31+
'symfony\\component\\dependencyinjection\\tests\\fixtures\\customdefinition' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
3232
'symfony\\component\\dependencyinjection\\tests\\fixtures\\testservicesubscriber' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber',
3333
);
3434
$this->methodMap = array(
35+
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => 'getSymfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService',
3536
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'getSymfony_Component_DependencyInjection_Tests_Fixtures_TestServiceSubscriberService',
36-
'autowired.Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => 'getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService',
3737
'foo_service' => 'getFooServiceService',
3838
);
3939
$this->privates = array(
40-
'autowired.Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true,
40+
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true,
4141
);
4242

4343
$this->aliases = array();
@@ -87,23 +87,23 @@ protected function getSymfony_Component_DependencyInjection_Tests_Fixtures_TestS
8787
protected function getFooServiceService()
8888
{
8989
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber(new \Symfony\Component\DependencyInjection\ServiceLocator(array('Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => function () {
90-
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition $v) { return $v; }; return $f(${($_ = isset($this->services['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition']) ? $this->services['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] : $this->getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()) && false ?: '_'});
90+
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition $v = null) { return $v; }; return $f(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] : $this->getSymfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()) && false ?: '_'});
9191
}, 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => function () {
9292
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber $v) { return $v; }; return $f(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] : $this->get('Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber')) && false ?: '_'});
9393
}, 'bar' => function () {
9494
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition $v) { return $v; }; return $f(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] : $this->get('Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber')) && false ?: '_'});
9595
}, 'baz' => function () {
96-
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition $v) { return $v; }; return $f(${($_ = isset($this->services['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition']) ? $this->services['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] : $this->getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()) && false ?: '_'});
96+
$f = function (\Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition $v = null) { return $v; }; return $f(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] : $this->getSymfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()) && false ?: '_'});
9797
})));
9898
}
9999

100100
/**
101-
* Gets the private 'autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition' shared autowired service.
101+
* Gets the private 'Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition' shared service.
102102
*
103103
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition
104104
*/
105-
protected function getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()
105+
protected function getSymfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()
106106
{
107-
return $this->services['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition();
107+
return $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition();
108108
}
109109
}

0 commit comments

Comments
 (0)