Skip to content

Commit 09195fa

Browse files
daniel-iwaniecnicolas-grekas
authored andcommitted
[DI] scope singly-implemented interfaces detection by file
1 parent 85113f1 commit 09195fa

24 files changed

+278
-12
lines changed

Loader/Configurator/ContainerConfigurator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ final public function parameters(): ParametersConfigurator
7373

7474
final public function services(): ServicesConfigurator
7575
{
76+
$this->loader->resetBeforeConfiguringServices();
77+
7678
return new ServicesConfigurator($this->container, $this->loader, $this->instanceof, $this->path, $this->anonymousCount);
7779
}
7880
}

Loader/Configurator/ServicesConfigurator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ final public function alias(string $id, string $referencedId): AliasConfigurator
105105
$ref = static::processValue($referencedId, true);
106106
$alias = new Alias((string) $ref, $this->defaults->isPublic());
107107
$this->container->setAlias($id, $alias);
108+
$this->loader->removeSinglyImplementedAlias((string) $ref);
108109

109110
return new AliasConfigurator($this, $alias);
110111
}

Loader/FileLoader.php

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

3336
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
3437
{
@@ -57,12 +60,10 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
5760
$classes = $this->findClasses($namespace, $resource, (array) $exclude);
5861
// prepare for deep cloning
5962
$serializedPrototype = serialize($prototype);
60-
$interfaces = [];
61-
$singlyImplemented = [];
6263

6364
foreach ($classes as $class => $errorMessage) {
6465
if (interface_exists($class, false)) {
65-
$interfaces[] = $class;
66+
$this->interfaces[] = $class;
6667
} else {
6768
$this->setDefinition($class, $definition = unserialize($serializedPrototype));
6869
if (null !== $errorMessage) {
@@ -71,14 +72,17 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
7172
continue;
7273
}
7374
foreach (class_implements($class, false) as $interface) {
74-
$singlyImplemented[$interface] = isset($singlyImplemented[$interface]) ? false : $class;
75+
$this->singlyImplemented[$interface] = isset($this->singlyImplemented[$interface]) ? false : $class;
7576
}
7677
}
7778
}
78-
foreach ($interfaces as $interface) {
79-
if (!empty($singlyImplemented[$interface])) {
80-
$this->container->setAlias($interface, $singlyImplemented[$interface])
81-
->setPublic(false);
79+
80+
foreach ($this->interfaces as $interface) {
81+
if (!empty($this->singlyImplemented[$interface]) && !$this->container->hasAlias($interface)) {
82+
$this->container->setAlias($interface, $this->singlyImplemented[$interface])->setPublic(false);
83+
$this->singlyImplementedAliases[$interface] = true;
84+
} elseif ($this->singlyImplementedAliases[$interface] ?? false) {
85+
$this->container->removeAlias($interface);
8286
}
8387
}
8488
}

Loader/PhpFileLoader.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ public function supports($resource, $type = null)
6363

6464
return 'php' === $type;
6565
}
66+
67+
public function resetBeforeConfiguringServices(): void
68+
{
69+
$this->interfaces = [];
70+
$this->singlyImplemented = [];
71+
$this->singlyImplementedAliases = [];
72+
}
73+
74+
public function removeSinglyImplementedAlias(string $alias): void
75+
{
76+
unset($this->singlyImplementedAliases[$alias]);
77+
}
6678
}
6779

6880
/**

Loader/XmlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public function load($resource, $type = null)
6666
$this->parseDefinitions($xml, $path, $defaults);
6767
} finally {
6868
$this->instanceof = [];
69+
$this->interfaces = [];
70+
$this->singlyImplemented = [];
71+
$this->singlyImplementedAliases = [];
6972
}
7073
}
7174

@@ -194,6 +197,7 @@ private function parseDefinition(\DOMElement $service, string $file, array $defa
194197
$this->validateAlias($service, $file);
195198

196199
$this->container->setAlias((string) $service->getAttribute('id'), $alias = new Alias($alias));
200+
unset($this->singlyImplementedAliases[(string) $service->getAttribute('id')]);
197201
if ($publicAttr = $service->getAttribute('public')) {
198202
$alias->setPublic(XmlUtils::phpize($publicAttr));
199203
} elseif (isset($defaults['public'])) {

Loader/YamlFileLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ public function load($resource, $type = null)
150150
$this->parseDefinitions($content, $path);
151151
} finally {
152152
$this->instanceof = [];
153+
$this->interfaces = [];
154+
$this->singlyImplemented = [];
155+
$this->singlyImplementedAliases = [];
153156
}
154157
}
155158

@@ -318,6 +321,7 @@ private function parseDefinition(string $id, $service, string $file, array $defa
318321

319322
if (\is_string($service) && 0 === strpos($service, '@')) {
320323
$this->container->setAlias($id, $alias = new Alias(substr($service, 1)));
324+
unset($this->singlyImplementedAliases[$id]);
321325
if (isset($defaults['public'])) {
322326
$alias->setPublic($defaults['public']);
323327
}
@@ -341,6 +345,7 @@ private function parseDefinition(string $id, $service, string $file, array $defa
341345

342346
if (isset($service['alias'])) {
343347
$this->container->setAlias($id, $alias = new Alias($service['alias']));
348+
unset($this->singlyImplementedAliases[$id]);
344349
if (\array_key_exists('public', $service)) {
345350
$alias->setPublic($service['public']);
346351
} elseif (isset($defaults['public'])) {
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])

0 commit comments

Comments
 (0)