10000 feature #22158 Revert "feature #20973 [DI] Add getter injection (nico… · symfony/symfony@9896f86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9896f86

Browse files
committed
feature #22158 Revert "feature #20973 [DI] Add getter injection (nicolas-grekas)" (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- Revert "feature #20973 [DI] Add getter injection (nicolas-grekas)" This reverts commit 2183f98, reversing changes made to b465634. | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no (master only) | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Let's remove getter injection, we now have enough alternative mechanisms to achieve almost the same results (e.g. `ServiceSubscriberInterface`, see #21708)., and I'm tired being called by names because of it. The only use case in core is `ControllerTrait`, but this should be gone if #22157 is merged. Commits ------- 23fa3a0 Revert "feature #20973 [DI] Add getter injection (nicolas-grekas)"
2 parents 3306dc2 + 23fa3a0 commit 9896f86

38 files changed

+18
-1274
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ CHANGELOG
1414
* deprecated `ContainerBuilder::getClassResource()`, use `ContainerBuilder::getReflectionClass()` or `ContainerBuilder::addObjectResource()` instead
1515
* added `ContainerBuilder::fileExists()` for checking and tracking file or directory existence
1616
* deprecated autowiring-types, use aliases instead
17-
* [EXPERIMENTAL] added support for getter autowiring
18-
* [EXPERIMENTAL] added support for getter-injection
1917
* added support for omitting the factory class name in a service definition if the definition class is set
2018
* deprecated case insensitivity of service identifiers
2119
* added "iterator" argument type for lazy iteration over a set of values and services

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ protected function processValue($value, $isRoot = false)
6161
} elseif ($value instanceof Definition) {
6262
$value->setArguments($this->processValue($value->getArguments()));
6363
$value->setProperties($this->processValue($value->getProperties()));
64-
$value->setOverriddenGetters($this->processValue($value->getOverriddenGetters()));
6564
$value->setMethodCalls($this->processValue($value->getMethodCalls()));
6665

6766
if ($v = $value->getFactory()) {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ protected function processValue($value, $isRoot = false)
109109

110110
if (!$this->onlyConstructorArguments) {
111111
$this->processValue($value->getProperties());
112-
$this->lazy = true;
113-
$this->processValue($value->getOverriddenGetters());
114-
$this->lazy = false;
115112
$this->processValue($value->getMethodCalls());
116113
$this->processValue($value->getConfigurator());
117114
}

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

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18-
use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyHelper;
18+
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\DependencyInjection\TypedReference;
2121

@@ -125,7 +125,6 @@ protected function processValue($value, $isRoot = false)
125125
}
126126

127127
$methodCalls = $this->autowireCalls($reflectionClass, $methodCalls, $autowiredMethods);
128-
$overriddenGetters = $this->autowireOverridenGetters($value->getOverriddenGetters(), $autowiredMethods);
129128

130129
if ($constructor) {
131130
list(, $arguments) = array_shift($methodCalls);
@@ -139,10 +138,6 @@ protected function processValue($value, $isRoot = false)
139138
$value->setMethodCalls($methodCalls);
140139
}
141140

142-
if ($overriddenGetters !== $value->getOverriddenGetters()) {
143-
$value->setOverriddenGetters($overriddenGetters);
144-
}
145-
146141
return parent::processValue($value, $isRoot);
147142
} finally {
148143
$this->currentDefinition = $parentDefinition;
@@ -165,7 +160,7 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass)
165160
$methodsToAutowire[strtolower($reflectionMethod->name)] = $reflectionMethod;
166161
}
167162

168-
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $reflectionMethod) {
163+
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
169164
$r = $reflectionMethod;
170165

171166
while (true) {
@@ -224,9 +219,6 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC
224219
}
225220

226221
foreach ($autowiredMethods as $lcMethod => $reflectionMethod) {
227-
if (!$reflectionMethod->getNumberOfParameters()) {
228-
continue; // skip getters
229-
}
230222
$method = $reflectionMethod->name;
231223

232224
if (!$reflectionMethod->isPublic()) {
@@ -270,7 +262,7 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
270262
continue;
271263
}
272264

273-
$type = InheritanceProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
265+
$type = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
274266

275267
if (!$type) {
276268
// no default value? Then fail
@@ -311,41 +303,6 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
311303
return $arguments;
312304
}
313305

314-
/**
315-
* Autowires getters.
316-
*
317-
* @param array $overridenGetters
318-
* @param array $autowiredMethods
319-
*
320-
* @return array
321-
*/
322-
private function autowireOverridenGetters(array $overridenGetters, array $autowiredMethods)
323-
{
324-
foreach ($autowiredMethods as $lcMethod => $reflectionMethod) {
325-
if (isset($overridenGetters[$lcMethod]) || $reflectionMethod->getNumberOfParameters() || $reflectionMethod->isConstructor()) {
326-
continue;
327-
}
328-
$class = $reflectionMethod->class;
329-
$method = $reflectionMethod->name;
330-
331-
if (!$type = InheritanceProxyHelper::getTypeHint($reflectionMethod, null, true)) {
332-
$type = InheritanceProxyHelper::getTypeHint($reflectionMethod);
333-
334-
throw new RuntimeException(sprintf('Cannot autowire service "%s": getter %s() must%s have its return value be configured explicitly.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $type ? '' : ' have a return-type hint or'));
335-
}
336-
337-
if (!$typeRef = $this->getAutowiredReference($type)) {
338-
$this->container->log($this, $this->createTypeNotFoundMessage($type, sprintf('return value of method %s()', $class !== $this->currentId ? $class.'::'.$method : $method)));
339-
continue;
340-
}
341-
342-
$overridenGetters[$lcMethod] = $typeRef;
343-
$this->usedTypes[$type] = $this->currentId;
344-
}
345-
346-
return $overridenGetters;
347-
}
348-
349306
/**
350307
* @return Reference|null A reference to the service matching the given type, if any
351308
*/

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ private function doResolveDefinition(ChildDefinition $definition)
8888
$def->setClass($parentDef->getClass());
8989
$def->setArguments($parentDef->getArguments());
9090
$def->setMethodCalls($parentDef->getMethodCalls());
91-
$def->setOverriddenGetters($parentDef->getOverriddenGetters());
9291
$def->setProperties($parentDef->getProperties());
9392
if ($parentDef->getAutowiringTypes(false)) {
9493
$def->setAutowiringTypes($parentDef->getAutowiringTypes(false));
@@ -183,10 +182,5 @@ public static function mergeDefinition(Definition $def, ChildDefinition $definit
183182
if ($calls = $definition->getMethodCalls()) {
184183
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
185184
}
186-
187-
// merge overridden getters
188-
foreach ($definition->getOverriddenGetters() as $k => $v) {
189-
$def->setOverriddenGetter($k, $v);
190-
}
191185
}
192186
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ private function processValue($value, $rootLevel = 0, $level = 0)
6464
}
6565
$value->setArguments($this->processValue($value->getArguments(), 0));
6666
$value->setProperties($this->processValue($value->getProperties(), 1));
67-
$value->setOverriddenGetters($this->processValue($value->getOverriddenGetters(), 1));
6867
$value->setMethodCalls($this->processValue($value->getMethodCalls(), 2));
6968
} elseif (is_array($value)) {
7069
$i = 0;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public function process(ContainerBuilder $container)
4242

4343
$definition->setArguments($this->processArguments($definition->getArguments()));
4444
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
45-
$definition->setOverriddenGetters($this->processArguments($definition->getOverriddenGetters()));
4645
$definition->setProperties($this->processArguments($definition->getProperties()));
4746
$definition->setFactory($this->processFactory($definition->getFactory()));
4847
}

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
use Symfony\Component\Config\Resource\ResourceInterface;
3838
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
3939
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
40-
use Symfony\Co B93C mponent\DependencyInjection\LazyProxy\InheritanceProxyHelper;
41-
use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyInterface;
4240
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
4341
use Symfony\Component\ExpressionLanguage\Expression;
4442
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
@@ -1042,9 +1040,6 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10421040
$arguments = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())));
10431041

10441042
if (null !== $factory = $definition->getFactory()) {
1045-
if ($definition->getOverriddenGetters()) {
1046-
throw new RuntimeException(sprintf('Cannot create service "%s": factories and overridden getters are incompatible with each other.', $id));
1047-
}
10481043
if (is_array($factory)) {
10491044
$factory = array($this->resolveServices($parameterBag->resolveValue($factory[0])), $factory[1]);
10501045
} elseif (!is_string($factory)) {
@@ -1063,31 +1058,11 @@ private function createService(Definition $definition, $id, $tryProxy = true)
10631058
} else {
10641059
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));
10651060

1061+
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
1062+
10661063
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
10671064
@trigger_error(sprintf('The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name), E_USER_DEPRECATED);
10681065
}
1069-
if ($definition->getOverriddenGetters()) {
1070-
static $salt;
1071-
if (null === $salt) {
1072-
$salt = str_replace('.', '', uniqid('', true));
1073-
}
1074-
$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);
1075-
if (!class_exists($proxyClass = 'SymfonyProxy_'.md5($service), false)) {
1076-
eval(sprintf('class %s extends %s', $proxyClass, $service));
1077-
}
1078-
$r = new \ReflectionClass($proxyClass);
1079-
$constructor = $r->getConstructor();
1080-
if ($constructor && !defined('HHVM_VERSION') && $constructor->getDeclaringClass()->isInternal()) {
1081-
$constructor = null;
1082-
}
1083-
$service = $constructor ? $r->newInstanceWithoutConstructor() : $r->newInstanceArgs($arguments);
1084-
call_user_func(\Closure::bind(function ($c, $g, $s) { $this->{'container'.$s} = $c; $this->{'getters'.$s} = $g; }, $service, $service), $this, $definition->getOverriddenGetters(), $salt);
1085-
if ($constructor) {
1086-
$constructor->invokeArgs($service, $arguments);
1087-
}
1088-
} else {
1089-
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
1090-
}
10911066
}
10921067

10931068
if ($tryProxy || !$definition->isLazy()) {
@@ -1348,44 +1323,6 @@ public function getEnvCounters()
13481323
return $this->envCounters;
13491324
}
13501325

1351-
private function generateOverriddenMethods($id, Definition $definition, \ReflectionClass $class, $salt)
1352-
{
1353-
if ($class->isFinal()) {
1354-
throw new RuntimeException(sprintf('Unable to configure service "%s": class "%s" cannot be marked as final.', $id, $class->name));
1355-
}
1356-
1357-
return $this->generateOverriddenGetters($id, $definition, $class, $salt);
1358-
}
1359-
1360-
private function generateOverriddenGetters($id, Definition $definition, \ReflectionClass $class, $salt)
1361-
{
1362-
$getters = '';
1363-
1364-
foreach ($definition->getOverriddenGetters() as $name => $returnValue) {
1365-
$r = InheritanceProxyHelper::getGetterReflector($class, $name, $id);
1366-
$signature = InheritanceProxyHelper::getSignature($r);
1367-
$visibility = $r->isProtected() ? 'protected' : 'public';
1368-
$getters .= <<<EOF
1369-
1370-
{$visibility} function {$signature} {
1371-
\$c = \$this->container{$salt};
1372-
\$b = \$c->getParameterBag();
1373-
\$v = \$this->getters{$salt}['{$name}'];
1374-
1375-
foreach (\$c->getServiceConditionals(\$v) as \$s) {
1376-
if (!\$c->has(\$s)) {
1377-
return parent::{$r->name}();
1378-
}
1379-
}
1380-
1381-
return \$c->resolveServices(\$b->unescapeValue(\$b->resolveValue(\$v)));
1382-
}
1383-
EOF;
1384-
}
1385-
1386-
return $getters;
1387-
}
1388-
13891326
/**
13901327
* @internal
13911328
*/

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class Definition
2929
private $deprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
3030
private $properties = array();
3131
private $calls = array();
32-
private $getters = array();
3332
private $instanceof = array();
3433
private $configurator;
3534
private $tags = array();
@@ -329,41 +328,6 @@ public function getMethodCalls()
329328
return $this->calls;
330329
}
331330

332-
/**
333-
* @return $this
334-
*
335-
* @experimental in version 3.3
336-
*/
337-
public function setOverriddenGetter($name, $returnValue)
338-
{
339-
if (!$name) {
340-
throw new InvalidArgumentException(sprintf('Getter name cannot be empty.'));
341-
}
342-
$this->getters[strtolower($name)] = $returnValue;
343-
344-
return $this;
345-
}
346-
347-
/**
348-
* @return $this
349-
*
350-
* @experimental in version 3.3
351-
*/
352-
public function setOverriddenGetters(array $getters)
353-
{
354-
$this->getters = array_change_key_case($getters, CASE_LOWER);
355-
356-
return $this;
357-
}
358-
359-
/**
360-
* @experimental in version 3.3
361-
*/
362-
public function getOverriddenGetters()
363-
{
364-
return $this->getters;
365-
}
366-
367331
/**
368332
* Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
369333
*

src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ public function dump(array $options = array())
8080
$this->findEdges($id, $call[1], false, $call[0].'()')
8181
);
8282
}
83-
84-
foreach ($definition->getOverriddenGetters() as $name => $value) {
85-
$this->edges[$id] = array_merge(
86-
$this->edges[$id],
87-
$this->findEdges($id, $value, false, $name.'()')
88-
);
89-
}
9083
}
9184

9285
return $this->container->resolveEnvPlaceholders($this->startDot().$this->addNodes().$this->addEdges().$this->endDot(), '__ENV_%s__');

0 commit comments

Comments
 (0)
0