Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 55c6f8f

Browse filesBrowse files
committed
[Dependency Injection] Add autowiring types for aliases
1 parent a86583d commit 55c6f8f
Copy full SHA for 55c6f8f

File tree

9 files changed

+144
-23
lines changed
Filter options

9 files changed

+144
-23
lines changed

‎src/Symfony/Component/DependencyInjection/Alias.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Alias.php
+71-1Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ class Alias
1515
{
1616
private $id;
1717
private $public;
18+
private $autowiringTypes = array();
1819

1920
/**
2021
* @param string $id Alias identifier
2122
* @param bool $public If this alias is public
2223
*/
23-
public function __construct($id, $public = true)
24+
public function __construct($id, $public = true, $autowiringTypes = array())
2425
{
2526
$this->id = strtolower($id);
2627
$this->public = $public;
28+
$this->setAutowiringTypes($autowiringTypes);
2729
}
2830

2931
/**
@@ -46,6 +48,74 @@ public function setPublic($boolean)
4648
$this->public = (bool) $boolean;
4749
}
4850

51+
/**
52+
* Gets autowiring types that will default to this alias.
53+
*
54+
* @return string[]
55+
*/
56+
public function getAutowiringTypes()
57+
{
58+
return array_keys($this->autowiringTypes);
59+
}
60+
61+
/**
62+
* Will this alias default for the given type?
63+
*
64+
* @param string $type
65+
*
66+
* @return bool
67+
*/
68+
public function hasAutowiringType($type)
69+
{
70+
return isset($this->autowiringTypes[$type]);
71+
}
72+
73+
/**
74+
* Adds a type that will default to this alias.
75+
*
76+
* @param string $type
77+
*
78+
* @return Alias The current instance
79+
*/
80+
public function addAutowiringType($type)
81+
{
82+
$this->autowiringTypes[$type] = true;
83+
84+
return $this;
85+
}
86+
87+
/**
88+
* Removes a type.
89+
*
90+
* @param string $type
91+
*
92+
* @return Alias The current instance
93+
*/
94+
public function removeAutowiringType($type)
95+
{
96+
unset($this->autowiringTypes[$type]);
97+
98+
return $this;
99+
}
100+
101+
/**
102+
* Sets types that will default to this alias.
103+
*
104+
* @param string[] $types
105+
*
106+
* @return Alias The current instance
107+
*/
108+
public function setAutowiringTypes(array $types)
109+
{
110+
$this->autowiringTypes = array();
111+
112+
foreach ($types as $type) {
113+
$this->autowiringTypes[$type] = true;
114+
}
115+
116+
return $this;
117+
}
118+
49119
/**
50120
* Returns the Id of this alias.
51121
*

‎src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ private function populateAvailableTypes()
218218
foreach ($this->container->getDefinitions() as $id => $definition) {
219219
$this->populateAvailableType($id, $definition);
220220
}
221+
222+
foreach ($this->container->getAliases() as $id => $alias) {
223+
foreach ($alias->getAutowiringTypes() as $type) {
224+
$this->definedTypes[$type] = true;
225+
$this->types[$type] = $id;
226+
}
227+
}
221228
}
222229

223230
/**

‎src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ private function parseDefinition(\DOMElement $service, $file)
139139
if ($publicAttr = $service->getAttribute('public')) {
140140
$public = XmlUtils::phpize($publicAttr);
141141
}
142-
$this->container->setAlias((string) $service->getAttribute('id'), new Alias($alias, $public));
142+
143+
$alias = new Alias($alias, $public);
144+
foreach ($this->getChildren($service, 'autowiring-type') as $type) {
145+
$alias->addAutowiringType($type->textContent);
146+
}
147+
148+
$this->container->setAlias((string) $service->getAttribute('id'), $alias);
143149

144150
return;
145151
}
@@ -510,7 +516,7 @@ private function validateAlias(\DOMElement $alias, $file)
510516
}
511517

512518
foreach ($alias->childNodes as $child) {
513-
if ($child instanceof \DOMElement && $child->namespaceURI === self::NS) {
519+
if ($child instanceof \DOMElement && $child->namespaceURI === self::NS && $child->tagName !== 'autowiring-type') {
514520
@trigger_error(sprintf('Using the element "%s" is deprecated for alias definition "%s" in "%s". The XmlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported elements.', $child->localName, $alias->getAttribute('id'), $file), E_USER_DEPRECATED);
515521
}
516522
}

‎src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+27-20Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ private function parseDefinition($id, $service, $file)
177177

178178
if (isset($service['alias'])) {
179179
$public = !array_key_exists('public', $service) || (bool) $service['public'];
180-
$this->container->setAlias($id, new Alias($service['alias'], $public));
180+
$this->container->setAlias($id, new Alias($service['alias'], $public, $this->parseAutowiringTypes($service, $id, $file)));
181181

182182
foreach ($service as $key => $value) {
183-
if (!in_array($key, array('alias', 'public'))) {
184-
@trigger_error(sprintf('The configuration key "%s" is unsupported for alias definition "%s" in "%s". Allowed configuration keys are "alias" and "public". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $key, $id, $file), E_USER_DEPRECATED);
183+
if (!in_array($key, array('alias', 'public', 'autowiring_types'))) {
184+
@trigger_error(sprintf('The configuration key "%s" is unsupported for alias definition "%s" in "%s". Allowed configuration keys are "alias", "public" and "autowiring_types". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $key, $id, $file), E_USER_DEPRECATED);
185185
}
186186
}
187187

@@ -305,23 +305,7 @@ private function parseDefinition($id, $service, $file)
305305
$definition->setAutowired($service['autowire']);
306306
}
307307

308-
if (isset($service['autowiring_types'])) {
309-
if (is_string($service['autowiring_types'])) {
310-
$definition->addAutowiringType($service['autowiring_types']);
311-
} else {
312-
if (!is_array($service['autowiring_types'])) {
313-
throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
314-
}
315-
316-
foreach ($service['autowiring_types'] as $autowiringType) {
317-
if (!is_string($autowiringType)) {
318-
throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
319-
}
320-
321-
$definition->addAutowiringType($autowiringType);
322-
}
323-
}
324-
}
308+
$definition->setAutowiringTypes($this->parseAutowiringTypes($service, $id, $file));
325309

326310
$this->container->setDefinition($id, $definition);
327311
}
@@ -501,6 +485,29 @@ private function loadFromExtensions($content)
501485
}
502486
}
503487

488+
private function parseAutowiringTypes(array $service, $id, $file)
489+
{
490+
if (!isset($service['autowiring_types'])) {
491+
return array();
492+
}
493+
494+
if (is_string($service['autowiring_types'])) {
495+
return array($service['autowiring_types']);
496+
} else {
497+
if (!is_array($service['autowiring_types'])) {
498+
throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
499+
}
500+
501+
foreach ($service['autowiring_types'] as $autowiringType) {
502+
if (!is_string($autowiringType)) {
503+
throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
504+
}
505+
}
506+
507+
return $service['autowiring_types'];
508+
}
509+
}
510+
504511
/**
505512
* Checks the keywords used to define a service.
506513
*

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,28 @@ public function testSetterInjectionCollisionThrowsException()
535535
$pass = new AutowirePass();
536536
$pass->process($container);
537537
}
538+
539+
public function testAutowiringTypesAliasDefinition()
540+
{
541+
$container = new ContainerBuilder();
542+
543+
$container->register('a.default', __NAMESPACE__.'\A');
544+
$container->register('a.extra', __NAMESPACE__.'\B');
545+
$container->register('c', __NAMESPACE__.'\C')->setAutowired(true);
546+
$container->setAlias('a', 'a.extra');
547+
548+
$container->getAlias('a')->addAutowiringType(__NAMESPACE__.'\A');
549+
550+
$pass = new AutowirePass();
551+
$pass->process($container);
552+
553+
$this->assertEquals(
554+
array(
555+
new Reference('a'),
556+
),
557+
$container->getDefinition('c')->getArguments()
558+
);
559+
}
538560
}
539561

540562
class Foo

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services22.xml

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services22.xml
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
<autowiring-type>Bar</autowiring-type>
66
<autowiring-type>Baz</autowiring-type>
77
</service>
8+
<service id="router" alias="router.default">
9+
<autowiring-type>Router</autowiring-type>
10+
</service>
811
</services>
912
</container>

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services22.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services22.yml
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ services:
66
baz_service:
77
class: Baz
88
autowiring_types: Foo
9+
10+
router:
11+
alias: router.default
12+
autowiring_types: Router

‎src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ public function testType()
545545
$loader->load('services22.xml');
546546

547547
$this->assertEquals(array('Bar', 'Baz'), $container->getDefinition('foo')->getAutowiringTypes());
548+
$this->assertEquals(array('Router'), $container->getAlias('router')->getAutowiringTypes());
548549
}
549550

550551
public function testAutowire()

‎src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ public function testTypes()
314314

315315
$this->assertEquals(array('Foo', 'Bar'), $container->getDefinition('foo_service')->getAutowiringTypes());
316316
$this->assertEquals(array('Foo'), $container->getDefinition('baz_service')->getAutowiringTypes());
317+
$this->assertEquals(array('Router'), $container->getAlias('router')->getAutowiringTypes());
317318
}
318319

319320
public function testAutowire()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.