From a26fd4757ebd12e469fedba6b4bc2d0f9027ce9e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 16 Oct 2023 11:40:00 +0200 Subject: [PATCH] ServiceDefinition: prepends @self in depth [WIP] --- src/DI/Definitions/ServiceDefinition.php | 11 ++++++++--- tests/DI/Compiler.services.setup.phpt | 3 +++ tests/DI/files/compiler.services.setup.neon | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/DI/Definitions/ServiceDefinition.php b/src/DI/Definitions/ServiceDefinition.php index ede56644b..f75af8119 100644 --- a/src/DI/Definitions/ServiceDefinition.php +++ b/src/DI/Definitions/ServiceDefinition.php @@ -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; + } } diff --git a/tests/DI/Compiler.services.setup.phpt b/tests/DI/Compiler.services.setup.phpt index b95de0cad..86417007a 100644 --- a/tests/DI/Compiler.services.setup.phpt +++ b/tests/DI/Compiler.services.setup.phpt @@ -22,6 +22,7 @@ class Lorem public function test(...$args) { Notes::add(__METHOD__ . ' ' . implode(' ', $args)); + return new self; } } @@ -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); diff --git a/tests/DI/files/compiler.services.setup.neon b/tests/DI/files/compiler.services.setup.neon index 17f12de3a..1a0a28eeb 100644 --- a/tests/DI/files/compiler.services.setup.neon +++ b/tests/DI/files/compiler.services.setup.neon @@ -36,6 +36,7 @@ services: # rich - Ipsum(a)::test - Ipsum(b)::test(10) + - test(outer)::test(inner) # object - \Ipsum(@self)