You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #22277 [DI] Add "factory" support to named args and autowiring (nicolas-grekas)
This PR was merged into the 3.3-dev branch.
Discussion
----------
[DI] Add "factory" support to named args and autowiring
| Q | A
| ------------- | ---
| Branch? | 3.3
| Bug fix? | yes
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
To me, factories are expected to work named arguments.
Doing so also unlocks supporting them when autowiring, and will benefit #22187 soon.
Commits
-------
27470de [DI] Add "factory" support to named args and autowiring
if (!$r = $this->container->getReflectionClass($class)) {
118
+
thrownewRuntimeException(sprintf('Unable to resolve service "%s": class "%s" does not exist.', $this->currentId, $class));
119
+
}
120
+
if (!$r = $r->getConstructor()) {
121
+
if ($required) {
122
+
thrownewRuntimeException(sprintf('Unable to resolve service "%s": class%s has no constructor.', $this->currentId, sprintf($class !== $this->currentId ? ' "%s"' : '', $class)));
123
+
}
124
+
} elseif (!$r->isPublic()) {
125
+
thrownewRuntimeException(sprintf('Unable to resolve service "%s": %s must be public.', $this->currentId, sprintf($class !== $this->currentId ? 'constructor of class "%s"' : 'its constructor', $class)));
thrownewRuntimeException(sprintf('Unable to resolve service "%s": the class is not set.', $this->currentId));
147
+
}
148
+
149
+
if (!$r = $this->container->getReflectionClass($class)) {
150
+
thrownewRuntimeException(sprintf('Unable to resolve service "%s": class "%s" does not exist.', $this->currentId, $class));
151
+
}
152
+
153
+
if (!$r->hasMethod($method)) {
154
+
thrownewRuntimeException(sprintf('Unable to resolve service "%s": method "%s()" does not exist.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
155
+
}
156
+
157
+
$r = $r->getMethod($method);
158
+
if (!$r->isPublic()) {
159
+
thrownewRuntimeException(sprintf('Unable to resolve service "%s": method "%s()" must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
@@ -51,10 +44,14 @@ protected function processValue($value, $isRoot = false)
51
44
continue;
52
45
}
53
46
if ('' === $key || '$' !== $key[0]) {
54
-
thrownewInvalidArgumentException(sprintf('Invalid key "%s" found in arguments of method "%s" for service "%s": only integer or $named arguments are allowed.', $key, $method, $this->currentId));
47
+
thrownewInvalidArgumentException(sprintf('Invalid key "%s" found in arguments of method "%s()" for service "%s": only integer or $named arguments are allowed.', $key, $method, $this->currentId));
@@ -64,7 +61,7 @@ protected function processValue($value, $isRoot = false)
64
61
}
65
62
}
66
63
67
-
thrownewInvalidArgumentException(sprintf('Unable to resolve service "%s": method "%s::%s" has no argument named "%s". Check your service definition.', $this->currentId, $class,$method, $key));
64
+
thrownewInvalidArgumentException(sprintf('Unable to resolve service "%s": method "%s()" has no argument named "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method :$method, $key));
68
65
}
69
66
70
67
if ($resolvedArguments !== $call[1]) {
@@ -84,34 +81,4 @@ protected function processValue($value, $isRoot = false)
84
81
85
82
returnparent::processValue($value, $isRoot);
86
83
}
87
-
88
-
/**
89
-
* @param string|null $class
90
-
* @param string $method
91
-
*
92
-
* @throws InvalidArgumentException
93
-
*
94
-
* @return array
95
-
*/
96
-
privatefunctiongetParameters($class, $method)
97
-
{
98
-
if (!$class) {
99
-
thrownewInvalidArgumentException(sprintf('Unable to resolve service "%s": the class is not set.', $this->currentId));
100
-
}
101
-
102
-
if (!$r = $this->container->getReflectionClass($class)) {
103
-
thrownewInvalidArgumentException(sprintf('Unable to resolve service "%s": class "%s" does not exist.', $this->currentId, $class));
104
-
}
105
-
106
-
if (!$r->hasMethod($method)) {
107
-
thrownewInvalidArgumentException(sprintf('Unable to resolve service "%s": method "%s::%s" does not exist.', $this->currentId, $class, $method));
108
-
}
109
-
110
-
$method = $r->getMethod($method);
111
-
if (!$method->isPublic()) {
112
-
thrownewInvalidArgumentException(sprintf('Unable to resolve service "%s": method "%s::%s" must be public.', $this->currentId, $class, $method->name));
@@ -662,7 +661,7 @@ public function provideNotWireableCalls()
662
661
{
663
662
returnarray(
664
663
array('setNotAutowireable', 'Cannot autowire service "foo": argument $n of method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireable() has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotARealClass" but this class does not exist.'),
665
-
array(null, 'Cannot autowire service "foo": method Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setProtectedMethod() must be public.'),
664
+
array(null, 'Cannot autowire service "foo": method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setProtectedMethod()" must be public.'),
666
665
);
667
666
}
668
667
@@ -745,6 +744,9 @@ public function __construct(Foo $foo)
0 commit comments