bug #34729 [DI] auto-register singly implemented interfaces by default (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[DI] auto-register singly implemented interfaces by default

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34524
| License       | MIT
| Doc PR        | -

Commits
-------

209b330fd6 [DI] auto-register singly implemented interfaces by default
This commit is contained in:
Nicolas Grekas 2019-11-30 15:05:16 +01:00
commit 9b658ed4a2
5 changed files with 14 additions and 6 deletions

View File

@ -36,6 +36,7 @@ abstract class FileLoader extends BaseFileLoader
protected $instanceof = []; protected $instanceof = [];
protected $interfaces = []; protected $interfaces = [];
protected $singlyImplemented = []; protected $singlyImplemented = [];
protected $autoRegisterAliasesForSinglyImplementedInterfaces = true;
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator) public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
{ {
@ -116,6 +117,10 @@ abstract class FileLoader extends BaseFileLoader
} }
} }
} }
if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) {
$this->registerAliasesForSinglyImplementedInterfaces();
}
} }
public function registerAliasesForSinglyImplementedInterfaces() public function registerAliasesForSinglyImplementedInterfaces()

View File

@ -23,6 +23,8 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
*/ */
class PhpFileLoader extends FileLoader class PhpFileLoader extends FileLoader
{ {
protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -36,6 +36,8 @@ class XmlFileLoader extends FileLoader
{ {
const NS = 'http://symfony.com/schema/dic/services'; const NS = 'http://symfony.com/schema/dic/services';
protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -110,6 +110,8 @@ class YamlFileLoader extends FileLoader
private $anonymousServicesCount; private $anonymousServicesCount;
private $anonymousServicesSuffix; private $anonymousServicesSuffix;
protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -89,6 +89,7 @@ class FileLoaderTest extends TestCase
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->setParameter('sub_dir', 'Sub'); $container->setParameter('sub_dir', 'Sub');
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures')); $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
$loader->autoRegisterAliasesForSinglyImplementedInterfaces = false;
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*'); $loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*');
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*'); // loading twice should not be an issue $loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*'); // loading twice should not be an issue
@ -121,7 +122,6 @@ class FileLoaderTest extends TestCase
// load everything, except OtherDir/AnotherSub & Foo.php // load everything, except OtherDir/AnotherSub & Foo.php
'Prototype/{%other_dir%/AnotherSub,Foo.php}' 'Prototype/{%other_dir%/AnotherSub,Foo.php}'
); );
$loader->registerAliasesForSinglyImplementedInterfaces();
$this->assertTrue($container->has(Bar::class)); $this->assertTrue($container->has(Bar::class));
$this->assertTrue($container->has(Baz::class)); $this->assertTrue($container->has(Baz::class));
@ -151,7 +151,6 @@ class FileLoaderTest extends TestCase
'Prototype/OtherDir/AnotherSub/DeeperBaz.php', 'Prototype/OtherDir/AnotherSub/DeeperBaz.php',
] ]
); );
$loader->registerAliasesForSinglyImplementedInterfaces();
$this->assertTrue($container->has(Foo::class)); $this->assertTrue($container->has(Foo::class));
$this->assertTrue($container->has(Baz::class)); $this->assertTrue($container->has(Baz::class));
@ -167,7 +166,6 @@ class FileLoaderTest extends TestCase
$prototype = new Definition(); $prototype = new Definition();
$prototype->setPublic(true)->setPrivate(true); $prototype->setPublic(true)->setPrivate(true);
$loader->registerClasses($prototype, 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/*'); $loader->registerClasses($prototype, 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/*');
$loader->registerAliasesForSinglyImplementedInterfaces();
$this->assertTrue($container->has(Bar::class)); $this->assertTrue($container->has(Bar::class));
$this->assertTrue($container->has(Baz::class)); $this->assertTrue($container->has(Baz::class));
@ -199,7 +197,6 @@ class FileLoaderTest extends TestCase
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\\', 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\\',
'Prototype/%bad_classes_dir%/*' 'Prototype/%bad_classes_dir%/*'
); );
$loader->registerAliasesForSinglyImplementedInterfaces();
$this->assertTrue($container->has(MissingParent::class)); $this->assertTrue($container->has(MissingParent::class));
@ -218,7 +215,6 @@ class FileLoaderTest extends TestCase
// the Sub is missing from namespace prefix // the Sub is missing from namespace prefix
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/Sub/*'); $loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/Sub/*');
$loader->registerAliasesForSinglyImplementedInterfaces();
} }
public function testRegisterClassesWithIncompatibleExclude() public function testRegisterClassesWithIncompatibleExclude()
@ -234,12 +230,13 @@ class FileLoaderTest extends TestCase
'Prototype/*', 'Prototype/*',
'yaml/*' 'yaml/*'
); );
$loader->registerAliasesForSinglyImplementedInterfaces();
} }
} }
class TestFileLoader extends FileLoader class TestFileLoader extends FileLoader
{ {
public $autoRegisterAliasesForSinglyImplementedInterfaces = true;
public function load($resource, $type = null) public function load($resource, $type = null)
{ {
return $resource; return $resource;