8000 [DI] add FileLoader::registerAliasesForSinglyImplementedInterfaces() · symfony/symfony@11bd896 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11bd896

Browse files
[DI] add FileLoader::registerAliasesForSinglyImplementedInterfaces()
1 parent bec3890 commit 11bd896

File tree

9 files changed

+44
-51
lines changed

9 files changed

+44
-51
lines changed

UPGRADE-4.4.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,37 @@ Debug
1616
DependencyInjection
1717
-------------------
1818

19+
* Made singly-implemented interfaces detection be scoped by file
1920
* Deprecated support for short factories and short configurators in Yaml
2021

2122
Before:
2223
```yaml
2324
services:
24-
my_service:
25-
factory: factory_service:method
25+
my_service:
26+
factory: factory_service:method
2627
```
2728
2829
After:
2930
```yaml
3031
services:
31-
my_service:
32-
factory: ['@factory_service', method]
32+
my_service:
33+
factory: ['@factory_service', method]
3334
```
35+
3436
* Deprecated `tagged` in favor of `tagged_iterator`
3537

3638
Before:
3739
```yaml
3840
services:
39-
App\Handler:
40-
tags: ['app.handler']
41-
4241
App\HandlerCollection:
43-
arguments: [!tagged app.handler]
42+
arguments: [!tagged my_tag]
4443
```
4544

4645
After:
4746
```yaml
4847
services:
49-
App\Handler:
50-
tags: ['app.handler']
51-
52-
App\HandlerCollection:
53-
arguments: [!tagged_iterator app.handler]
48+
App\HandlerCollection:
49+
arguments: [!tagged_iterator my_tag]
5450
```
5551

5652
* Passing an instance of `Symfony\Component\DependencyInjection\Parameter` as class name to `Symfony\Component\DependencyInjection\Definition` is deprecated.
@@ -143,6 +139,7 @@ HttpKernel
143139

144140
As many bundles must be compatible with a range of Symfony versions, the current
145141
directory convention is not deprecated yet, but it will be in the future.
142+
146143
* Deprecated the second and third argument of `KernelInterface::locateResource`
147144
* Deprecated the second and third argument of `FileLocator::__construct`
148145
* Deprecated loading resources from `%kernel.root_dir%/Resources` and `%kernel.root_dir%` as
@@ -244,13 +241,13 @@ TwigBundle
244241
Before (`templates/bundles/TwigBundle/Exception/error.jsonld.twig`):
245242
```twig
246243
{
247-
"@id": "https://example.com",
248-
"@type": "error",
249-
"@context": {
250-
"title": "{{ status_text }}",
251-
"code": {{ status_code }},
252-
"message": "{{ exception.message }}"
253-
}
244+
"@id": "https://example.com",
245+
"@type": "error",
246+
"@context": {
247+
"title": "{{ status_text }}",
248+
"code": {{ status_code }},
249+
"message": "{{ exception.message }}"
250+
}
254251
}
255252
```
256253

src/Symfony/Component/DependencyInjection/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
-----

src/Symfony/Component/DependencyInjection/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
}

src/Symfony/Component/DependencyInjection/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
}

src/Symfony/Component/DependencyInjection/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
/**

src/Symfony/Component/DependencyInjection/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
/**

src/Symfony/Component/DependencyInjection/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'])) {

src/Symfony/Component/DependencyInjection/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'])) {

src/Symfony/Component/DependencyInjection/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)
0