Skip to content

Commit

Permalink
ServiceDefinition: prepends @self to setup immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2023
1 parent 6cba2d5 commit f614afc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
23 changes: 13 additions & 10 deletions src/DI/Definitions/ServiceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ public function setArgument($key, $value)
*/
public function setSetup(array $setup)
{
foreach ($setup as $v) {
if (!$v instanceof Statement) {
foreach ($setup as &$entity) {
if (!$entity instanceof Statement) {
throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Definitions\Statement[].');
}
$entity = $this->prependSelf($entity);
}

$this->setup = $setup;
Expand All @@ -134,9 +135,10 @@ public function getSetup(): array
*/
public function addSetup($entity, array $args = [])
{
$this->setup[] = $entity instanceof Statement
$entity = $entity instanceof Statement
? $entity
: new Statement($entity, $args);
$this->setup[] = $this->prependSelf($entity);
return $this;
}

Expand Down Expand Up @@ -178,18 +180,19 @@ public function complete(Nette\DI\Resolver $resolver): void
$this->creator = $resolver->completeStatement($this->creator);

foreach ($this->setup as &$setup) {
if (
is_string($setup->getEntity())
&& strpbrk($setup->getEntity(), ':@?\\') === false
) { // auto-prepend @self
$setup = new Statement([new Reference(Reference::Self), $setup->getEntity()], $setup->arguments);
}

$setup = $resolver->completeStatement($setup, true);
}
}


private function prependSelf(Statement $setup): Statement
{
return is_string($setup->getEntity()) && strpbrk($setup->getEntity(), ':@?\\') === false
? new Statement([new Reference(Reference::Self), $setup->getEntity()], $setup->arguments)
: $setup;
}


public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
{
$code = $generator->formatStatement($this->creator) . ";\n";
Expand Down
8 changes: 4 additions & 4 deletions src/DI/Extensions/InjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ private function updateDefinition(Definitions\ServiceDefinition $def): void

foreach (self::getInjectProperties($class) as $property => $type) {
$builder = $this->getContainerBuilder();
$inject = new Definitions\Statement('$' . $property, [Definitions\Reference::fromType((string) $type)]);
$inject = new Definitions\Statement(['@self', '$' . $property], [Definitions\Reference::fromType((string) $type)]);
foreach ($setups as $key => $setup) {
if ($setup->getEntity() === $inject->getEntity()) {
if ($setup->getEntity() == $inject->getEntity()) { // intentionally ==
$inject = $setup;
$builder = null;
unset($setups[$key]);
Expand All @@ -70,9 +70,9 @@ private function updateDefinition(Definitions\ServiceDefinition $def): void
}

foreach (array_reverse(self::getInjectMethods($class)) as $method) {
$inject = new Definitions\Statement($method);
$inject = new Definitions\Statement(['@self', $method]);
foreach ($setups as $key => $setup) {
if ($setup->getEntity() === $inject->getEntity()) {
if ($setup->getEntity() == $inject->getEntity()) { // intentionally ==
$inject = $setup;
unset($setups[$key]);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/DI/Definitions.ServiceDefinition.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ test('Setting setups with arguments/statements', function () {
$def->addSetup(new Statement(stdClass::class, [1, 2]));
$def->addSetup(new Statement(stdClass::class, [1, 2]), [99]); // 99 is ignored
Assert::equal([
new Statement(stdClass::class, [1, 2]),
new Statement(stdClass::class, [1, 2]),
new Statement(stdClass::class, [1, 2]),
new Statement(['@self', stdClass::class], [1, 2]),
new Statement(['@self', stdClass::class], [1, 2]),
new Statement(['@self', stdClass::class], [1, 2]),
], $def->getSetup());
});

Expand Down
3 changes: 2 additions & 1 deletion tests/DI/InjectExtension.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ services:
inject: true
setup:
- injectB(1)
- @self::injectC(1)
- $e(@\ConcreteDependencyB)
');

Expand Down Expand Up @@ -128,7 +129,7 @@ Assert::equal([
Assert::equal([
new Statement([new Reference('self'), 'injectA']),
new Statement([new Reference('self'), 'injectB'], [1]),
new Statement([new Reference('self'), 'injectC']),
new Statement([new Reference('self'), 'injectC'], [1]),
new Statement([new Reference('self'), 'injectD']),
new Statement([new Reference('self'), '$e'], [new Reference('b')]),
new Statement([new Reference('self'), '$c'], [new Reference('std')]),
Expand Down

0 comments on commit f614afc

Please sign in to comment.