Skip to content

Commit

Permalink
dyn
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2023
1 parent 6db8f33 commit 4dbf693
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
14 changes: 5 additions & 9 deletions src/DI/Extensions/ParametersExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,18 @@ public function loadConfiguration()
public function afterCompile(Nette\PhpGenerator\ClassType $class)
{
$builder = $this->getContainerBuilder();
$dynamicParams = $this->dynamicParams;
$dynamicParams = array_fill_keys($this->dynamicParams, true);
foreach ($builder->parameters as $key => $value) {
$value = [$value];
array_walk_recursive($value, function ($val) use (&$dynamicParams, $key): void {
if ($val instanceof DynamicParameter || $val instanceof Nette\DI\Definitions\Statement) {
$dynamicParams[] = $key;
$dynamicParams[$key] = ($dynamicParams[$key] ?? true) && ($val instanceof DynamicParameter);
}
});
}
$dynamicParams = array_values(array_unique($dynamicParams));

$method = Method::from([Container::class, 'getStaticParameters'])
->addBody('return ?;', [array_diff_key($builder->parameters, array_flip($dynamicParams))]);
->addBody('return ?;', [array_diff_key($builder->parameters, $dynamicParams)]);
$class->addMember($method);

if (!$dynamicParams) {
Expand All @@ -81,7 +80,7 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
$method = Method::from([Container::class, 'getDynamicParameter']);
$class->addMember($method);
$method->addBody('switch (true) {');
foreach ($dynamicParams as $key) {
foreach ($dynamicParams as $key => $foo) {
$value = Helpers::expand($this->config[$key] ?? null, $builder->parameters);
try {
$value = $generator->convertArguments($resolver->completeArguments(Helpers::filterArguments([$value])))[0];
Expand All @@ -94,10 +93,7 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)

$method = Method::from([Container::class, 'getParameters']);
$class->addMember($method);
$method->addBody(
'array_map(function ($key) { try { $this->getParameter($key); } catch (\Throwable $e) { $this->parameters[$key] = "unable to resolve"; } }, ?);',
[$dynamicParams]
);
$method->addBody('array_map([$this, \'getParameter\'], ?);', [array_keys($dynamicParams, true, true)]);
$method->addBody('return parent::getParameters();');

foreach ($this->dynamicValidators as [$param, $expected, $path]) {
Expand Down
3 changes: 3 additions & 0 deletions tests/DI/Compiler.parameters.code.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ parameters:
arrayDynamic:
dynamic: %dynamic%
inner: %arrayDynamic.dynamic.foo%
mix:
expr: %expr%
dynamic: %dynamic%
refStatic: %static%
refDynamic: %dynamic%
refDynamic2: %dynamic.foo%
Expand Down
27 changes: 11 additions & 16 deletions tests/DI/Compiler.parameters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test('Statement as parameter', function () {
one: Service(%bar%)
');

Assert::same(['bar' => 'a'], $container->getParameters());
Assert::same([], $container->getParameters());
Assert::same('a', $container->getParameter('bar'));
Assert::same('a', $container->getService('one')->arg);
});
Expand All @@ -56,10 +56,7 @@ test('Statement within string expansion', function () {
one: Service(%expand%)
');

Assert::same(
['bar' => 'a', 'expand' => 'helloa'],
$container->getParameters()
);
Assert::same([], $container->getParameters());
Assert::same('helloa', $container->getService('one')->arg);
});

Expand Down Expand Up @@ -89,7 +86,7 @@ test('Class method and constant resolution', function () {
one: Service(%bar%)
');

Assert::same(['bar' => 'Service::method hello'], $container->getParameters());
Assert::same([], $container->getParameters());
Assert::same('Service::method hello', $container->getService('one')->arg);
});

Expand Down Expand Up @@ -122,10 +119,7 @@ test('Parameter as an instantiated class', function () {
two: Service(two)
');

Assert::equal(
['bar' => new Service($container->getService('two'))],
$container->getParameters()
);
Assert::equal([], $container->getParameters());
Assert::same($container->getService('two'), $container->getService('one')->arg->arg);
});

Expand All @@ -141,10 +135,7 @@ test('Parameter as array of services', function () {
two: Service(two)
');

Assert::same(
['bar' => [$container->getService('one'), $container->getService('two')]],
$container->getParameters()
);
Assert::same([], $container->getParameters());
Assert::same([$container->getService('two')], $container->getService('one')->arg);
});

Expand Down Expand Up @@ -172,7 +163,8 @@ test('Invalid statement as parameter', function () {
bar: unknown()
');

Assert::same(['bar' => 'unable to resolve'], $container->getParameters());
Assert::same([], $container->getParameters());
Assert::same('unable to resolve', $container->getParameter('bar'));
});


Expand All @@ -183,5 +175,8 @@ test('Invalid statement as parameter', function () {
bar: Service::unknown()
');

Assert::same(['bar' => 'unable to resolve'], $container->getParameters());
Assert::same([], $container->getParameters());
Assert::exception(function () use ($container) {
return $container->getParameter('bar');
}, Error::class, 'Call to undefined method Service::unknown()');
});
9 changes: 2 additions & 7 deletions tests/DI/expected/compiler.parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function getDynamicParameter($key)
'dynamic' => $this->getParameter('dynamic'),
'inner' => $this->getParameter('dynamic')['foo'],
];
case $key === 'mix': return ['expr' => trim(' a '), 'dynamic' => $this->getParameter('dynamic')];
case $key === 'refDynamic': return $this->getParameter('dynamic');
case $key === 'refDynamic2': return $this->getParameter('dynamic')['foo'];
case $key === 'refExpr': return trim(' a ');
Expand All @@ -61,17 +62,11 @@ protected function getDynamicParameter($key)

public function getParameters(): array
{
array_map(function ($key) { try { $this->getParameter($key); } catch (\Throwable $e) { $this->parameters[$key] = "unable to resolve"; } }, [
array_map([$this, 'getParameter'], [
'dynamic',
'expr',
'arrayExpr',
'arrayExpr2',
'arrayDynamic',
'refDynamic',
'refDynamic2',
'refExpr',
'refArrayE1',
'refArrayE2',
'refArrayD1',
'refArrayD2',
'refArrayD3',
Expand Down

0 comments on commit 4dbf693

Please sign in to comment.