Skip to content

Commit 506db25

Browse files
committed
bug #33350 [DI] scope singly-implemented interfaces detection by file (daniel-iwaniec, nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [DI] scope singly-implemented interfaces detection by file | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | License | MIT [DependencyInjection] fixed handling singly implemented interfaces when importing multiple resources for example: ```yaml App\Adapter\: resource: '../src/Adapter/*' App\Port\: resource: '../src/Port/*' ``` this configuration wont create service for interface (in other words singly implemented interface wont be autowired) and this chage fixes it **Also** this will prevent false positives - for example if I had one implementation in \App\Port namespace and another in \App\Adapter then interface service would still be registered but that could potentially break exisitng code not aware of this bug Commits ------- c1f39709ff [DI] add FileLoader::registerAliasesForSinglyImplementedInterfaces() bec38900d8 [DI] scope singly-implemented interfaces detection by file
2 parents 14ee5e7 + ad4ae44 commit 506db25

25 files changed

+277
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* deprecated `tagged` in favor of `tagged_iterator`
1111
* deprecated passing an instance of `Symfony\Component\DependencyInjection\Parameter` as class name to `Symfony\Component\DependencyInjection\Definition`
1212
* added support for binding iterable and tagged services
13+
* made singly-implemented interfaces detection be scoped by file
1314

1415
4.3.0
1516
-----

Loader/Configurator/ServicesConfigurator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,9 @@ final public function __invoke(string $id, string $class = null): ServiceConfigu
139139
{
140140
return $this->set($id, $class);
141141
}
142+
143+
public function __destruct()
144+
{
145+
$this->loader->registerAliasesForSinglyImplementedInterfaces();
146+
}
142147
}

Loader/FileLoader.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ abstract class FileLoader extends BaseFileLoader
2929
protected $container;
3030
protected $isLoadingInstanceof = false;
3131
protected $instanceof = [];
32+
protected $interfaces = [];
33+
protected $singlyImplemented = [];
3234

3335
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
3436
{
@@ -57,12 +59,10 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
5759
$classes = $this->findClasses($namespace, $resource, (array) $exclude);
5860
// prepare for deep cloning
5961
$serializedPrototype = serialize($prototype);
60-
$interfaces = [];
61-
$singlyImplemented = [];
6262

6363
foreach ($classes as $class => $errorMessage) {
6464
if (interface_exists($class, false)) {
65-
$interfaces[] = $class;
65+
$this->interfaces[] = $class;
6666
} else {
6767
$this->setDefinition($class, $definition = unserialize($serializedPrototype));
6868
if (null !== $errorMessage) {
@@ -71,16 +71,21 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
7171
continue;
7272
}
7373
foreach (class_implements($class, false) as $interface) {
74-
$singlyImplemented[$interface] = isset($singlyImplemented[$interface]) ? false : $class;
74+
$this->singlyImplemented[$interface] = isset($this->singlyImplemented[$interface]) ? false : $class;
7575
}
7676
}
7777
}
78-
foreach ($interfaces as $interface) {
79-
if (!empty($singlyImplemented[$interface])) {
80-
$this->container->setAlias($interface, $singlyImplemented[$interface])
81-
->setPublic(false);
78+
}
79+
80+
public function registerAliasesForSinglyImplementedInterfaces()
81+
{
82+
foreach ($this->interfaces as $interface) {
83+
if (!empty($this->singlyImplemented[$interface]) && !$this->container->hasAlias($interface)) {
84+
$this->container->setAlias($interface, $this->singlyImplemented[$interface])->setPublic(false);
8285
}
8386
}
87+
88+
$this->interfaces = $this->singlyImplemented = [];
8489
}
8590

8691
/**

Loader/PhpFileLoader.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ public function load($resource, $type = null)
4141
return include $path;
4242
}, $this, ProtectedPhpFileLoader::class);
4343

44-
$callback = $load($path);
44+
try {
45+
$callback = $load($path);
4546

46-
if (\is_object($callback) && \is_callable($callback)) {
47-
$callback(new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource), $this->container, $this);
47+
if (\is_object($callback) && \is_callable($callback)) {
48+
$callback(new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource), $this->container, $this);
49+
}
50+
} finally {
51+
$this->instanceof = [];
52+
$this->registerAliasesForSinglyImplementedInterfaces();
4853
}
4954
}
5055

Loader/XmlFileLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function load($resource, $type = null)
6666
$this->parseDefinitions($xml, $path, $defaults);
6767
} finally {
6868
$this->instanceof = [];
69+
$this->registerAliasesForSinglyImplementedInterfaces();
6970
}
7071
}
7172

Loader/YamlFileLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public function load($resource, $type = null)
150150
$this->parseDefinitions($content, $path);
151151
} finally {
152152
$this->instanceof = [];
153+
$this->registerAliasesForSinglyImplementedInterfaces();
153154
}
154155
}
155156

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter;
4+
5+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\PortInterface;
6+
7+
class Adapter implements PortInterface
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\AnotherAdapter;
4+
5+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\PortInterface;
6+
7+
class Adapter implements PortInterface
8+
{
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port;
4+
5+
interface PortInterface
6+
{
7+
}

Tests/Fixtures/config/prototype.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
->tag('baz');
1010
$di->load(Prototype::class.'\\', '../Prototype')
1111
->autoconfigure()
12-
->exclude('../Prototype/{OtherDir,BadClasses}')
12+
->exclude('../Prototype/{OtherDir,BadClasses,SinglyImplementedInterface}')
1313
->factory('f')
1414
->deprecate('%service_id%')
1515
->args([0])

Tests/Fixtures/config/prototype_array.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
->tag('baz');
1010
$di->load(Prototype::class.'\\', '../Prototype')
1111
->autoconfigure()
12-
->exclude(['../Prototype/OtherDir', '../Prototype/BadClasses'])
12+
->exclude(['../Prototype/OtherDir', '../Prototype/BadClasses', '../Prototype/SinglyImplementedInterface'])
1313
->factory('f')
1414
->deprecate('%service_id%')
1515
->args([0])
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<defaults autowire="true" />
8+
9+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\"
10+
resource="../Prototype/SinglyImplementedInterface/Adapter/*" />
11+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\AnotherAdapter\"
12+
resource="../Prototype/SinglyImplementedInterface/AnotherAdapter/*" />
13+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\"
14+
resource="../Prototype/SinglyImplementedInterface/Port/*" />
15+
</services>
16+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<defaults autowire="true" />
8+
9+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\PortInterface"
10+
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\Adapter" />
11+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\"
12+
resource="../Prototype/SinglyImplementedInterface/Port/*" />
13+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\"
14+
resource="../Prototype/SinglyImplementedInterface/Adapter/*" />
15+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\AnotherAdapter\"
16+
resource="../Prototype/SinglyImplementedInterface/AnotherAdapter/*" />
17+
</services>
18+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<defaults autowire="true" />
8+
9+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\"
10+
resource="../Prototype/SinglyImplementedInterface/Port/*" />
11+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\"
12+
resource="../Prototype/SinglyImplementedInterface/Adapter/*" />
13+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\PortInterface"
14+
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\Adapter" />
15+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\AnotherAdapter\"
16+
resource="../Prototype/SinglyImplementedInterface/AnotherAdapter/*" />
17+
</services>
18+
</container>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
33
<services>
4-
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" exclude="../Prototype/{OtherDir,BadClasses}" />
4+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" exclude="../Prototype/{OtherDir,BadClasses,SinglyImplementedInterface}" />
55
</services>
66
</container>

Tests/Fixtures/xml/services_prototype_array.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*">
55
<exclude>../Prototype/OtherDir</exclude>
66
<exclude>../Prototype/BadClasses</exclude>
7+
<exclude>../Prototype/SinglyImplementedInterface</exclude>
78
</prototype>
89
</services>
910
</container>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<defaults autowire="true" />
8+
9+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\"
10+
resource="../Prototype/SinglyImplementedInterface/Port/*" />
11+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\"
12+
resource="../Prototype/SinglyImplementedInterface/Adapter/*" />
13+
</services>
14+
</container>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
5+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\:
6+
resource: ../Prototype/SinglyImplementedInterface/Adapter/*
7+
8+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\AnotherAdapter\:
9+
resource: ../Prototype/SinglyImplementedInterface/AnotherAdapter/*
10+
11+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\:
12+
resource: ../Prototype/SinglyImplementedInterface/Port/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
5+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\PortInterface:
6+
alias: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\Adapter
7+
8+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\:
9+
resource: ../Prototype/SinglyImplementedInterface/Port/*
10+
11+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\:
12+
resource: ../Prototype/SinglyImplementedInterface/Adapter/*
13+
14+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\AnotherAdapter\:
15+
resource: ../Prototype/SinglyImplementedInterface/AnotherAdapter/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
5+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\:
6+
resource: ../Prototype/SinglyImplementedInterface/Port/*
7+
8+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\:
9+
resource: ../Prototype/SinglyImplementedInterface/Adapter/*
10+
11+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\PortInterface:
12+
alias: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\Adapter
13+
14+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\AnotherAdapter\:
15+
resource: ../Prototype/SinglyImplementedInterface/AnotherAdapter/*
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
services:
22
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
33
resource: ../Prototype
4-
exclude: '../Prototype/{OtherDir,BadClasses}'
4+
exclude: '../Prototype/{OtherDir,BadClasses,SinglyImplementedInterface}'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
5+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Port\:
6+
resource: ../Prototype/SinglyImplementedInterface/Port/*
7+
8+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\SinglyImplementedInterface\Adapter\:
9+
resource: ../Prototype/SinglyImplementedInterface/Adapter/*

Tests/Loader/FileLoaderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,10 @@ public function supports($resource, $type = null): bool
240240
{
241241
return false;
242242
}
243+
244+
public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
245+
{
246+
parent::registerClasses($prototype, $namespace, $resource, $exclude);
247+
$this->registerAliasesForSinglyImplementedInterfaces();
248+
}
243249
}

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ public function testPrototype()
652652
[
653653
str_replace(\DIRECTORY_SEPARATOR, '/', $prototypeRealPath.\DIRECTORY_SEPARATOR.'OtherDir') => true,
654654
str_replace(\DIRECTORY_SEPARATOR, '/', $prototypeRealPath.\DIRECTORY_SEPARATOR.'BadClasses') => true,
655+
str_replace(\DIRECTORY_SEPARATOR, '/', $prototypeRealPath.\DIRECTORY_SEPARATOR.'SinglyImplementedInterface') => true,
655656
]
656657
);
657658
$this->assertContains((string) $globResource, $resources);
@@ -684,6 +685,7 @@ public function testPrototypeExcludeWithArray()
684685
[
685686
str_replace(\DIRECTORY_SEPARATOR, '/', $prototypeRealPath.\DIRECTORY_SEPARATOR.'BadClasses') => true,
686687
str_replace(\DIRECTORY_SEPARATOR, '/', $prototypeRealPath.\DIRECTORY_SEPARATOR.'OtherDir') => true,
688+
str_replace(\DIRECTORY_SEPARATOR, '/', $prototypeRealPath.\DIRECTORY_SEPARATOR.'SinglyImplementedInterface') => true,
687689
]
688690
);
689691
$this->assertContains((string) $globResource, $resources);
@@ -901,4 +903,50 @@ public function testOverriddenDefaultsBindings()
901903

902904
$this->assertSame('overridden', $container->get('bar')->quz);
903905
}
906+
907+
public function testSinglyImplementedInterfacesInMultipleResources()
908+
{
909+
$container = new ContainerBuilder();
910+
911+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
912+
$loader->load('singly_implemented_interface_in_multiple_resources.xml');
913+
914+
$alias = $container->getAlias(Prototype\SinglyImplementedInterface\Port\PortInterface::class);
915+
916+
$this->assertSame(Prototype\SinglyImplementedInterface\Adapter\Adapter::class, (string) $alias);
917+
}
918+
919+
public function testNotSinglyImplementedInterfacesInMultipleResources()
920+
{
921+
$container = new ContainerBuilder();
922+
923+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
924+
$loader->load('not_singly_implemented_interface_in_multiple_resources.xml');
925+
926+
$this->assertFalse($container->hasAlias(Prototype\SinglyImplementedInterface\Port\PortInterface::class));
927+
}
928+
929+
public function testNotSinglyImplementedInterfacesInMultipleResourcesWithPreviouslyRegisteredAlias()
930+
{
931+
$container = new ContainerBuilder();
932+
933+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
934+
$loader->load('not_singly_implemented_interface_in_multiple_resources_with_previously_registered_alias.xml');
935+
936+
$alias = $container->getAlias(Prototype\SinglyImplementedInterface\Port\PortInterface::class);
937+
938+
$this->assertSame(Prototype\SinglyImplementedInterface\Adapter\Adapter::class, (string) $alias);
939+
}
940+
941+
public function testNotSinglyImplementedInterfacesInMultipleResourcesWithPreviouslyRegisteredAlias2()
942+
{
943+
$container = new ContainerBuilder();
944+
945+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
946+
$loader->load('not_singly_implemented_interface_in_multiple_resources_with_previously_registered_alias2.xml');
947+
948+
$alias = $container->getAlias(Prototype\SinglyImplementedInterface\Port\PortInterface::class);
949+
950+
$this->assertSame(Prototype\SinglyImplementedInterface\Adapter\Adapter::class, (string) $alias);
951+
}
904952
}

0 commit comments

Comments
 (0)