Skip to content

Commit

Permalink
Compiler: deprecated key 'create'
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 7, 2016
1 parent dead11d commit 35135b1
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 39 deletions.
19 changes: 10 additions & 9 deletions src/DI/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,16 @@ public static function parseService(ServiceDefinition $definition, $config)
$config = ['class' => NULL, 'implement' => $config->getEntity(), 'factory' => array_shift($config->arguments)];

} elseif (!is_array($config) || isset($config[0], $config[1])) {
$config = ['class' => NULL, 'create' => $config];
$config = ['class' => NULL, 'factory' => $config];
}

if (array_key_exists('factory', $config)) {
$config['create'] = $config['factory'];
unset($config['factory']);
if (array_key_exists('create', $config)) {
trigger_error("Key 'create' is deprecated, use 'factory' or 'class' in configuration.", E_USER_DEPRECATED);
$config['factory'] = $config['create'];
unset($config['create']);
};

$known = ['class', 'create', 'arguments', 'setup', 'autowired', 'dynamic', 'inject', 'parameters', 'implement', 'run', 'tags'];
$known = ['class', 'factory', 'arguments', 'setup', 'autowired', 'dynamic', 'inject', 'parameters', 'implement', 'run', 'tags'];
if ($error = array_diff(array_keys($config), $known)) {
$hints = array_filter(array_map(function ($error) use ($known) {
return Nette\Utils\ObjectMixin::getSuggestion($known, $error);
Expand All @@ -354,7 +355,7 @@ public static function parseService(ServiceDefinition $definition, $config)
$definition->setArguments($arguments);
}

if (array_key_exists('class', $config) || array_key_exists('create', $config)) {
if (array_key_exists('class', $config) || array_key_exists('factory', $config)) {
$definition->setClass(NULL);
$definition->setFactory(NULL);
}
Expand All @@ -367,9 +368,9 @@ public static function parseService(ServiceDefinition $definition, $config)
$definition->setFactory($config['class'], $arguments);
}

if (array_key_exists('create', $config)) {
Validators::assertField($config, 'create', 'callable|Nette\DI\Statement|null');
$definition->setFactory($config['create'], $arguments);
if (array_key_exists('factory', $config)) {
Validators::assertField($config, 'factory', 'callable|Nette\DI\Statement|null');
$definition->setFactory($config['factory'], $arguments);
}

if (isset($config['setup'])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Compiler.extension.defaultServices.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ services:
two: Bar
three: Factory::createLorem()
four:
create: Ipsum
class: Ipsum
implement: IIpsumFactory
');

Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Compiler.services.autowiring.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Ipsum
$container = createContainer(new DI\Compiler, '
services:
model:
create: Factory()::createModel
factory: Factory()::createModel
setup:
# local methods
- test(...)
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Compiler.services.tags.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $compiler = new DI\Compiler;
$container = createContainer($compiler, '
services:
lorem:
create: stdClass
class: stdClass
tags:
- a
b: c
Expand Down
8 changes: 4 additions & 4 deletions tests/DI/NeonAdapter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ services:
three:
class: Lorem
create: Factory::createLorem
factory: Factory::createLorem
arguments:
- 1
four:
create: Factory::createLorem(1)
factory: Factory::createLorem(1)
five:
create: [Factory, createLorem](1)
factory: [Factory, createLorem](1)
six: Factory::createLorem(1)
seven: @factory
Expand All @@ -199,7 +199,7 @@ services:
factory: Lorem
rich1: Lorem(1)::foo()
rich2:
create: Lorem(Ipsum(@one))::foo(1)
factory: Lorem(Ipsum(@one))::foo(1)
rich3: Factory::createLorem(1)::foo()
rich4: Factory()::createLorem(1)::foo()
Expand Down
18 changes: 9 additions & 9 deletions tests/DI/files/compiler.extensionOverride.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ services:
one4:
arguments: [2]
one5:
create: IpsumFactory::create
factory: IpsumFactory::create
one6:
create: IpsumFactory::create
factory: IpsumFactory::create
arguments: [2]
one7:
create: IpsumFactory::create(2)
factory: IpsumFactory::create(2)

two1:
class: Ipsum
Expand All @@ -26,12 +26,12 @@ services:
two4:
arguments: [2]
two5:
create: IpsumFactory::create
factory: IpsumFactory::create
two6:
create: IpsumFactory::create
factory: IpsumFactory::create
arguments: [2]
two7:
create: IpsumFactory::create(2)
factory: IpsumFactory::create(2)

three1:
class: Ipsum
Expand All @@ -43,9 +43,9 @@ services:
three4:
arguments: [2]
three5:
create: IpsumFactory::create
factory: IpsumFactory::create
three6:
create: IpsumFactory::create
factory: IpsumFactory::create
arguments: [2]
three7:
create: IpsumFactory::create(2)
factory: IpsumFactory::create(2)
6 changes: 3 additions & 3 deletions tests/DI/files/compiler.generatedFactory.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
finder: IFinderFactory

article:
create: Article(%title%)
factory: Article(%title%)
implement: IArticleFactory
parameters: [title]

Expand All @@ -18,12 +18,12 @@ services:
parameters: [title]

fooFactory1:
create: Foo(..., %baz%)
factory: Foo(..., %baz%)
implement: IFooFactory
parameters: [Baz baz = NULL]

fooFactory3:
create: Foo
class: Foo
implement: IFooFactory

fooFactory4: IFooFactory
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/files/compiler.generatedFactory.returnTypes.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:

article:
create: Article(%title%)
factory: Article(%title%)
implement: IArticleFactory
parameters: [title]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:

article:
create: Article(%title%)
factory: Article(%title%)
implement: IArticleFactory
parameters: [string title]

Expand Down
8 changes: 4 additions & 4 deletions tests/DI/files/compiler.services.factory.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ services:

three:
class: Lorem
create: Factory::createLorem
factory: Factory::createLorem
arguments: [1]

four:
create: Factory::createLorem(1)
factory: Factory::createLorem(1)

five:
create: [Factory, createLorem](1)
factory: [Factory, createLorem](1)

six: Factory::createLorem(1)

Expand All @@ -48,7 +48,7 @@ services:
rich1: Lorem(1)::foo

rich2:
create: Lorem(Ipsum(@one))::foo(1)
factory: Lorem(Ipsum(@one))::foo(1)

rich3: Factory::createLorem(1)::foo

Expand Down
2 changes: 1 addition & 1 deletion tests/DI/files/compiler.services.setup.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ parameters:

services:
lorem:
create: Lorem
class: Lorem
setup:
# local methods
- test(2)
Expand Down
8 changes: 4 additions & 4 deletions tests/DI/files/neonAdapter.save.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ services:

three:
class: Lorem
create: Factory::createLorem
factory: Factory::createLorem
arguments: [1]

four:
create: Factory::createLorem(1)
factory: Factory::createLorem(1)

five:
create: [Factory, createLorem](1)
factory: [Factory, createLorem](1)

six: Factory::createLorem(1)

Expand All @@ -48,7 +48,7 @@ services:
rich1: Lorem(1)::foo

rich2:
create: Lorem(Ipsum(@one))::foo(1)
factory: Lorem(Ipsum(@one))::foo(1)

rich3: Factory::createLorem(1)::foo

Expand Down

0 comments on commit 35135b1

Please sign in to comment.