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

[Dependency Injection] Add autowiring types for aliases #19970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion 72 src/Symfony/Component/DependencyInjection/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class Alias
{
private $id;
private $public;
private $autowiringTypes = array();

/**
* @param string $id Alias identifier
* @param bool $public If this alias is public
*/
public function __construct($id, $public = true)
public function __construct($id, $public = true, $autowiringTypes = array())
{
$this->id = strtolower($id);
$this->public = $public;
$this->setAutowiringTypes($autowiringTypes);
}

/**
Expand All @@ -46,6 +48,74 @@ public function setPublic($boolean)
$this->public = (bool) $boolean;
}

/**
* Gets autowiring types that will default to this alias.
*
* @return string[]
*/
public function getAutowiringTypes()
{
return array_keys($this->autowiringTypes);
}

/**
* Will this alias default for the given type?
*
* @param string $type
*
* @return bool
*/
public function hasAutowiringType($type)
{
return isset($this->autowiringTypes[$type]);
}

/**
* Adds a type that will default to this alias.
*
* @param string $type
*
* @return Alias The current instance
*/
public function addAutowiringType($type)
{
$this->autowiringTypes[$type] = true;

return $this;
}

/**
* Removes a type.
*
* @param string $type
*
* @return Alias The current instance
*/
public function removeAutowiringType($type)
{
unset($this->autowiringTypes[$type]);

return $this;
}

/**
* Sets types that will default to this alias.
*
* @param string[] $types
*
* @return Alias The current instance
*/
public function setAutowiringTypes(array $types)
{
$this->autowiringTypes = array();

foreach ($types as $type) {
$this->autowiringTypes[$type] = true;
}

return $this;
}

/**
* Returns the Id of this alias.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ private function populateAvailableTypes()
foreach ($this->container->getDefinitions() as $id => $definition) {
$this->populateAvailableType($id, $definition);
}

foreach ($this->container->getAliases() as $id => $alias) {
foreach ($alias->getAutowiringTypes() as $type) {
$this->definedTypes[$type] = true;
$this->types[$type] = $id;
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ private function parseDefinition(\DOMElement $service, $file)
if ($publicAttr = $service->getAttribute('public')) {
$public = XmlUtils::phpize($publicAttr);
}
$this->container->setAlias((string) $service->getAttribute('id'), new Alias($alias, $public));

$alias = new Alias($alias, $public);
foreach ($this->getChildren($service, 'autowiring-type') as $type) {
$alias->addAutowiringType($type->textContent);
}

$this->container->setAlias((string) $service->getAttribute('id'), $alias);

return;
}
Expand Down Expand Up @@ -510,7 +516,7 @@ private function validateAlias(\DOMElement $alias, $file)
}

foreach ($alias->childNodes as $child) {
if ($child instanceof \DOMElement && $child->namespaceURI === self::NS) {
if ($child instanceof \DOMElement && $child->namespaceURI === self::NS && $child->tagName !== 'autowiring-type') {
@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);
}
}
Expand Down
47 changes: 27 additions & 20 deletions 47 src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ private function parseDefinition($id, $service, $file)

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

foreach ($service as $key => $value) {
if (!in_array($key, array('alias', 'public'))) {
@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);
if (!in_array($key, array('alias', 'public', 'autowiring_types'))) {
@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);
}
}

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

if (isset($service['autowiring_types'])) {
if (is_string($service['autowiring_types'])) {
$definition->addAutowiringType($service['autowiring_types']);
} else {
if (!is_array($service['autowiring_types'])) {
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));
}

foreach ($service['autowiring_types'] as $autowiringType) {
if (!is_string($autowiringType)) {
throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
}

$definition->addAutowiringType($autowiringType);
}
}
}
$definition->setAutowiringTypes($this->parseAutowiringTypes($service, $id, $file));

$this->container->setDefinition($id, $definition);
}
Expand Down Expand Up @@ -501,6 +485,29 @@ private function loadFromExtensions($content)
}
}

private function parseAutowiringTypes(array $service, $id, $file)
{
if (!isset($service['autowiring_types'])) {
return array();
}

if (is_string($service['autowiring_types'])) {
return array($service['autowiring_types']);
} else {
if (!is_array($service['autowiring_types'])) {
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));
}

foreach ($service['autowiring_types'] as $autowiringType) {
if (!is_string($autowiringType)) {
throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
}

return $service['autowiring_types'];
}
}

/**
* Checks the keywords used to define a service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,28 @@ public function testSetterInjectionCollisionThrowsException()
$pass = new AutowirePass();
$pass->process($container);
}

public function testAutowiringTypesAliasDefinition()
{
$container = new ContainerBuilder();

$container->register('a.default', __NAMESPACE__.'\A');
$container->register('a.extra', __NAMESPACE__.'\B');
$container->register('c', __NAMESPACE__.'\C')->setAutowired(true);
$container->setAlias('a', 'a.extra');

$container->getAlias('a')->addAutowiringType(__NAMESPACE__.'\A');

$pass = new AutowirePass();
$pass->process($container);

$this->assertEquals(
array(
new Reference('a'),
),
$container->getDefinition('c')->getArguments()
);
}
}

class Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
<autowiring-type>Bar</autowiring-type>
<autowiring-type>Baz</autowiring-type>
</service>
<service id="router" alias="router.default">
<autowiring-type>Router</autowiring-type>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ services:
baz_service:
class: Baz
autowiring_types: Foo

router:
alias: router.default
autowiring_types: Router
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ public function testType()
$loader->load('services22.xml');

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

public function testAutowire()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public function testTypes()

$this->assertEquals(array('Foo', 'Bar'), $container->getDefinition('foo_service')->getAutowiringTypes());
$this->assertEquals(array('Foo'), $container->getDefinition('baz_service')->getAutowiringTypes());
$this->assertEquals(array('Router'), $container->getAlias('router')->getAutowiringTypes());
}

public function testAutowire()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.