Skip to content

Commit

Permalink
allowed %foo.bar% expansion of Statement
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 6, 2024
1 parent 4a16514 commit 023d506
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 2 additions & 0 deletions tests/DI/Compiler.parameters.code.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ parameters:
refDynamic: %dynamic%
refDynamic2: %dynamic.foo%
refExpr: %expr%
refExpr2: %expr.1%
refArrayE1: %arrayExpr%
refArrayE2: %arrayExpr.expr%
refArrayD1: %arrayDynamic%
Expand All @@ -49,6 +50,7 @@ services:
- Service(
%static%
%expr%
%expr.1%
%dynamic%
%dynamic.foo%
%arrayExpr%
Expand Down
23 changes: 23 additions & 0 deletions tests/DI/Compiler.parameters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class Service
}


function getArray()
{
return ['foo' => 123];
}


test('Statement as parameter', function () {
$compiler = new DI\Compiler;
$container = createContainer($compiler, '
Expand Down Expand Up @@ -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, '
Expand Down
2 changes: 2 additions & 0 deletions tests/DI/expected/compiler.parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ')],
Expand Down Expand Up @@ -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 [
Expand Down

0 comments on commit 023d506

Please sign in to comment.