Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 710d9a8

Browse files
committed
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition
Also, not allowing arguments or method calls for autoconfigure. This is a safety mechanism, since we don't have merging logic. It will allow us to add this in the future if we want to. The reason is that parent-child definitions are a different mechanism for "inheritance" than instanceofConditionas and defaults... creating some edge cases when trying to figure out which settings "win". For example: Suppose a child and parent definitions are defined in different YAML files. The child receives public: false from its _defaults, and the parent receives public: true from its _defaults. Should the final child definition be public: true (so the parent overrides the child, even though it only came from _defaults) or public: false (where the child wins... even though it was only set from its _defaults). Or, if the parent is explicitly set to public: true, should that override the public: false of the child (which it got from its _defaults)? On one hand, the parent is being explicitly set. On the other hand, the child is explicitly in a file settings _defaults public to false. There's no correct answer. There are also problems with instanceof. The importance goes: defaults < instanceof < service definition But how does parent-child relationships fit into that? If a child has public: false from an _instanceof, but the parent explicitly sets public: true, which wins? Should we assume the parent definition wins because it's explicitly set? Or would the _instanceof win, because that's being explicitly applied to the child definition's class by an _instanceof that lives in the same file as that class (whereas the parent definition may live in a different file). Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that the complexity was growing too much. The solution is to not allow any of these new feature to be used by ChildDefinition objects. In other words, when you want some sort of "inheritance" for your service, you should *either* giving your service a parent *or* using defaults and instanceof. And instead of silently not applying defaults and instanceof to child definitions, I think it's better to scream that it's not supported.
1 parent 9d9f628 commit 710d9a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+495
-192
lines changed

src/Symfony/Component/DependencyInjection/ChildDefinition.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection;
1313

14+
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
1415
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1516
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
1617

@@ -134,6 +135,22 @@ public function replaceArgument($index, $value)
134135

135136
return $this;
136137
}
138+
139+
/**
140+
* @internal
141+
*/
142+
public function setAutoconfigured($autoconfigured)
143+
{
144+
throw new BadMethodCallException('A ChildDefinition cannot be autoconfigured.');
145+
}
146+
147+
/**
148+
* @internal
149+
*/
150+
public function setInstanceofConditionals(array $instanceof)
151+
{
152+
throw new BadMethodCallException('A ChildDefinition cannot have instanceof conditionals set on it.');
153+
}
137154
}
138155

139156
class_alias(ChildDefinition::class, DefinitionDecorator::class);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ private function doResolveDefinition(ChildDefinition $definition)
101101
$def->setPublic($parentDef->isPublic());
102102
$def->setLazy($parentDef->isLazy());
103103
$def->setAutowired($parentDef->isAutowired());
104-
$def->setAutoconfigured($parentDef->isAutoconfigured());
105104
$def->setChanges($parentDef->getChanges());
106105

107106
// overwrite with values specified in the decorator
@@ -130,9 +129,6 @@ private function doResolveDefinition(ChildDefinition $definition)
130129
if (isset($changes['autowired'])) {
131130
$def->setAutowired($definition->isAutowired());
132131
}
133-
if (isset($changes['autoconfigured'])) {
134-
$def->setAutoconfigured($definition->isAutoconfigured());
135-
}
136132
if (isset($changes['shared'])) {
137133
$def->setShared($definition->isShared());
138134
}
@@ -174,6 +170,9 @@ private function doResolveDefinition(ChildDefinition $definition)
174170
// these attributes are always taken from the child
175171
$def->setAbstract($definition->isAbstract());
176172
$def->setTags($definition->getTags());
173+
// autoconfigure is never taken from parent (on purpose)
174+
// and it's not legal on an instanceof
175+
$def->setAutoconfigured($definition->isAutoconfigured());
177176

178177
return $def;
179178
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\ChildDefinition;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1718

1819
/**
1920
* Applies instanceof conditionals to definitions.
@@ -27,6 +28,15 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
2728
*/
2829
public function process(ContainerBuilder $container)
2930
{
31+
foreach ($container->getAutoconfiguredInstanceof() as $interface => $definition) {
32+
if ($definition->getArguments()) {
33+
throw new InvalidArgumentException(sprintf('Autoconfigured instanceof for type "%s" defines arguments but these are not supported and should be removed.', $interface));
34+
}
35+
if ($definition->getMethodCalls()) {
36+
throw new InvalidArgumentException(sprintf('Autoconfigured instanceof for type "%s" defines method calls but these are not supported and should be removed.', $interface));
37+
}
38+
}
39+
3040
foreach ($container->getDefinitions() as $id => $definition) {
3141
if ($definition instanceof ChildDefinition) {
3242
// don't apply "instanceof" to children: it will be applied to their parent
@@ -39,17 +49,17 @@ public function process(ContainerBuilder $container)
3949
private function processDefinition(ContainerBuilder $container, $id, Definition $definition)
4050
{
4151
$instanceofConditionals = $definition->getInstanceofConditionals();
42-
$automaticInstanceofConditionals = $definition->isAutoconfigured() ? $container->getAutomaticInstanceofDefinitions() : array();
52+
$autoconfiguredInstanceof = $definition->isAutoconfigured() ? $container->getAutoconfiguredInstanceof() : array();
4353

44-
if (!$instanceofConditionals && !$automaticInstanceofConditionals) {
54+
if (!$instanceofConditionals && !$autoconfiguredInstanceof) {
4555
return $definition;
4656
}
4757

4858
if (!$class = $container->getParameterBag()->resolveValue($definition->getClass())) {
4959
return $definition;
5060
}
5161

52-
$conditionals = $this->mergeConditionals($automaticInstanceofConditionals, $instanceofConditionals);
62+
$conditionals = $this->mergeConditionals($autoconfiguredInstanceof, $instanceofConditionals);
5363

5464
$definition->setInstanceofConditionals(array());
5565
$parent = $shared = null;
@@ -113,13 +123,13 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
113123
return $definition;
114124
}
115125

116-
private function mergeConditionals(array $automaticInstanceofConditionals, array $instanceofConditionals)
126+
private function mergeConditionals(array $autoconfiguredInstanceof, array $instanceofConditionals)
117127
{
118128
// make each value an array of ChildDefinition
119-
$conditionals = array_map(function($childDef) { return array($childDef); }, $automaticInstanceofConditionals);
129+
$conditionals = array_map(function ($childDef) { return array($childDef); }, $autoconfiguredInstanceof);
120130

121131
foreach ($instanceofConditionals as $interface => $instanceofDef) {
122-
if (!isset($automaticInstanceofConditionals[$interface])) {
132+
if (!isset($autoconfiguredInstanceof[$interface])) {
123133
$conditionals[$interface] = array();
124134
}
125135

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
118118
*/
119119
private $vendors;
120120

121-
private $automaticInstanceofDefinitions = array();
121+
private $autoconfiguredInstanceof = array();
122122

123123
public function __construct(ParameterBagInterface $parameterBag = null)
124124
{
@@ -641,12 +641,12 @@ public function merge(ContainerBuilder $container)
641641
}
642642
}
643643

644-
foreach ($container->getAutomaticInstanceofDefinitions() as $interface => $childDefinition) {
645-
if (isset($this->automaticInstanceofDefinitions[$interface])) {
644+
foreach ($container->getAutoconfiguredInstanceof() as $interface => $childDefinition) {
645+
if (isset($this->autoconfiguredInstanceof[$interface])) {
646646
throw new InvalidArgumentException(sprintf('"%s" has already been autoconfigured and merge() does not support merging autoconfiguration for the same class/interface.', $interface));
647647
}
648648

649-
$this->automaticInstanceofDefinitions[$interface] = $childDefinition;
649+
$this->autoconfiguredInstanceof[$interface] = $childDefinition;
650650
}
651651
}
652652

@@ -1272,25 +1272,26 @@ public function getExpressionLanguageProviders()
12721272
* Returns a ChildDefinition that will be used for autoconfiguring the interface/class.
12731273
*
12741274
* @param string $interface The class or interface to match
1275+
*
12751276
* @return ChildDefinition
12761277
*/
12771278
public function registerForAutoconfiguration($interface)
12781279
{
1279-
if (!isset($this->automaticInstanceofDefinitions[$interface])) {
1280-
$this->automaticInstanceofDefinitions[$interface] = new ChildDefinition('');
1280+
if (!isset($this->autoconfiguredInstanceof[$interface])) {
1281+
$this->autoconfiguredInstanceof[$interface] = new ChildDefinition('');
12811282
}
12821283

1283-
return $this->automaticInstanceofDefinitions[$interface];
1284+
return $this->autoconfiguredInstanceof[$interface];
12841285
}
12851286

12861287
/**
12871288
* Returns an array of ChildDefinition[] keyed by interface.
12881289
*
12891290
* @return ChildDefinition[]
12901291
*/
1291-
public function getAutomaticInstanceofDefinitions()
1292+
public function getAutoconfiguredInstanceof()
12921293
{
1293-
return $this->automaticInstanceofDefinitions;
1294+
return $this->autoconfiguredInstanceof;
12941295
}
12951296

12961297
/**

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected function setDefinition($id, Definition $definition)
7979
}
8080
$this->instanceof[$id] = $definition;
8181
} else {
82-
$this->container->setDefinition($id, $definition->setInstanceofConditionals($this->instanceof));
82+
$this->container->setDefinition($id, $definition instanceof ChildDefinition ? $definition : $definition->setInstanceofConditionals($this->instanceof));
8383
}
8484
}
8585

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
215215
if ($this->isLoadingInstanceof) {
216216
$definition = new ChildDefinition('');
217217
} elseif ($parent = $service->getAttribute('parent')) {
218+
if (!empty($this->instanceof)) {
219+
throw new InvalidArgumentException(sprintf('The service "%s" cannot use the "parent" option in the same file where "instanceof" configuration is defined as using both is not supported. Try moving your child definitions to a different file.', $service->getAttribute('id')));
220+
}
221+
222+
if (!empty($defaults)) {
223+
throw new InvalidArgumentException(sprintf('The service "%s" cannot use the "parent" option in the same file where "defaults" configuration is defined as using both is not supported. Try moving your child definitions to a different file.', $service->getAttribute('id')));
224+
}
225+
218226
$definition = new ChildDefinition($parent);
219227

220228
if ($value = $service->getAttribute('inherit-tags')) {
@@ -255,7 +263,11 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
255263
}
256264

257265
if ($value = $service->getAttribute('autoconfigure')) {
258-
$definition->setAutoconfigured(XmlUtils::phpize($value));
266+
if (!$definition instanceof ChildDefinition) {
267+
$definition->setAutoconfigured(XmlUtils::phpize($value));
268+
} elseif ($value = XmlUtils::phpize($value)) {
269+
throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try setting autoconfigure="false" for the service.', $service->getAttribute('id')));
270+
}
259271
}
260272

261273
if ($files = $this->getChildren($service, 'file')) {

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ class YamlFileLoader extends FileLoader
8888
'calls' => 'calls',
8989
'tags' => 'tags',
9090
'autowire' => 'autowire',
91-
'autoconfigure' => 'autoconfigure',
9291
);
9392

9493
private static $defaultsKeywords = array(
@@ -357,6 +356,14 @@ private function parseDefinition($id, $service, $file, array $defaults)
357356
if ($this->isLoadingInstanceof) {
358357
$definition = new ChildDefinition('');
359358
} elseif (isset($service['parent'])) {
359+
if (!empty($this->instanceof)) {
360+
throw new InvalidArgumentException(sprintf('The service "%s" cannot use the "parent" option in the same file where "_instanceof" configuration is defined as using both is not supported. Try moving your child definitions to a different file.', $id));
361+
}
362+
363+
if (!empty($defaults)) {
364+
throw new InvalidArgumentException(sprintf('The service "%s" cannot use the "parent" option in the same file where "_defaults" configuration is defined as using both is not supported. Try moving your child definitions to a different file.', $id));
365+
}
366+
360367
$definition = new ChildDefinition($service['parent']);
361368

362369
$inheritTag = isset($service['inherit_tags']) ? $service['inherit_tags'] : (isset($defaults['inherit_tags']) ? $defaults['inherit_tags'] : null);
@@ -518,7 +525,11 @@ private function parseDefinition($id, $service, $file, array $defaults)
518525
}
519526

520527
if (isset($service['autoconfigure'])) {
521-
$definition->setAutoconfigured($service['autoconfigure']);
528+
if (!$definition instanceof ChildDefinition) {
529+
$definition->setAutoconfigured($service['autoconfigure']);
530+
} elseif ($service['autoconfigure']) {
531+
throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try setting "autoconfigure: false" for the service.', $id));
532+
}
522533
}
523534

524535
if (array_key_exists('resource', $service)) {

src/Symfony/Component/DependencyInjection/Tests/ChildDefinitionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,22 @@ public function testDefinitionDecoratorAliasExistsForBackwardsCompatibility()
132132
{
133133
$this->assertInstanceOf(ChildDefinition::class, new DefinitionDecorator('foo'));
134134
}
135+
136+
/**
137+
* @expectedException \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
138+
*/
139+
public function testCannotCallSetAutoconfigured()
140+
{
141+
$def = new ChildDefinition('foo');
142+
$def->setAutoconfigured(true);
143+
}
144+
145+
/**
146+
* @expectedException \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
147+
*/
148+
public function testCannotCallSetInstanceofConditionals()
149+
{
150+
$def = new ChildDefinition('foo');
151+
$def->setInstanceofConditionals(array('Foo' => new ChildDefinition('')));
152+
}
135153
}

0 commit comments

Comments
 (0)