8000 [DI] Refacto / cleanup / minor fixes · symfony/symfony@2818954 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2818954

Browse files
[DI] Refacto / cleanup / minor fixes
1 parent a736458 commit 2818954

25 files changed

+319
-270
lines changed

src/Symfony/Component/DependencyInjection/ChildDefinition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ public function setDeprecated($boolean = true, $template = null)
162162
/**
163163
* {@inheritdoc}
164164
*/
165-
public function setAutowiredMethods(array $autowiredMethods)
165+
public function setAutowiredCalls(array $autowiredCalls)
166166
{
167-
$this->changes['autowired_methods'] = true;
167+
$this->changes['autowired_calls'] = true;
168168

169-
return parent::setAutowiredMethods($autowiredMethods);
169+
return parent::setAutowiredCalls($autowiredCalls);
170170
}
171171

172172
/**

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
class AutowirePass extends AbstractRecursivePass
2626
{
27+
const MODE_REQUIRED = 1;
28+
const MODE_OPTIONAL = 2;
29+
2730
private $definedTypes = array();
2831
private $types;
2932
private $ambiguousServiceTypes = array();
@@ -72,15 +75,15 @@ public static function createResourceForClass(\ReflectionClass $reflectionClass)
7275
*/
7376
protected function processValue($value, $isRoot = false)
7477
{
75-
if (!$value instanceof Definition || !$autowiredMethods = $value->getAutowiredMethods()) {
78+
if (!$value instanceof Definition || !$value->getAutowiredCalls()) {
7679
return parent::processValue($value, $isRoot);
7780
}
7881

7982
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass())) {
8083
return parent::processValue($value, $isRoot);
8184
}
8285

83-
$autowiredMethods = $this->getMethodsToAutowire($reflectionClass, $autowiredMethods);
86+
$autowiredMethods = $this->getMethodsToAutowire($reflectionClass, $value->getAutowiredCalls());
8487
$methodCalls = $value->getMethodCalls();
8588

8689
if ($constructor = $reflectionClass->getConstructor()) {
@@ -89,7 +92,7 @@ protected function processValue($value, $isRoot = false)
8992
throw new RuntimeException(sprintf('Cannot autowire service "%s": class %s has no constructor but arguments are defined.', $this->currentId, $reflectionClass->name, $method));
9093
}
9194

92-
$methodCalls = $this->autowireMethodCalls($reflectionClass, $methodCalls, $autowiredMethods);
95+
$methodCalls = $this->autowireCalls($reflectionClass, $methodCalls, $autowiredMethods);
9396
$overriddenGetters = $this->autowireOverridenGetters($value->getOverriddenGetters(), $autowiredMethods);
9497

9598
if ($constructor) {
@@ -125,6 +128,14 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
125128
$regexList = array();
126129
$methodsToAutowire = array();
127130

131+
if ($reflectionMethod = $reflectionClass->getConstructor()) {
132+
$methodsToAutowire[$lcMethod = strtolower($reflectionMethod->name)] = $reflectionMethod;
133+
unset($autowiredMethods[$lcMethod]);
134+
}
135+
if (!$autowiredMethods) {
136+
return $methodsToAutowire;
137+
}
138+
128139
foreach ($autowiredMethods as $pattern) {
129140
$regexList[] = '/^'.str_replace('\*', '.*', preg_quote($pattern, '/')).'$/i';
130141
}
@@ -141,10 +152,6 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
141152
continue 2;
142153
}
143154
}
144-
145-
if ($reflectionMethod->isConstructor()) {
146-
$methodsToAutowire[strtolower($reflectionMethod->name)] = $reflectionMethod;
147-
}
148155
}
149156

150157
if ($notFound = array_diff($autowiredMethods, $found)) {
@@ -161,13 +168,10 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
161168
*
162169
* @return array
163170
*/
164-
private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $methodCalls, array $autowiredMethods)
171+
private function autowireCalls(\ReflectionClass $reflectionClass, array $methodCalls, array $autowiredMethods)
165172
{
166-
$parameterBag = $this->container->getParameterBag();
167-
168173
foreach ($methodCalls as $i => $call) {
169174
list($method, $arguments) = $call;
170-
$method = $parameterBag->resolveValue($method);
171175

172176
if (isset($autowiredMethods[$lcMethod = strtolower($method)]) && $autowiredMethods[$lcMethod]->isPublic()) {
173177
$reflectionMethod = $autowiredMethods[$lcMethod];
@@ -182,15 +186,15 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
182186
}
183187
}
184188

185-
$arguments = $this->autowireMethodCall($reflectionMethod, $arguments, true);
189+
$arguments = $this->autowireMethod($reflectionMethod, $arguments, self::MODE_REQUIRED);
186190

187191
if ($arguments !== $call[1]) {
188192
$methodCalls[$i][1] = $arguments;
189193
}
190194
}
191195

192-
foreach ($autowiredMethods as $reflectionMethod) {
193-
if ($reflectionMethod->isPublic() && $arguments = $this->autowireMethodCall($reflectionMethod, array(), false)) {
196+
foreach ($autowiredMethods as $lcMethod => $reflectionMethod) {
197+
if ($reflectionMethod->isPublic() && $arguments = $this->autowireMethod($reflectionMethod, array(), self::MODE_OPTIONAL)) {
194198
$methodCalls[] = array($reflectionMethod->name, $arguments);
195199
}
196200
}
@@ -203,19 +207,22 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
203207
*
204208
* @param \ReflectionMethod $reflectionMethod
205209
* @param array $arguments
206-
* @param bool $mustAutowire
210+
* @param int $mode
207211
*
208212
* @return array The autowired arguments
209213
*
210214
* @throws RuntimeException
211215
*/
212-
private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $arguments, $mustAutowire)
216+
private function autowireMethod(\ReflectionMethod $reflectionMethod, array $arguments, $mode)
213217
{
214218
$didAutowire = false; // Whether any arguments have been autowired or not
215219
foreach ($reflectionMethod->getParameters() as $index => $parameter) {
216220
if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
217221
continue;
218222
}
223+
if (method_exists($parameter, 'isVariadic') && $parameter->isVariadic()) {
224+
continue;
225+
}
219226

220227
if (method_exists($parameter, 'getType')) {
221228
if ($typeName = $parameter->getType()) {
@@ -228,7 +235,7 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
228235
if (!$typeName) {
229236
// no default value? Then fail
230237
if (!$parameter->isOptional()) {
231-
if ($mustAutowire) {
238+
if (self::MODE_REQUIRED === $mode) {
232239
throw new RuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s::%s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $reflectionMethod->class, $reflectionMethod->name));
233240
}
234241

@@ -268,7 +275,7 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
268275
$value = $parameter->getDefaultValue();
269276
} else {
270277
// The exception code is set to 1 if the exception must be thrown even if it's an optional setter
271-
if (1 === $e->getCode() || $mustAutowire) {
278+
if (1 === $e->getCode() || self::MODE_REQUIRED === $mode) {
272279
throw $e;
273280
}
274281

@@ -279,7 +286,7 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
279286
// Typehint against a non-existing class
280287

281288
if (!$parameter->isDefaultValueAvailable()) {
282-
if ($mustAutowire) {
289+
if (self::MODE_REQUIRED === $mode) {
283290
throw new RuntimeException(sprintf('Cannot autowire argument $%s of method %s::%s() for service "%s": Class %s does not exist.', $parameter->name, $reflectionMethod->class, $reflectionMethod->name, $this->currentId, $typeName));
284291
}
285292

@@ -292,7 +299,7 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
292299
$arguments[$index] = $value;
293300
}
294301

295-
if (!$mustAutowire && !$didAutowire) {
302+
if (self::MODE_REQUIRED !== $mode && !$didAutowire) {
296303
return array();
297304
}
298305

@@ -313,8 +320,8 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
313320
*/
314321
private function autowireOverridenGetters(array $overridenGetters, array $autowiredMethods)
315322
{
316-
foreach ($autowiredMethods as $reflectionMethod) {
317-
if (isset($overridenGetters[strtolower($reflectionMethod->name)])
323+
foreach ($autowiredMethods as $lcMethod => $reflectionMethod) {
324+
if (isset($overridenGetters[$lcMethod])
318325
|| !method_exists($reflectionMethod, 'getReturnType')
319326
|| 0 !== $reflectionMethod->getNumberOfParameters()
320327
|| $reflectionMethod->isFinal()
@@ -326,7 +333,7 @@ private function autowireOverridenGetters(array $overridenGetters, array $autowi
326333
$typeName = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType->__toString();
327334

328335
if ($this->container->has($typeName) && !$this->container->findDefinition($typeName)->isAbstract()) {
329-
$overridenGetters[$reflectionMethod->name] = new Reference($typeName);
336+
$overridenGetters[$lcMethod] = new Reference($typeName);
330337
continue;
331338
}
332339

@@ -346,7 +353,7 @@ private function autowireOverridenGetters(array $overridenGetters, array $autowi
346353
continue;
347354
}
348355

349-
$overridenGetters[$reflectionMethod->name] = $value;
356+
$overridenGetters[$lcMethod] = $value;
350357
}
351358

352359
return $overridenGetters;

src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private function doResolveDefinition(ChildDefinition $definition)
101101
$def->setFile($parentDef->getFile());
102102
$def->setPublic($parentDef->isPublic());
103103
$def->setLazy($parentDef->isLazy());
104-
$def->setAutowiredMethods($parentDef->getAutowiredMethods());
104+
$def->setAutowiredCalls($parentDef->getAutowiredCalls());
105105

106106
// overwrite with values specified in the decorator
107107
$changes = $definition->getChanges();
@@ -126,8 +126,8 @@ private function doResolveDefinition(ChildDefinition $definition)
126126
if (isset($changes['deprecated'])) {
127127
$def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
128128
}
129-
if (isset($changes['autowired_methods'])) {
130-
$def->setAutowiredMethods($definition->getAutowiredMethods());
129+
if (isset($changes['autowired_calls'])) {
130+
$def->setAutowiredCalls($definition->getAutowiredCalls());
131131
}
132132
if (isset($changes['decorated_service'])) {
133133
$decoratedService = $definition->getDecoratedService();

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
use Symfony\Component\Config\Resource\ResourceInterface;
3434
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
3535
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
36-
use Symfony\Component\DependencyInjection\LazyProxy\GetterProxyInterface;
36+
use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyHelper;
37+
use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyInterface;
3738
use Symfony\Component\ExpressionLanguage\Expression;
3839
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
3940

@@ -1022,7 +1023,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10221023
if (null === $salt) {
10231024
$salt = str_replace('.', '', uniqid('', true));
10241025
}
1025-
$service = sprintf('%s implements \\%s { private $container%4$s; private $values%4$s; %s }', $r->name, GetterProxyInterface::class, $this->generateOverriddenGetters($id, $definition, $r, $salt), $salt);
1026+
$service = sprintf('%s implements \\%s { private $container%4$s; private $getters%4$s; %s }', $r->name, InheritanceProxyInterface::class, $this->generateOverriddenMethods($id, $definition, $r, $salt), $salt);
10261027
if (!class_exists($proxyClass = 'SymfonyProxy_'.md5($service), false)) {
10271028
eval(sprintf('class %s extends %s', $proxyClass, $service));
10281029
}
@@ -1032,7 +1033,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10321033
$constructor = null;
10331034
}
10341035
$service = $constructor ? $r->newInstanceWithoutConstructor() : $r->newInstanceArgs($arguments);
1035-
call_user_func(\Closure::bind(function ($c, $v, $s) { $this->{'container'.$s} = $c; $this->{'values'.$s} = $v; }, $service, $service), $this, $definition->getOverriddenGetters(), $salt);
1036+
call_user_func(\Closure::bind(function ($c, $g, $s) { $this->{'container'.$s} = $c; $this->{'getters'.$s} = $g; }, $service, $service), $this, $definition->getOverriddenGetters(), $salt);
10361037
if ($constructor) {
10371038
$constructor->invokeArgs($service, $arguments);
10381039
}
@@ -1294,22 +1295,29 @@ public function getEnvCounters()
12941295
return $this->envCounters;
12951296
}
12961297

1297-
private function generateOverriddenGetters($id, Definition $definition, \ReflectionClass $class, $salt)
1298+
private function generateOverriddenMethods($id, Definition $definition, \ReflectionClass $class, $salt)
12981299
{
12991300
if ($class->isFinal()) {
1300-
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": class "%s" cannot be marked as final.', $id, $class->name));
1301+
throw new RuntimeException(sprintf('Unable to configure service "%s": class "%s" cannot be marked as final.', $id, $class->name));
13011302
}
1303+
1304+
return $this->generateOverriddenGetters($id, $definition, $class, $salt);
1305+
}
1306+
1307+
private function generateOverriddenGetters($id, Definition $definition, \ReflectionClass $class, $salt)
1308+
{
13021309
$getters = '';
1310+
13031311
foreach ($definition->getOverriddenGetters() as $name => $returnValue) {
1304-
$r = self::getGetterReflector($class, $name, $id, $type);
1312+
$r = InheritanceProxyHelper::getGetterReflector($class, $name, $id);
1313+
$signature = InheritanceProxyHelper::getSignature($r);
13051314
$visibility = $r->isProtected() ? 'protected' : 'public';
1306-
$name = var_export($name, true);
13071315
$getters .= <<<EOF
13081316
1309-
{$visibility} function {$r->name}(){$type} {
1317+
{$visibility} function {$signature} {
13101318
\$c = \$this->container{$salt};
13111319
\$b = \$c->getParameterBag();
1312-
\$v = \$this->values{$salt}[{$name}];
1320+
\$v = \$this->getters{$salt}['{$name}'];
13131321
13141322
foreach (\$c->getServiceConditionals(\$v) as \$s) {
13151323
if (!\$c->has(\$s)) {
@@ -1325,71 +1333,6 @@ private function generateOverriddenGetters($id, Definition $definition, \Reflect
13251333
return $getters;
13261334
}
13271335

1328-
/**
1329-
* @internal
1330-
*/
1331-
public static function getGetterReflector(\ReflectionClass $class, $name, $id, &$type)
1332-
{
1333-
if (!$class->hasMethod($name)) {
1334-
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" does not exist.', $id, $class->name, $name));
1335-
}
F438 1336-
$r = $class->getMethod($name);
1337-
if ($r->isPrivate()) {
1338-
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" must be public or protected.', $id, $class->name, $r->name));
1339-
}
1340-
if ($r->isStatic()) {
1341-
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" cannot be static.', $id, $class->name, $r->name));
1342-
}
1343-
if ($r->isFinal()) {
1344-
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" cannot be marked as final.', $id, $class->name, $r->name));
1345-
}
1346-
if ($r->returnsReference()) {
1347-
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" cannot return by reference.', $id, $class->name, $r->name));
1348-
}
1349-
if (0 < $r->getNumberOfParameters()) {
1350-
throw new RuntimeException(sprintf('Unable to configure getter injection for service "%s": method "%s::%s" cannot have any arguments.', $id, $class->name, $r->name));
1351-
}
1352-
if ($type = method_exists($r, 'getReturnType') ? $r->getReturnType() : null) {
1353-
$type = ': '.($type->allowsNull() ? '?' : '').self::generateTypeHint($type, $r);
1354-
}
1355-
1356-
return $r;
1357-
}
1358-
1359-
/**
1360-
* @internal
1361-
*/
1362-
public static function generateTypeHint($type, \ReflectionFunctionAbstract $r)
1363-
{
1364-
if (is_string($type)) {
1365-
$name = $type;
1366-
1367-
if ('callable' === $name || 'array' === $name) {
1368-
return $name;
1369-
}
1370-
} else {
1371-
$name = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
1372-
1373-
if ($type->isBuiltin()) {
1374-
return $name;
1375-
}
1376-
}
1377-
$lcName = strtolower($name);
1378-
1379-
if ('self' !== $lcName && 'parent' !== $lcName) {
1380-
return '\\'.$name;
1381-
}
1382-
if (!$r instanceof \ReflectionMethod) {
1383-
return;
1384-
}
1385-
if ('self' === $lcName) {
1386-
return '\\'.$r->getDeclaringClass()->name;
1387-
}
1388-
if ($parent = $r->getDeclaringClass()->getParentClass()) {
1389-
return '\\'.$parent->name;
1390-
}
1391-
}
1392-
13931336
/**
13941337
* @internal
13951338
*/

0 commit comments

Comments
 (0)
0