Skip to content

Commit

Permalink
ServiceDefinition: prepends @self in depth [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 30, 2023
1 parent 75c7f0c commit a26fd47
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/DI/Definitions/ServiceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,14 @@ public function complete(Nette\DI\Resolver $resolver): void

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;
// TODO: variable can start with a capital letter, etc.
if (is_string($member = $setup->getEntity()) && preg_match('~^\$?[a-z][a-zA-Z0-9[\]]*$~', $member)) {
return new Statement([new Reference(Reference::Self), $member], $setup->arguments);
} elseif (is_array($setup->getEntity()) && $setup->getEntity()[0] instanceof Statement) {
return new Statement([$this->prependSelf($setup->getEntity()[0]), $setup->getEntity()[1]], $setup->arguments);
} else {
return $setup;
}
}


Expand Down
3 changes: 3 additions & 0 deletions tests/DI/Compiler.services.setup.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Lorem
public function test(...$args)
{
Notes::add(__METHOD__ . ' ' . implode(' ', $args));
return new self;
}
}

Expand Down Expand Up @@ -79,6 +80,8 @@ Assert::same([
'globtest 7',
'Ipsum::test a',
'Ipsum::test 10 b',
'Lorem::test outer',
'Lorem::test inner',
], Notes::fetch());

Assert::same(8, $container->getService('lorem')->test);
Expand Down
1 change: 1 addition & 0 deletions tests/DI/files/compiler.services.setup.neon
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ services:
# rich
- Ipsum(a)::test
- Ipsum(b)::test(10)
- test(outer)::test(inner)

# object
- \Ipsum(@self)
Expand Down

0 comments on commit a26fd47

Please sign in to comment.