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

[DependencyInjection] Introduce a flag to enable setter autowiring #19631

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 3 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ private function completeDefinition($id, Definition $definition)
$this->autowireMethod($id, $definition, $constructor, true);
}

if (!$definition->hasAutowiredSetters()) {
return;
}

$methodsCalled = array();
foreach ($definition->getMethodCalls() as $methodCall) {
$methodsCalled[$methodCall[0]] = true;
Expand Down
25 changes: 25 additions & 0 deletions 25 src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Definition
private $lazy = false;
private $decoratedService;
private $autowired = false;
private $autowiredSetters = false;
private $autowiringTypes = array();

protected $arguments;
Expand Down Expand Up @@ -679,6 +680,30 @@ public function setAutowired($autowired)
return $this;
}

/**
* Are setters autowired?
*
* @return bool
*/
public function hasAutowiredSetters()
{
return $this->autowiredSetters;
}

/**
* Sets setters autowired.
*
* @param $autowiredSetters
*
* @return Definition The current instance
*/
public function setAutowiredSetters($autowiredSetters)
{
$this->autowiredSetters = $autowiredSetters;

return $this;
}

/**
* Gets autowiring types that will default to this definition.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ private function parseDefinition(\DOMElement $service, $file)
$definition->setAutowired(XmlUtils::phpize($value));
}

if ($value = $service->getAttribute('autowire-setters')) {
$definition->setAutowiredSetters(XmlUtils::phpize($value));
}

if ($files = $this->getChildren($service, 'file')) {
$definition->setFile($files[0]->nodeValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class YamlFileLoader extends FileLoader
'decoration_inner_name' => 'decoration_inner_name',
'decoration_priority' => 'decoration_priority',
'autowire' => 'autowire',
'autowire_setters' => 'autowire_setters',
'autowiring_types' => 'autowiring_types',
);

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

if (isset($service['autowire_setters'])) {
$definition->setAutowiredSetters($service['autowire_setters']);
}

if (isset($service['autowiring_types'])) {
if (is_string($service['autowiring_types'])) {
$definition->addAutowiringType($service['autowiring_types']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<xsd:attribute name="decoration-inner-name" type="xsd:string" />
<xsd:attribute name="decoration-priority" type="xsd:integer" />
<xsd:attribute name="autowire" type="boolean" />
<xsd:attribute name="autowire-setters" type="boolean" />
</xsd:complexType>

<xsd:complexType name="tag">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ public function testSetterInjection()
$container
->register('setter_injection', SetterInjection::class)
->setAutowired(true)
->setAutowiredSetters(true)
->addMethodCall('setWithCallsConfigured', array('manual_arg1', 'manual_arg2'))
;

Expand Down Expand Up @@ -470,6 +471,20 @@ public function testSetterInjection()
);
}

public function testSetterInjectionNotEnabledByDefault()
{
$container = new ContainerBuilder();
$container
->register('setter_injection', SetterInjection::class)
->setAutowired(true)
;

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

$this->assertCount(0, $container->getDefinition('setter_injection')->getMethodCalls());
}

/**
* @dataProvider getCreateResourceTests
*/
Expand Down Expand Up @@ -531,6 +546,7 @@ public function testSetterInjectionCollisionThrowsException()
$container->register('c2', CollisionB::class);
$aDefinition = $container->register('setter_injection_collision', SetterInjectionCollision::class);
$aDefinition->setAutowired(true);
$aDefinition->setAutowiredSetters(true);

$pass = new AutowirePass();
$pass->process($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ public function testAutowired()
$this->assertTrue($def->isAutowired());
}

public function testSettersAutowired()
{
$def = new Definition('stdClass');
$this->assertFalse($def->hasAutowiredSetters());
$def->setAutowiredSetters(true);
$this->assertTrue($def->hasAutowiredSetters());
}

public function testTypes()
{
$def = new Definition('stdClass');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="bar" class="Bar" autowire="true" autowire-setters="true" />
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
bar_service:
class: BarClass
autowire: true
autowire_setters: true
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,15 @@ public function testAutowire()
$this->assertTrue($container->getDefinition('bar')->isAutowired());
}

public function testAutowireSetters()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services25.xml');

$this->assertTrue($container->getDefinition('bar')->hasAutowiredSetters());
}

/**
* @group legacy
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ public function testAutowire()
$this->assertTrue($container->getDefinition('bar_service')->isAutowired());
}

public function testAutowireSetters()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services25.yml');

$this->assertTrue($container->getDefinition('bar_service')->hasAutowiredSetters());
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage The value of the "decorates" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo").
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.