Skip to content

Commit

Permalink
FactoryDefinition: result definition is added to container [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 12, 2024
1 parent ee04330 commit ea318fc
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 32 deletions.
5 changes: 5 additions & 0 deletions src/DI/Definitions/FactoryDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class FactoryDefinition extends Definition
private const MethodCreate = 'create';

private Definition $resultDefinition;
private ?string $reference = null;


public function __construct()
Expand Down Expand Up @@ -87,6 +88,10 @@ public function resolveType(Nette\DI\Resolver $resolver): void
{
if (!$this->getType()) {
throw new ServiceCreationException('Type is missing in definition of service.');

} elseif ($this->reference === null) {
$this->resultDefinition->setAutowired(false);
$this->reference = $resolver->getContainerBuilder()->addDefinition(null, $this->resultDefinition)->getName();
}

$type = Type::fromReflection(new \ReflectionMethod($this->getType(), self::MethodCreate));
Expand Down
18 changes: 2 additions & 16 deletions src/DI/Extensions/DecoratorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ public function beforeCompile()

public function addSetups(string $type, array $setups): void
{
foreach ($this->findByType($type) as $def) {
if ($def instanceof Definitions\FactoryDefinition) {
$def = $def->getResultDefinition();
}

foreach ($this->getContainerBuilder()->findByType($type) as $def) {
foreach ($setups as $setup) {
if (is_array($setup)) {
$setup = new Definitions\Statement(key($setup), array_values($setup));
Expand All @@ -69,18 +65,8 @@ public function addSetups(string $type, array $setups): void
public function addTags(string $type, array $tags): void
{
$tags = Nette\Utils\Arrays::normalize($tags, filling: true);
foreach ($this->findByType($type) as $def) {
foreach ($this->getContainerBuilder()->findByType($type) as $def) {
$def->setTags($def->getTags() + $tags);
}
}


private function findByType(string $type): array
{
return array_filter(
$this->getContainerBuilder()->getDefinitions(),
fn(Definitions\Definition $def): bool => is_a($def->getType(), $type, true)
|| ($def instanceof Definitions\FactoryDefinition && is_a($def->getResultType(), $type, allow_string: true)),
);
}
}
9 changes: 2 additions & 7 deletions src/DI/Extensions/InjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ public function getConfigSchema(): Nette\Schema\Schema
public function beforeCompile()
{
foreach ($this->getContainerBuilder()->getDefinitions() as $def) {
if ($def->getTag(self::TagInject)) {
$def = $def instanceof Definitions\FactoryDefinition
? $def->getResultDefinition()
: $def;
if ($def instanceof Definitions\ServiceDefinition) {
$this->updateDefinition($def);
}
if ($def instanceof Definitions\ServiceDefinition && $def->getTag(self::TagInject)) {
$this->updateDefinition($def);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Extensions/ServicesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private function updateFactoryDefinition(Definitions\FactoryDefinition $definiti
}

if (isset($config->inject)) {
$definition->addTag(InjectExtension::TagInject, $config->inject);
$resultDef->addTag(InjectExtension::TagInject, $config->inject);
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/DI/DecoratorExtension.factories.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $compiler->addExtension('foo', new class extends DI\CompilerExtension {
public function beforeCompile()
{
$this->getContainerBuilder()
->addFactoryDefinition('bar')
->addFactoryDefinition('fac1')
->setImplement(FooFactory::class);
}
});
Expand All @@ -45,16 +45,16 @@ decorator:
FooFactory:
tags: [a]
services:
foo: {implement: FooFactory}
fac2: {implement: FooFactory}
');


$builder = $compiler->getContainerBuilder();

Assert::true($builder->getDefinition('foo')->getTag(DI\Extensions\InjectExtension::TagInject));
Assert::true($builder->getDefinition('foo')->getTag('a'));
Assert::count(1, $builder->getDefinition('foo')->getResultDefinition()->getSetup());
Assert::true($builder->getDefinition('fac1')->getTag('a'));
Assert::count(1, $builder->getDefinition('fac1')->getResultDefinition()->getSetup());
Assert::true($builder->getDefinition('fac1')->getResultDefinition()->getTag(DI\Extensions\InjectExtension::TagInject));

Assert::true($builder->getDefinition('bar')->getTag(DI\Extensions\InjectExtension::TagInject));
Assert::true($builder->getDefinition('bar')->getTag('a'));
Assert::count(1, $builder->getDefinition('bar')->getResultDefinition()->getSetup());
Assert::true($builder->getDefinition('fac2')->getTag('a'));
Assert::count(1, $builder->getDefinition('fac2')->getResultDefinition()->getSetup());
Assert::true($builder->getDefinition('fac2')->getResultDefinition()->getTag(DI\Extensions\InjectExtension::TagInject));
1 change: 1 addition & 0 deletions tests/SearchExtension/all.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Assert::same([
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));


Expand Down
1 change: 1 addition & 0 deletions tests/SearchExtension/batches.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ Assert::same([
'Foo\\ClassBar' => ['foo' => true],
'InterfaceOk1' => ['ok' => true],
'InterfaceOk2' => ['ok' => true],
'stdClass' => [],
], $services);
3 changes: 3 additions & 0 deletions tests/SearchExtension/classes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Assert::same([
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));


Expand Down Expand Up @@ -53,6 +54,7 @@ Assert::same([
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));


Expand Down Expand Up @@ -102,4 +104,5 @@ Assert::same([
'ExtendsStdClass',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));
1 change: 1 addition & 0 deletions tests/SearchExtension/duplicates.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Assert::same([
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));
4 changes: 4 additions & 0 deletions tests/SearchExtension/files.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Assert::same([
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));


Expand All @@ -41,6 +42,7 @@ Assert::same([
'ExtendsStdClass',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));


Expand All @@ -63,6 +65,7 @@ Assert::same([
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));


Expand All @@ -83,4 +86,5 @@ Assert::same([
'ExtendsStdClass',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));
2 changes: 2 additions & 0 deletions tests/SearchExtension/parents.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Assert::same([
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));


Expand Down Expand Up @@ -99,4 +100,5 @@ Assert::same([
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
'stdClass',
], array_keys($services));
2 changes: 2 additions & 0 deletions tests/SearchExtension/tags.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Assert::same([
'ExtendsStdClass' => ['a' => 1, 'b' => 2],
'InterfaceOk1' => ['a' => 1, 'b' => 2],
'InterfaceOk2' => ['a' => 1, 'b' => 2],
'stdClass' => [],
], $services);


Expand All @@ -43,4 +44,5 @@ Assert::same([
'ExtendsStdClass' => [],
'InterfaceOk1' => [],
'InterfaceOk2' => [],
'stdClass' => [],
], $services);

0 comments on commit ea318fc

Please sign in to comment.