Skip to content

Commit 18fdc11

Browse files
committed
Processor: definition of 'type' will not override 'factory' of parent service (#39)
for BC compatibility, 'class' will override 'factory'
1 parent 4a07bd1 commit 18fdc11

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

src/DI/Config/Processor.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ public function normalizeConfig($config): array
134134
return ['factory' => $config];
135135

136136
} elseif (is_array($config)) {
137+
if (isset($config['class']) && !isset($config['factory'])) {
138+
$config['factory'] = null;
139+
}
137140
foreach (['class' => 'type', 'dynamic' => 'imported'] as $alias => $original) {
138141
if (array_key_exists($alias, $config)) {
139142
if (array_key_exists($original, $config)) {
@@ -195,22 +198,18 @@ private function updateServiceDefinition(Definitions\ServiceDefinition $definiti
195198
{
196199
$config = self::processArguments($config);
197200

198-
if (array_key_exists('type', $config) || array_key_exists('factory', $config)) {
201+
if (array_key_exists('factory', $config)) {
202+
$definition->setFactory($config['factory']);
199203
$definition->setType(null);
200-
$definition->setFactory(null);
201204
}
202205

203206
if (array_key_exists('type', $config)) {
204207
if ($config['type'] instanceof Statement) {
205208
trigger_error("Service '$name': option 'type' or 'class' should be changed to 'factory'.", E_USER_DEPRECATED);
209+
$definition->setFactory($config['type']);
206210
} else {
207211
$definition->setType($config['type']);
208212
}
209-
$definition->setFactory($config['type']);
210-
}
211-
212-
if (array_key_exists('factory', $config)) {
213-
$definition->setFactory($config['factory']);
214213
}
215214

216215
if (array_key_exists('arguments', $config)) {

tests/DI/Compiler.extensionOverride.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class FooExtension extends Nette\DI\CompilerExtension
7373
$builder->addDefinition('one8')
7474
->setFactory('Lorem', [1])
7575
->addSetup('__construct', [2]);
76+
$builder->addDefinition('one9')
77+
->setFactory('Lorem', [1]);
78+
$builder->addDefinition('one10')
79+
->setFactory('Lorem', [1]);
7680

7781
$builder->addDefinition('two1')
7882
->setType('Lorem')
@@ -101,6 +105,12 @@ class FooExtension extends Nette\DI\CompilerExtension
101105
$builder->addDefinition('two9')
102106
->setType('Lorem')
103107
->setFactory('Factory::createLorem', [1, 2]);
108+
$builder->addDefinition('two10')
109+
->setType('Lorem')
110+
->setFactory('Factory::createLorem', [1]);
111+
$builder->addDefinition('two11')
112+
->setType('Lorem')
113+
->setFactory('Factory::createLorem', [1]);
104114

105115
$builder->addDefinition('three1')
106116
->setFactory('Factory::createLorem', [1]);
@@ -116,6 +126,10 @@ class FooExtension extends Nette\DI\CompilerExtension
116126
->setFactory('Factory::createLorem', [1]);
117127
$builder->addDefinition('three7')
118128
->setFactory('Factory::createLorem', [1]);
129+
$builder->addDefinition('three8')
130+
->setFactory('Factory::createLorem', [1]);
131+
$builder->addDefinition('three9')
132+
->setFactory('Factory::createLorem', [1]);
119133
}
120134
}
121135

@@ -166,6 +180,16 @@ Assert::same([
166180
'Ipsum::__construct ',
167181
], Notes::fetch());
168182

183+
Assert::exception(function () use ($container) {
184+
$container->getService('one9');
185+
}, TypeError::class, 'Return value of %a%::createServiceOne9() must be an instance of Ipsum, instance of Lorem returned');
186+
Notes::fetch();
187+
188+
Assert::type(Ipsum::class, $container->getService('one10'));
189+
Assert::same([
190+
'Ipsum::__construct ',
191+
], Notes::fetch());
192+
169193

170194
Assert::type(Ipsum::class, $container->getService('two1'));
171195
Assert::same([
@@ -217,6 +241,16 @@ Assert::same([
217241
'Lorem::__construct 2 new',
218242
], Notes::fetch());
219243

244+
Assert::exception(function () use ($container) {
245+
$container->getService('two11');
246+
}, TypeError::class, 'Return value of %a%::createServiceTwo11() must be an instance of Ipsum, instance of Lorem returned');
247+
Notes::fetch();
248+
249+
Assert::type(Ipsum::class, $container->getService('two12'));
250+
Assert::same([
251+
'Ipsum::__construct ',
252+
], Notes::fetch());
253+
220254

221255

222256
Assert::type(Ipsum::class, $container->getService('three1'));
@@ -253,3 +287,13 @@ Assert::type(Ipsum::class, $container->getService('three7'));
253287
Assert::same([
254288
'Ipsum::__construct 2',
255289
], Notes::fetch());
290+
291+
Assert::exception(function () use ($container) {
292+
$container->getService('three8');
293+
}, TypeError::class, 'Return value of %a%::createServiceThree8() must be an instance of Ipsum, instance of Lorem returned');
294+
Notes::fetch();
295+
296+
Assert::type(Ipsum::class, $container->getService('three9'));
297+
Assert::same([
298+
'Ipsum::__construct ',
299+
], Notes::fetch());

tests/DI/Config.Processor.normalizeConfig.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Assert::same(['implement' => Iface::class, 'tagged' => 123], $processor->normali
4343

4444

4545
// aliases
46-
Assert::same(['type' => 'val'], $processor->normalizeConfig(['class' => 'val']));
46+
Assert::same(['factory' => null, 'type' => 'val'], $processor->normalizeConfig(['class' => 'val']));
4747
Assert::same(['imported' => 'val'], $processor->normalizeConfig(['dynamic' => 'val']));
4848

4949
Assert::exception(function () use ($processor) {

tests/DI/files/compiler.extensionOverride.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ services:
1818
factory: IpsumFactory::create(2)
1919
one8!:
2020
factory: Ipsum
21+
one9:
22+
type: Ipsum
23+
one10:
24+
class: Ipsum
2125

2226
two1:
2327
factory: Ipsum
@@ -42,6 +46,10 @@ services:
4246
two10:
4347
factory: Factory::createLorem(2)
4448
arguments: [1: new]
49+
two11:
50+
type: Ipsum
51+
two12:
52+
class: Ipsum
4553

4654
three1:
4755
factory: Ipsum
@@ -59,3 +67,7 @@ services:
5967
arguments: [2]
6068
three7:
6169
factory: IpsumFactory::create(2)
70+
three8:
71+
type: Ipsum
72+
three9:
73+
class: Ipsum

0 commit comments

Comments
 (0)