Skip to content

Commit 9950b90

Browse files
committed
feature symfony#22356 [DI] Rework config hierarchy: defaults > instanceof > service config (weaverryan, nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Rework config hierarchy: defaults > instanceof > service config | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Replaces symfony#22294. The main change is that ChildDefinition does not have "instanceof" applied anymore. All the complexity of the pass came from the merging logic required to deal with them. But in fact, we'd better not have any such logic and just not apply "instanceof" to ChildDefinition (but have them inherit from their parents with the usual logic). Commits ------- 6d6116b Adding an integration test for the hirarchy of defaults, instanceof, child, parent definitions ab86457 [DI] Rework config hierarchy: defaults > instanceof > service config cbaee55 [DI] Track changes at the "Definition" level
2 parents 44008e8 + 6d6116b commit 9950b90

22 files changed

+575
-442
lines changed

src/Symfony/Component/DependencyInjection/ChildDefinition.php

Lines changed: 8 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@ class ChildDefinition extends Definition
2323
{
2424
private $parent;
2525
private $inheritTags = false;
26-
private $changes = array();
2726

2827
/**
2928
* @param string $parent The id of Definition instance to decorate
3029
*/
3130
public function __construct($parent)
3231
{
33-
parent::__construct();
34-
3532
$this->parent = $parent;
3633
}
3734

@@ -46,13 +43,17 @@ public function getParent()
4643
}
4744

4845
/**
49-
* Returns all changes tracked for the Definition object.
46+
* Sets the Definition being decorated.
47+
*
48+
* @param string $parent
5049
*
51-
* @return array An array of changes for this Definition
50+
* @return $this
5251
*/
53-
public function getChanges()
52+
public function setParent($parent)
5453
{
55-
return $this->changes;
54+
$this->parent = $parent;
55+
56+
return $this;
5657
}
5758

5859
/**
@@ -79,116 +80,6 @@ public function getInheritTags()
7980
return $this->inheritTags;
8081
}
8182

82-
/**
83-
* {@inheritdoc}
84-
*/
85-
public function setClass($class)
86-
{
87-
$this->changes['class'] = true;
88-
89-
return parent::setClass($class);
90-
}
91-
92-
/**
93-
* {@inheritdoc}
94-
*/
95-
public function setFactory($callable)
96-
{
97-
$this->changes['factory'] = true;
98-
99-
return parent::setFactory($callable);
100-
}
101-
102-
/**
103-
* {@inheritdoc}
104-
*/
105-
public function setConfigurator($callable)
106-
{
107-
$this->changes['configurator'] = true;
108-
109-
return parent::setConfigurator($callable);
110-
}
111-
112-
/**
113-
* {@inheritdoc}
114-
*/
115-
public function setFile($file)
116-
{
117-
$this->changes['file'] = true;
118-
119-
return parent::setFile($file);
120-
}
121-
122-
/**
123-
* {@inheritdoc}
124-
*/
125-
public function setShared($boolean)
126-
{
127-
$this->changes['shared'] = true;
128-
129-
return parent::setShared($boolean);
130-
}
131-
132-
/**
133-
* {@inheritdoc}
134-
*/
135-
public function setPublic($boolean)
136-
{
137-
$this->changes['public'] = true;
138-
139-
return parent::setPublic($boolean);
140-
}
141-
142-
/**
143-
* {@inheritdoc}
144-
*/
145-
public function setLazy($boolean)
146-
{
147-
$this->changes['lazy'] = true;
148-
149-
return parent::setLazy($boolean);
150-
}
151-
152-
/**
153-
* {@inheritdoc}
154-
*/
155-
public function setAbstract($boolean)
156-
{
157-
$this->changes['abstract'] = true;
158-
159-
return parent::setAbstract($boolean);
160-
}
161-
162-
/**
163-
* {@inheritdoc}
164-
*/
165-
public function setDecoratedService($id, $renamedId = null, $priority = 0)
166-
{
167-
$this->changes['decorated_service'] = true;
168-
169-
return parent::setDecoratedService($id, $renamedId, $priority);
170-
}
171-
172-
/**
173-
* {@inheritdoc}
174-
*/
175-
public function setDeprecated($boolean = true, $template = null)
176-
{
177-
$this->changes['deprecated'] = true;
178-
179-
return parent::setDeprecated($boolean, $template);
180-
}
181-
182-
/**
183-
* {@inheritdoc}
184-
*/
185-
public function setAutowired($autowired)
186-
{
187-
$this->changes['autowired'] = true;
188-
189-
return parent::setAutowired($autowired);
190-
}
191-
19283
/**
19384
* Gets an argument to pass to the service constructor/factory method.
19485
*

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ protected function processValue($value, $isRoot = false)
6565
$value->setProperties($this->processValue($value->getProperties()));
6666
$value->setMethodCalls($this->processValue($value->getMethodCalls()));
6767

68-
if ($v = $value->getFactory()) {
69-
$value->setFactory($this->processValue($v));
68+
$changes = $value->getChanges();
69+
if (isset($changes['factory'])) {
70+
$value->setFactory($this->processValue($value->getFactory()));
7071
}
71-
if ($v = $value->getConfigurator()) {
72-
$value->setConfigurator($this->processValue($v));
72+
if (isset($changes['configurator'])) {
73+
$value->setConfigurator($this->processValue($value->getConfigurator()));
7374
}
7475
}
7576

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function __construct()
4242
$this->beforeOptimizationPasses = array(
4343
100 => array(
4444
$resolveClassPass = new ResolveClassPass(),
45-
new ResolveDefinitionInheritancePass(),
45+
new ResolveInstanceofConditionalsPass(),
46+
new ResolveTagsInheritancePass(),
4647
),
4748
);
4849

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

Lines changed: 0 additions & 99 deletions
This file was deleted.

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function doResolveDefinition(ChildDefinition $definition)
8484
$def = new Definition();
8585

8686
// merge in parent definition
87-
// purposely ignored attributes: abstract, tags
87+
// purposely ignored attributes: abstract, shared, tags
8888
$def->setClass($parentDef->getClass());
8989
$def->setArguments($parentDef->getArguments());
9090
$def->setMethodCalls($parentDef->getMethodCalls());
@@ -101,27 +101,8 @@ private function doResolveDefinition(ChildDefinition $definition)
101101
$def->setPublic($parentDef->isPublic());
102102
$def->setLazy($parentDef->isLazy());
103103
$def->setAutowired($parentDef->isAutowired());
104+
$def->setChanges($parentDef->getChanges());
104105

105-
self::mergeDefinition($def, $definition);
106-
107-
// merge autowiring types
108-
foreach ($definition->getAutowiringTypes(false) as $autowiringType) {
109-
$def->addAutowiringType($autowiringType);
110-
}
111-
112-
// these attributes are always taken from the child
113-
$def->setAbstract($definition->isAbstract());
114-
$def->setShared($definition->isShared());
115-
$def->setTags($definition->getTags());
116-
117-
return $def;
118-
}
119-
120-
/**
121-
* @internal
122-
*/
123-
public static function mergeDefinition(Definition $def, ChildDefinition $definition)
124-
{
125106
// overwrite with values specified in the decorator
126107
$changes = $definition->getChanges();
127108
if (isset($changes['class'])) {
@@ -148,6 +129,9 @@ public static function mergeDefinition(Definition $def, ChildDefinition $definit
148129
if (isset($changes['autowired'])) {
149130
$def->setAutowired($definition->isAutowired());
150131
}
132+
if (isset($changes['shared'])) {
133+
$def->setShared($definition->isShared());
134+
}
151135
if (isset($changes['decorated_service'])) {
152136
$decoratedService = $definition->getDecoratedService();
153137
if (null === $decoratedService) {
@@ -177,5 +161,16 @@ public static function mergeDefinition(Definition $def, ChildDefinition $definit
177161
if ($calls = $definition->getMethodCalls()) {
178162
$def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
179163
}
164+
165+
// merge autowiring types
166+
foreach ($definition->getAutowiringTypes(false) as $autowiringType) {
167+
$def->addAutowiringType($autowiringType);
168+
}
169+
170+
// these attributes are always taken from the child
171+
$def->setAbstract($definition->isAbstract());
172+
$def->setTags($definition->getTags());
173+
174+
return $def;
180175
}
181176
}

0 commit comments

Comments
 (0)