Skip to content

Commit ad4ae44

Browse files
[DI] add FileLoader::registerAliasesForSinglyImplementedInterfaces()
1 parent 09195fa commit ad4ae44

File tree

8 files changed

+27
-31
lines changed

8 files changed

+27
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* deprecated support for short factories and short configurators in Yaml
99
* deprecated `tagged` in favor of `tagged_iterator`
1010
* deprecated passing an instance of `Symfony\Component\DependencyInjection\Parameter` as class name to `Symfony\Component\DependencyInjection\Definition`
11+
* made singly-implemented interfaces detection be scoped by file
1112

1213
4.3.0
1314
-----

Loader/Configurator/ContainerConfigurator.php

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

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

Loader/Configurator/ServicesConfigurator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ 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);
109108

110109
return new AliasConfigurator($this, $alias);
111110
}
@@ -140,4 +139,9 @@ final public function __invoke(string $id, string $class = null): ServiceConfigu
140139
{
141140
return $this->set($id, $class);
142141
}
142+
143+
public function __destruct()
144+
{
145+
$this->loader->registerAliasesForSinglyImplementedInterfaces();
146+
}
143147
}

Loader/FileLoader.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ abstract class FileLoader extends BaseFileLoader
3131
protected $instanceof = [];
3232
protected $interfaces = [];
3333
protected $singlyImplemented = [];
34-
protected $singlyImplementedAliases = [];
3534

3635
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
3736
{
@@ -76,15 +75,17 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
7675
}
7776
}
7877
}
78+
}
7979

80+
public function registerAliasesForSinglyImplementedInterfaces()
81+
{
8082
foreach ($this->interfaces as $interface) {
8183
if (!empty($this->singlyImplemented[$interface]) && !$this->container->hasAlias($interface)) {
8284
$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);
8685
}
8786
}
87+
88+
$this->interfaces = $this->singlyImplemented = [];
8889
}
8990

9091
/**

Loader/PhpFileLoader.php

Lines changed: 8 additions & 15 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

@@ -63,18 +68,6 @@ public function supports($resource, $type = null)
6368

6469
return 'php' === $type;
6570
}
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-
}
7871
}
7972

8073
/**

Loader/XmlFileLoader.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ 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 = [];
69+
$this->registerAliasesForSinglyImplementedInterfaces();
7270
}
7371
}
7472

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

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

Loader/YamlFileLoader.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ 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 = [];
153+
$this->registerAliasesForSinglyImplementedInterfaces();
156154
}
157155
}
158156

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

322320
if (\is_string($service) && 0 === strpos($service, '@')) {
323321
$this->container->setAlias($id, $alias = new Alias(substr($service, 1)));
324-
unset($this->singlyImplementedAliases[$id]);
325322
if (isset($defaults['public'])) {
326323
$alias->setPublic($defaults['public']);
327324
}
@@ -345,7 +342,6 @@ private function parseDefinition(string $id, $service, string $file, array $defa
345342

346343
if (isset($service['alias'])) {
347344
$this->container->setAlias($id, $alias = new Alias($service['alias']));
348-
unset($this->singlyImplementedAliases[$id]);
349345
if (\array_key_exists('public', $service)) {
350346
$alias->setPublic($service['public']);
351347
} elseif (isset($defaults['public'])) {

Tests/Loader/FileLoaderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,10 @@ public function supports($resource, $type = null): bool
260260
{
261261
return false;
262262
}
263+
264+
public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
265+
{
266+
parent::registerClasses($prototype, $namespace, $resource, $exclude);
267+
$this->registerAliasesForSinglyImplementedInterfaces();
268+
}
263269
}

0 commit comments

Comments
 (0)