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 bf91eda

Browse filesBrowse files
Revert "feature #17608 [DependencyInjection] Autowiring: add setter injection support (dunglas)"
This reverts commit 7eab6b9, reversing changes made to 35f201f.
1 parent 46a8ede commit bf91eda
Copy full SHA for bf91eda

File tree

4 files changed

+9
-180
lines changed
Filter options

4 files changed

+9
-180
lines changed

‎CHANGELOG-3.2.md

Copy file name to clipboardExpand all lines: CHANGELOG-3.2.md
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
101101
* feature #19325 [FrameworkBundle] Allow to specify a domain when updating translations (antograssiot)
102102
* feature #19277 [Serializer] Argument objects (theofidry, dunglas)
103103
* feature #19322 [HttpFoundation] Add Request::isMethodIdempotent method (dunglas)
104-
* feature #17608 [DependencyInjection] Autowiring: add setter injection support (dunglas)
105104
* feature #18510 Added a SecurityUserValueResolver for controllers (iltar)
106105
* feature #19203 [Bridge/Doctrine] Reset the EM lazy-proxy instead of the EM service (nicolas-grekas)
107106
* feature #19236 [FrameworkBundle] Deprecate the service serializer.mapping.cache.doctrine.apc (Ener-Getick)

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ CHANGELOG
44
3.2.0
55
-----
66

7-
* added support for setter autowiring
87
* allowed to prioritize compiler passes by introducing a third argument to `PassConfig::addPass()`, to `Compiler::addPass` and to `ContainerBuilder::addCompilerPass()`
98
* added support for PHP constants in YAML configuration files
109
* deprecated the ability to set or unset a private service with the `Container::set()` method

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+9-59Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,12 @@ private function completeDefinition($id, Definition $definition)
9797
$this->container->addResource(static::createResourceForClass($reflectionClass));
9898
}
9999

100-
if ($constructor = $reflectionClass->getConstructor()) {
101-
$this->autowireMethod($id, $definition, $constructor, true);
102-
}
103-
104-
$methodsCalled = array();
105-
foreach ($definition->getMethodCalls() as $methodCall) {
106-
$methodsCalled[$methodCall[0]] = true;
107-
}
108-
109-
foreach (self::getSetters($reflectionClass) as $reflectionMethod) {
110-
if (!isset($methodsCalled[$reflectionMethod->name])) {
111-
$this->autowireMethod($id, $definition, $reflectionMethod, false);
112-
}
113-
}
114-
}
115-
116-
/**
117-
* Autowires the constructor or a setter.
118-
*
119-
* @param string $id
120-
* @param Definition $definition
121-
* @param \ReflectionMethod $reflectionMethod
122-
* @param bool $isConstructor
123-
*
124-
* @throws RuntimeException
125-
*/
126-
private function autowireMethod($id, Definition $definition, \ReflectionMethod $reflectionMethod, $isConstructor)
127-
{
128-
if ($isConstructor) {
129-
$arguments = $definition->getArguments();
130-
} else {
131-
$arguments = array();
100+
if (!$constructor = $reflectionClass->getConstructor()) {
101+
return;
132102
}
133103

134-
$addMethodCall = false;
135-
foreach ($reflectionMethod->getParameters() as $index => $parameter) {
104+
$arguments = $definition->getArguments();
105+
foreach ($constructor->getParameters() as $index => $parameter) {
136106
if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
137107
continue;
138108
}
@@ -141,11 +111,7 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
141111
if (!$typeHint = $parameter->getClass()) {
142112
// no default value? Then fail
143113
if (!$parameter->isOptional()) {
144-
if ($isConstructor) {
145-
throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id));
146-
}
147-
148-
return;
114+
throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id));
149115
}
150116

151117
// specifically pass the default value
@@ -160,35 +126,24 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
160126

161127
if (isset($this->types[$typeHint->name])) {
162128
$value = new Reference($this->types[$typeHint->name]);
163-
$addMethodCall = true;
164129
} else {
165130
try {
166131
$value = $this->createAutowiredDefinition($typeHint, $id);
167-
$addMethodCall = true;
168132
} catch (RuntimeException $e) {
169133
if ($parameter->allowsNull()) {
170134
$value = null;
171135
} elseif ($parameter->isDefaultValueAvailable()) {
172136
$value = $parameter->getDefaultValue();
173137
} else {
174-
// The exception code is set to 1 if the exception must be thrown even if it's a setter
175-
if (1 === $e->getCode() || $isConstructor) {
176-
throw $e;
177-
}
178-
179-
return;
138+
throw $e;
180139
}
181140
}
182141
}
183142
} catch (\ReflectionException $e) {
184143
// Typehint against a non-existing class
185144

186145
if (!$parameter->isDefaultValueAvailable()) {
187-
if ($isConstructor) {
188-
throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $e->getMessage()), 0, $e);
189-
}
190-
191-
return;
146+
throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $e->getMessage()), 0, $e);
192147
}
193148

194149
$value = $parameter->getDefaultValue();
@@ -200,12 +155,7 @@ private function autowireMethod($id, Definition $definition, \ReflectionMethod $
200155
// it's possible index 1 was set, then index 0, then 2, etc
201156
// make sure that we re-order so they're injected as expected
202157
ksort($arguments);
203-
204-
if ($isConstructor) {
205-
$definition->setArguments($arguments);
206-
} elseif ($addMethodCall) {
207-
$definition->addMethodCall($reflectionMethod->name, $arguments);
208-
}
158+
$definition->setArguments($arguments);
209159
}
210160

211161
/**
@@ -303,7 +253,7 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
303253
$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
304254
$matchingServices = implode(', ', $this->ambiguousServiceTypes[$typeHint->name]);
305255

306-
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHint->name, $id, $classOrInterface, $matchingServices), 1);
256+
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHint->name, $id, $classOrInterface, $matchingServices));
307257
}
308258

309259
if (!$typeHint->isInstantiable()) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php
-119Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -429,47 +429,6 @@ public function testOptionalScalarArgsNotPassedIfLast()
429429
);
430430
}
431431

432-
public function testSetterInjection()
433-
{
434-
$container = new ContainerBuilder();
435-
$container->register('app_foo', Foo::class);
436-
$container->register('app_a', A::class);
437-
$container->register('app_collision_a', CollisionA::class);
438-
$container->register('app_collision_b', CollisionB::class);
439-
440-
// manually configure *one* call, to override autowiring
441-
$container
442-
->register('setter_injection', SetterInjection::class)
443-
->setAutowired(true)
444-
->addMethodCall('setWithCallsConfigured', array('manual_arg1', 'manual_arg2'))
445-
;
446-
447-
$pass = new AutowirePass();
448-
$pass->process($container);
449-
450-
$methodCalls = $container->getDefinition('setter_injection')->getMethodCalls();
451-
452-
// grab the call method names
453-
$actualMethodNameCalls = array_map(function ($call) {
454-
return $call[0];
455-
}, $methodCalls);
456-
$this->assertEquals(
457-
array('setWithCallsConfigured', 'setFoo'),
458-
$actualMethodNameCalls
459-
);
460-
461-
// test setWithCallsConfigured args
462-
$this->assertEquals(
463-
array('manual_arg1', 'manual_arg2'),
464-
$methodCalls[0][1]
465-
);
466-
// test setFoo args
467-
$this->assertEquals(
468-
array(new Reference('app_foo')),
469-
$methodCalls[1][1]
470-
);
471-
}
472-
473432
/**
474433
* @dataProvider getCreateResourceTests
475434
*/
@@ -517,24 +476,6 @@ public function testIgnoreServiceWithClassNotExisting()
517476

518477
$this->assertTrue($container->hasDefinition('bar'));
519478
}
520-
521-
/**
522-
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
523-
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" for the service "setter_injection_collision". Multiple services exist for this interface (c1, c2).
524-
* @expectedExceptionCode 1
525-
*/
526-
public function testSetterInjectionCollisionThrowsException()
527-
{
528-
$container = new ContainerBuilder();
529-
530-
$container->register('c1', CollisionA::class);
531-
$container->register('c2', CollisionB::class);
532-
$aDefinition = $container->register('setter_injection_collision', SetterInjectionCollision::class);
533-
$aDefinition->setAutowired(true);
534-
535-
$pass = new AutowirePass();
536-
$pass->process($container);
537-
}
538479
}
539480

540481
class Foo
@@ -707,69 +648,9 @@ public function setBar(Bar $bar)
707648
class IdenticalClassResource extends ClassForResource
708649
{
709650
}
710-
711651
class ClassChangedConstructorArgs extends ClassForResource
712652
{
713653
public function __construct($foo, Bar $bar, $baz)
714654
{
715655
}
716656
}
717-
718-
class SetterInjection
719-
{
720-
public function setFoo(Foo $foo)
721-
{
722-
// should be called
723-
}
724-
725-
public function setDependencies(Foo $foo, A $a)
726-
{
727-
// should be called
728-
}
729-
730-
public function setBar()
731-
{
732-
// should not be called
733-
}
734-
735-
public function setNotAutowireable(NotARealClass $n)
736-
{
737-
// should not be called
738-
}
739-
740-
public function setArgCannotAutowire($foo)
741-
{
742-
// should not be called
743-
}
744-
745-
public function setOptionalNotAutowireable(NotARealClass $n = null)
746-
{
747-
// should not be called
748-
}
749-
750-
public function setOptionalNoTypeHint($foo = null)
751-
{
752-
// should not be called
753-
}
754-
755-
public function setOptionalArgNoAutowireable($other = 'default_val')
756-
{
757-
// should not be called
758-
}
759-
760-
public function setWithCallsConfigured(A $a)
761-
{
762-
// this method has a calls configured on it
763-
// should not be called
764-
}
765-
}
766-
767-
class SetterInjectionCollision
768-
{
769-
public function setMultipleInstancesForOneArg(CollisionInterface $collision)
770-
{
771-
// The CollisionInterface cannot be autowired - there are multiple
772-
773-
// should throw an exception
774-
}
775-
}

0 commit comments

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