10000 minor #21561 [DI] Refacto / cleanup / minor fixes (nicolas-grekas) · symfony/symfony@2697cee · GitHub
[go: up one dir, main page]

Skip to content

Commit 2697cee

Browse files
committed
minor #21561 [DI] Refacto / cleanup / minor fixes (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Refacto / cleanup / minor fixes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - An almost neutral refacto touching only new things in master - unlocking some next steps. - renames "autowire method" to "autowire call" - renames `GetterProxyInterface` to `InheritanceProxyInterface` - moves some helpers in a new internal `InheritanceProxyHelper` - some other minor things Commits ------- ad5b5b5 [DI] Refacto / cleanup / minor fixes
2 parents 554b1a7 + ad5b5b5 commit 2697cee

25 files changed

+334
-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: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
*/
2525
class AutowirePass extends AbstractRecursivePass
2626
{
27+
/**
28+
* @internal
29+
*/
30+
const MODE_REQUIRED = 1;
31+
32+
/**
33+
* @internal
34+
*/
35+
const MODE_OPTIONAL = 2;
36+
2737
private $definedTypes = array();
2838
private $types;
2939
private $ambiguousServiceTypes = array();
@@ -72,15 +82,15 @@ public static function createResourceForClass(\ReflectionClass $reflectionClass)
7282
*/
7383
protected function processValue($value, $isRoot = false)
7484
{
75-
if (!$value instanceof Definition || !$autowiredMethods = $value->getAutowiredMethods()) {
85+
if (!$value instanceof Definition || !$value->getAutowiredCalls()) {
7686
return parent::processValue($value, $isRoot);
7787
}
7888

7989
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass())) {
8090
return parent::processValue($value, $isRoot);
8191
}
8292

83-
$autowiredMethods = $this->getMethodsToAutowire($reflectionClass, $autowiredMethods);
93+
$autowiredMethods = $this->getMethodsToAutowire($reflectionClass, $value->getAutowiredCalls());
8494
$methodCalls = $value->getMethodCalls();
8595

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

92-
$methodCalls = $this->autowireMethodCalls($reflectionClass, $methodCalls, $autowiredMethods);
102+
$methodCalls = $this->autowireCalls($reflectionClass, $methodCalls, $autowiredMethods);
93103
$overriddenGetters = $this->autowireOverridenGetters($value->getOverriddenGetters(), $autowiredMethods);
94104

95105
if ($constructor) {
@@ -125,6 +135,14 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
125135
$regexList = array();
126136
$methodsToAutowire = array();
127137

138+
if ($reflectionMethod = $reflectionClass->getConstructor()) {
139+
$methodsToAutowire[$lcMethod = strtolower($reflectionMethod->name)] = $reflectionMethod;
140+
unset($autowiredMethods['__construct']);
141+
}
142+
if (!$autowiredMethods) {
143+
return $methodsToAutowire;
144+
}
145+
128146
foreach ($autowiredMethods as $pattern) {
129147
$regexList[] = '/^'.str_replace('\*', '.*', preg_quote($pattern, '/')).'$/i';
130148
}
@@ -141,10 +159,6 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
141159
continue 2;
142160
}
143161
}
144-
145-
if ($reflectionMethod->isConstructor()) {
146-
$methodsToAutowire[strtolower($reflectionMethod->name)] = $reflectionMethod;
147-
}
148162
}
149163

150164
if ($notFound = array_diff($autowiredMethods, $found)) { E377
@@ -161,13 +175,10 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
161175
*
162176
* @return array
163177
*/
164-
private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $methodCalls, array $autowiredMethods)
178+
private function autowireCalls(\ReflectionClass $reflectionClass, array $methodCalls, array $autowiredMethods)
165179
{
166-
$parameterBag = $this->container->getParameterBag();
167-
168180
foreach ($methodCalls as $i => $call) {
169181
list($method, $arguments) = $call;
170-
$method = $parameterBag->resolveValue($method);
171182

172183
if (isset($autowiredMethods[$lcMethod = strtolower($method)]) && $autowiredMethods[$lcMethod]->isPublic()) {
173184
$reflectionMethod = $autowiredMethods[$lcMethod];
@@ -182,15 +193,15 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
182193
}
183194
}
184195

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

187198
if ($arguments !== $call[1]) {
188199
$methodCalls[$i][1] = $arguments;
189200
}
190201
}
191202

192-
foreach ($autowiredMethods as $reflectionMethod) {
193-
if ($reflectionMethod->isPublic() && $arguments = $this->autowireMethodCall($reflectionMethod, array(), false)) {
203+
foreach ($autowiredMethods as $lcMethod => $reflectionMethod) {
204+
if ($reflectionMethod->isPublic() && $arguments = $this->autowireMethod($reflectionMethod, array(), self::MODE_OPTIONAL)) {
194205
$methodCalls[] = array($reflectionMethod->name, $arguments);
195206
}
196207
}
@@ -203,19 +214,22 @@ private function autowireMethodCalls(\ReflectionClass $reflectionClass, array $m
203214
*
204215
* @param \ReflectionMethod $reflectionMethod
205216
* @param array $arguments
206-
* @param bool $mustAutowire
217+
* @param int $mode
207218
*
208219
* @return array The autowired arguments
209220
*
210221
* @throws RuntimeException
211222
*/
212-
private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $arguments, $mustAutowire)
223+
private function autowireMethod(\ReflectionMethod $reflectionMethod, array $arguments, $mode)
213224
{
214225
$didAutowire = false; // Whether any arguments have been autowired or not
215226
foreach ($reflectionMethod->getParameters() as $index => $parameter) {
216227
if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) {
217228
continue;
218229
}
230+
if (method_exists($parameter, 'isVariadic') && $parameter->isVariadic()) {
231+
continue;
232+
}
219233

220234
if (method_exists($parameter, 'getType')) {
221235
if ($typeName = $parameter->getType()) {
@@ -228,7 +242,7 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
228242
if (!$typeName) {
229243
// no default value? Then fail
230244
if (!$parameter->isOptional()) {
231-
if ($mustAutowire) {
245+
if (self::MODE_REQUIRED === $mode) {
232246
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));
233247
}
234248

@@ -268,7 +282,7 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
268282
$value = $parameter->getDefaultValue();
269283
} else {
270284
// 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) {
285+
if (1 === $e->getCode() || self::MODE_REQUIRED === $mode) {
272286
throw $e;
273287
}
274288

@@ -279,7 +293,7 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
279293
// Typehint against a non-existing class
280294

281295
if (!$parameter->isDefaultValueAvailable()) {
282-
if ($mustAutowire) {
296+
if (self::MODE_REQUIRED === $mode) {
283297
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));
284298
}
285299

@@ -292,7 +306,7 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
292306
$arguments[$index] = $value;
293307
}
294308

295-
if (!$mustAutowire && !$didAutowire) {
309+
if (self::MODE_REQUIRED !== $mode && !$didAutowire) {
296310
return array();
297311
}
298312

@@ -313,8 +327,8 @@ private function autowireMethodCall(\ReflectionMethod $reflectionMethod, array $
313327
*/
314328
private function autowireOverridenGetters(array $overridenGetters, array $autowiredMethods)
315329
{
316-
foreach ($autowiredMethods as $reflectionMethod) {
317-
if (isset($overridenGetters[strtolower($reflectionMethod->name)])
330+
foreach ($autowiredMethods as $lcMethod => $reflectionMethod) {
331+
if (isset($overridenGetters[$lcMethod])
318332
|| !method_exists($reflectionMethod, 'getReturnType')
319333
|| 0 !== $reflectionMethod->getNumberOfParameters()
320334
|| $reflectionMethod->isFinal()
@@ -326,7 +340,7 @@ private function autowireOverridenGetters(array $overridenGetters, array $autowi
326340
$typeName = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType->__toString();
327341

328342
if ($this->container->has($typeName) && !$this->container->findDefinition($typeName)->isAbstract()) {
329-
$overridenGetters[$reflectionMethod->name] = new Reference($typeName);
343+
$overridenGetters[$lcMethod] = new Reference($typeName);
330344
continue;
331345
}
332346

@@ -346,7 +360,7 @@ private function autowireOverridenGetters(array $overridenGetters, array $autowi
346360
continue;
347361
}
348362

349-
$overridenGetters[$reflectionMethod->name] = $value;
363+
$overridenGetters[$lcMethod] = $value;
350364
}
351365

352366
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-
}
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