Skip to content

Commit cbaee55

Browse files
weaverryannicolas-grekas
authored andcommitted
[DI] Track changes at the "Definition" level
1 parent bc93526 commit cbaee55

13 files changed

+150
-166
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/ResolveParameterPlaceHoldersPass.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ protected function processValue($value, $isRoot = false)
5858
return $this->bag->resolveValue($value);
5959
}
6060
if ($value instanceof Definition) {
61-
$value->setClass($this->bag->resolveValue($value->getClass()));
62-
$value->setFile($this->bag->resolveValue($value->getFile()));
61+
$changes = $value->getChanges();
62+
if (isset($changes['class'])) {
63+
$value->setClass($this->bag->resolveValue($value->getClass()));
64+
}
65+
if (isset($changes['file'])) {
66+
$value->setFile($this->bag->resolveValue($value->getFile()));
67+
}
6368
$value->setProperties($this->bag->resolveValue($value->getProperties()));
6469
$value->setMethodCalls($this->bag->resolveValue($value->getMethodCalls()));
6570
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public function process(ContainerBuilder $container)
4343
$definition->setArguments($this->processArguments($definition->getArguments()));
4444
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
4545
$definition->setProperties($this->processArguments($definition->getProperties()));
46-
$definition->setFactory($this->processFactory($definition->getFactory()));
46+
if (isset($definition->getChanges()['factory'])) {
47+
$definition->setFactory($this->processFactory($definition->getFactory()));
48+
}
4749
}
4850

4951
foreach ($container->getAliases() as $id => $alias) {

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,44 @@ class Definition
3939
private $decoratedService;
4040
private $autowired = false;
4141
private $autowiringTypes = array();
42+
private $changes = array();
4243

43-
protected $arguments;
44+
protected $arguments = array();
4445

4546
/**
4647
* @param string|null $class The service class
4748
* @param array $arguments An array of arguments to pass to the service constructor
4849
*/
4950
public function __construct($class = null, array $arguments = array())
5051
{
51-
$this->class = $class;
52+
if (null !== $class) {
53+
$this->setClass($class);
54+
}
5255
$this->arguments = $arguments;
5356
}
5457

58+
/**
59+
* Returns all changes tracked for the Definition object.
60+
*
61+
* @return array An array of changes for this Definition
62+
*/
63+
public function getChanges()
64+
{
65+
return $this->changes;
66+
}
67+
68+
/**
69+
* Sets the tracked changes for the Definition object.
70+
*
71+
* @return $this
72+
*/
73+
public function setChanges(array $changes)
74+
{
75+
$this->changes = $changes;
76+
77+
return $this;
78+
}
79+
5580
/**
5681
* Sets a factory.
5782
*
@@ -61,6 +86,8 @@ public function __construct($class = null, array $arguments = array())
6186
*/
6287
public function setFactory($factory)
6388
{
89+
$this->changes['factory'] = true;
90+
6491
if (is_string($factory) && strpos($factory, '::') !== false) {
6592
$factory = explode('::', $factory, 2);
6693
}
@@ -97,6 +124,8 @@ public function setDecoratedService($id, $renamedId = null, $priority = 0)
97124
throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
98125
}
99126

127+
$this->changes['decorated_service'] = true;
128+
100129
if (null === $id) {
101130
$this->decoratedService = null;
102131
} else {
@@ -125,6 +154,8 @@ public function getDecoratedService()
125154
*/
126155
public function setClass($class)
127156
{
157+
$this->changes['class'] = true;
158+
128159
$this->class = $class;
129160

130161
return $this;
@@ -448,6 +479,8 @@ public function clearTags()
448479
*/
449480
public function setFile($file)
450481
{
482+
$this->changes['file'] = true;
483+
451484
$this->file = $file;
452485

453486
return $this;
@@ -472,6 +505,8 @@ public function getFile()
472505
*/
473506
public function setShared($shared)
474507
{
508+
$this->changes['shared'] = true;
509+
475510
$this->shared = (bool) $shared;
476511

477512
return $this;
@@ -496,6 +531,8 @@ public function isShared()
496531
*/
497532
public function setPublic($boolean)
498533
{
534+
$this->changes['public'] = true;
535+
499536
$this->public = (bool) $boolean;
500537

501538
return $this;
@@ -520,6 +557,8 @@ public function isPublic()
520557
*/
521558
public function setLazy($lazy)
522559
{
560+
$this->changes['lazy'] = true;
561+
523562
$this->lazy = (bool) $lazy;
524563

525564
return $this;
@@ -612,6 +651,8 @@ public function setDeprecated($status = true, $template = null)
612651
$this->deprecationTemplate = $template;
613652
}
614653

654+
$this->changes['deprecated'] = true;
655+
615656
$this->deprecated = (bool) $status;
616657

617658
return $this;
@@ -649,6 +690,8 @@ public function getDeprecationMessage($id)
649690
*/
650691
public function setConfigurator($configurator)
651692
{
693+
$this->changes['configurator'] = true;
694+
652695
if (is_string($configurator) && strpos($configurator, '::') !== false) {
653696
$configurator = explode('::', $configurator, 2);
654697
}
@@ -709,6 +752,8 @@ public function isAutowired()
709752
*/
710753
public function setAutowired($autowired)
711754
{
755+
$this->changes['autowired'] = true;
756+
712757
$this->autowired = (bool) $autowired;
713758

714759
return $this;

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,19 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
222222
$defaults = array();
223223
} else {
224224
$definition = new Definition();
225+
226+
if (isset($defaults['public'])) {
227+
$definition->setPublic($defaults['public']);
228+
}
229+
if (isset($defaults['autowire'])) {
230+
$definition->setAutowired($defaults['autowire']);
231+
}
232+
233+
$definition->setChanges(array());
225234
}
226235

227236
if ($publicAttr = $service->getAttribute('public')) {
228237
$definition->setPublic(XmlUtils::phpize($publicAttr));
229-
} elseif (isset($defaults['public'])) {
230-
$definition->setPublic($defaults['public']);
231238
}
232239

233240
foreach (array('class', 'shared', 'synthetic', 'lazy', 'abstract') as $key) {
@@ -239,8 +246,6 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults =
239246

240247
if ($value = $service->getAttribute('autowire')) {
241248
$definition->setAutowired(XmlUtils::phpize($value));
242-
} elseif (isset($defaults['autowire'])) {
243-
$definition->setAutowired($defaults['autowire']);
244249
}
245250

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

0 commit comments

Comments
 (0)