From 023d5069fa0f40e5eed8538c72dd4b6a6b2298de Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 3 Feb 2024 18:39:55 +0100 Subject: [PATCH] allowed %foo.bar% expansion of Statement --- src/DI/Helpers.php | 2 ++ tests/DI/Compiler.parameters.code.phpt | 2 ++ tests/DI/Compiler.parameters.phpt | 23 +++++++++++++++++++++++ tests/DI/expected/compiler.parameters.php | 2 ++ 4 files changed, 29 insertions(+) diff --git a/src/DI/Helpers.php b/src/DI/Helpers.php index 7ad902fb9..33313492c 100644 --- a/src/DI/Helpers.php +++ b/src/DI/Helpers.php @@ -113,6 +113,8 @@ private static function expandParameter(string $parameter, array $params, ?array } } elseif ($val instanceof DynamicParameter) { $val = new DynamicParameter($val . '[' . var_export($key, true) . ']'); + } elseif ($val instanceof Statement) { + $val = new Statement('(?)[?]', [$val, $key]); } else { throw new Nette\InvalidArgumentException(sprintf("Missing parameter '%s'.", $parameter)); } diff --git a/tests/DI/Compiler.parameters.code.phpt b/tests/DI/Compiler.parameters.code.phpt index bc01ecb50..f9720829b 100644 --- a/tests/DI/Compiler.parameters.code.phpt +++ b/tests/DI/Compiler.parameters.code.phpt @@ -39,6 +39,7 @@ parameters: refDynamic: %dynamic% refDynamic2: %dynamic.foo% refExpr: %expr% + refExpr2: %expr.1% refArrayE1: %arrayExpr% refArrayE2: %arrayExpr.expr% refArrayD1: %arrayDynamic% @@ -49,6 +50,7 @@ services: - Service( %static% %expr% + %expr.1% %dynamic% %dynamic.foo% %arrayExpr% diff --git a/tests/DI/Compiler.parameters.phpt b/tests/DI/Compiler.parameters.phpt index 1b2097ea5..e078c3433 100644 --- a/tests/DI/Compiler.parameters.phpt +++ b/tests/DI/Compiler.parameters.phpt @@ -29,6 +29,12 @@ class Service } +function getArray() +{ + return ['foo' => 123]; +} + + test('Statement as parameter', function () { $compiler = new DI\Compiler; $container = createContainer($compiler, ' @@ -62,6 +68,23 @@ test('Statement within string expansion', function () { }); +test('Statement within array expansion', function () { + $compiler = new DI\Compiler; + $container = createContainer($compiler, ' + parameters: + bar: ::getArray() + expand: %bar.foo% + + services: + one: Service(%expand%) + '); + + Assert::same([], $container->getParameters()); + Assert::same(123, $container->getParameter('expand')); + Assert::same(123, $container->getService('one')->arg); +}); + + test('NOT class constant as parameter', function () { $compiler = new DI\Compiler; $container = createContainer($compiler, ' diff --git a/tests/DI/expected/compiler.parameters.php b/tests/DI/expected/compiler.parameters.php index 0e2dfbbad..435c07016 100644 --- a/tests/DI/expected/compiler.parameters.php +++ b/tests/DI/expected/compiler.parameters.php @@ -14,6 +14,7 @@ public function createService01(): Service return new Service( 123, trim(' a '), + (trim(' a '))['1'], $this->getParameter('dynamic'), $this->getParameter('dynamic')['foo'], ['expr' => trim(' a ')], @@ -52,6 +53,7 @@ protected function getDynamicParameter($key) case $key === 'refDynamic': return $this->getParameter('dynamic'); case $key === 'refDynamic2': return $this->getParameter('dynamic')['foo']; case $key === 'refExpr': return trim(' a '); + case $key === 'refExpr2': return (trim(' a '))['1']; case $key === 'refArrayE1': return ['expr' => trim(' a ')]; case $key === 'refArrayE2': return trim(' a '); case $key === 'refArrayD1': return [