Skip to content

Commit 4241f91

Browse files
committed
Compiler: added configuration option 'type' as replacement for 'class'
1 parent 79faff1 commit 4241f91

11 files changed

+30
-22
lines changed

src/DI/Compiler.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,16 @@ public static function loadDefinition(ServiceDefinition $definition, $config, st
318318
return;
319319

320320
} elseif (is_string($config) && interface_exists($config)) {
321-
$config = ['class' => null, 'implement' => $config];
321+
$config = ['implement' => $config];
322322

323323
} elseif ($config instanceof Statement && is_string($config->getEntity()) && interface_exists($config->getEntity())) {
324-
$config = ['class' => null, 'implement' => $config->getEntity(), 'factory' => array_shift($config->arguments)];
324+
$config = ['implement' => $config->getEntity(), 'factory' => array_shift($config->arguments)];
325325

326326
} elseif (!is_array($config) || isset($config[0], $config[1])) {
327-
$config = ['class' => null, 'factory' => $config];
327+
$config = ['factory' => $config];
328328
}
329329

330-
$known = ['class', 'factory', 'arguments', 'setup', 'autowired', 'dynamic', 'inject', 'parameters', 'implement', 'run', 'tags', 'alteration'];
330+
$known = ['type', 'class', 'factory', 'arguments', 'setup', 'autowired', 'dynamic', 'inject', 'parameters', 'implement', 'run', 'tags', 'alteration'];
331331
if ($error = array_diff(array_keys($config), $known)) {
332332
$hints = array_filter(array_map(function ($error) use ($known) {
333333
return Nette\Utils\ObjectMixin::getSuggestion($known, $error);
@@ -343,6 +343,14 @@ public static function loadDefinition(ServiceDefinition $definition, $config, st
343343
$definition->setFactory(null);
344344
}
345345

346+
if (array_key_exists('type', $config)) {
347+
Validators::assertField($config, 'type', 'string|null');
348+
$definition->setClass($config['type']);
349+
if (array_key_exists('class', $config)) {
350+
throw new Nette\InvalidStateException("Unexpected 'class' when 'type' is used.");
351+
}
352+
}
353+
346354
if (array_key_exists('class', $config)) {
347355
Validators::assertField($config, 'class', 'string|Nette\DI\Statement|null');
348356
if ($config['class'] instanceof Statement) {

src/DI/ContainerBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public function prepareClassList()
289289

290290
if ($def->isDynamic()) {
291291
if (!$def->getClass()) {
292-
throw new ServiceCreationException("Class is missing in definition of service '$name'.");
292+
throw new ServiceCreationException("Type is missing in definition of service '$name'.");
293293
}
294294
$def->setFactory(null);
295295
continue;
@@ -298,7 +298,7 @@ public function prepareClassList()
298298
// complete class-factory pairs
299299
if (!$def->getEntity()) {
300300
if (!$def->getClass()) {
301-
throw new ServiceCreationException("Class and factory are missing in definition of service '$name'.");
301+
throw new ServiceCreationException("Factory and type are missing in definition of service '$name'.");
302302
}
303303
$def->setFactory($def->getClass(), ($factory = $def->getFactory()) ? $factory->arguments : []);
304304
}

tests/DI/Compiler.configOverride.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ $compiler = new DI\Compiler;
2626
$compiler->addConfig([
2727
'services' => [
2828
's1' => 'Ipsum',
29-
's2' => ['class' => 'Ipsum'],
29+
's2' => ['type' => 'Ipsum'],
3030
],
3131
]);
3232
$compiler->addConfig([
3333
'services' => [
3434
's1' => ['arguments' => [2]],
35-
's2' => ['class' => 'Ipsum', 'alteration' => true],
35+
's2' => ['type' => 'Ipsum', 'alteration' => true],
3636
],
3737
]);
3838

@@ -52,7 +52,7 @@ Assert::same([
5252

5353
$compiler->addConfig([
5454
'services' => [
55-
's3' => ['class' => 'Ipsum', 'alteration' => true],
55+
's3' => ['type' => 'Ipsum', 'alteration' => true],
5656
],
5757
]);
5858

tests/DI/Compiler.extension.defaultServices.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ services:
9292
two: Bar
9393
three: Factory::createLorem()
9494
four:
95-
class: Ipsum
95+
type: Ipsum
9696
implement: IIpsumFactory
9797
');
9898

tests/DI/Compiler.services.dynamic.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Service
2121
$container = createContainer(new DI\Compiler, '
2222
services:
2323
one:
24-
class: Service
24+
type: Service
2525
dynamic: true
2626
');
2727

tests/DI/Compiler.services.nonshared.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ services:
3131
factory: Ipsum
3232
3333
lorem:
34-
class: Lorem
34+
type: Lorem
3535
parameters: [Ipsum foo, bar: %false%]
3636
setup:
3737
- test(%foo%, %bar%)

tests/DI/ContainerBuilder.autowiring.chaining.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ $compiler = new DI\Compiler;
7777
$container = createContainer($compiler, '
7878
services:
7979
baz:
80-
class: Baz
80+
type: Baz
8181
factory: Foo::createUnknown()::foo()
8282
');
8383
Assert::true($container->hasService('baz'));

tests/DI/NeonAdapter.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Assert::match(<<<'EOD'
108108
# generated by Nette
109109
110110
parameters:
111-
class: Ipsum
111+
type: Ipsum
112112
113113
services:
114114
referencedService: @one
@@ -123,15 +123,15 @@ services:
123123
calledServiceAsParam: Ipsum(@one())
124124
calledServiceWithArgsAsParam: Ipsum(@one(1))
125125
one:
126-
class: %class%
126+
type: %class%
127127
arguments:
128128
- 1
129129
130130
two:
131131
factory: %class%(1)
132132
133133
three:
134-
class: Lorem
134+
type: Lorem
135135
factory: Factory::createLorem
136136
arguments:
137137
- 1

tests/DI/files/compiler.generatedFactory.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ services:
2323
parameters: [Baz baz = null]
2424

2525
fooFactory3:
26-
class: Foo
26+
type: Foo
2727
implement: IFooFactory
2828

2929
fooFactory4: IFooFactory

tests/DI/files/compiler.services.factory.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ services:
1616
calledServiceWithArgsAsParam: Ipsum(@one(1))
1717

1818
one:
19-
class: %class%
19+
type: %class%
2020
arguments: [1]
2121

2222
two:
2323
factory: %class%(1)
2424

2525
three:
26-
class: Lorem
26+
type: Lorem
2727
factory: Factory::createLorem
2828
arguments: [arg: 5, 1, 2]
2929

0 commit comments

Comments
 (0)