Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitbf91eda

Browse files
Revert "feature#17608 [DependencyInjection] Autowiring: add setter injection support (dunglas)"
This reverts commit7eab6b9, reversingchanges made to35f201f.
1 parent46a8ede commitbf91eda

File tree

4 files changed

+9
-180
lines changed

4 files changed

+9
-180
lines changed

‎CHANGELOG-3.2.md‎

Lines 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‎

Lines 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‎

Lines 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-
privatefunctionautowireMethod($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-
thrownewRuntimeException(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+
thrownewRuntimeException(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 =newReference($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-
thrownewRuntimeException(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+
thrownewRuntimeException(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-
thrownewRuntimeException(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+
thrownewRuntimeException(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‎

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -429,47 +429,6 @@ public function testOptionalScalarArgsNotPassedIfLast()
429429
);
430430
}
431431

432-
publicfunctiontestSetterInjection()
433-
{
434-
$container =newContainerBuilder();
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 =newAutowirePass();
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(newReference('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-
publicfunctiontestSetterInjectionCollisionThrowsException()
527-
{
528-
$container =newContainerBuilder();
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 =newAutowirePass();
536-
$pass->process($container);
537-
}
538479
}
539480

540481
class Foo
@@ -707,69 +648,9 @@ public function setBar(Bar $bar)
707648
class IdenticalClassResourceextends ClassForResource
708649
{
709650
}
710-
711651
class ClassChangedConstructorArgsextends ClassForResource
712652
{
713653
publicfunction__construct($foo,Bar$bar,$baz)
714654
{
715655
}
716656
}
717-
718-
class SetterInjection
719-
{
720-
publicfunctionsetFoo(Foo$foo)
721-
{
722-
// should be called
723-
}
724-
725-
publicfunctionsetDependencies(Foo$foo,A$a)
726-
{
727-
// should be called
728-
}
729-
730-
publicfunctionsetBar()
731-
{
732-
// should not be called
733-
}
734-
735-
publicfunctionsetNotAutowireable(NotARealClass$n)
736-
{
737-
// should not be called
738-
}
739-
740-
publicfunctionsetArgCannotAutowire($foo)
741-
{
742-
// should not be called
743-
}
744-
745-
publicfunctionsetOptionalNotAutowireable(NotARealClass$n =null)
746-
{
747-
// should not be called
748-
}
749-
750-
publicfunctionsetOptionalNoTypeHint($foo =null)
751-
{
752-
// should not be called
753-
}
754-
755-
publicfunctionsetOptionalArgNoAutowireable($other ='default_val')
756-
{
757-
// should not be called
758-
}
759-
760-
publicfunctionsetWithCallsConfigured(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-
publicfunctionsetMultipleInstancesForOneArg(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)

[8]ページ先頭

©2009-2025 Movatter.jp