Skip to content
Permalink
Browse files

parameters can contain statements [Closes #221]

parameters:
	bar: ::getenv(FOO)
  • Loading branch information...
dg committed Oct 29, 2019
1 parent bf3701e commit c1a02611c1cb9aa24a6b75df3aff29df572c1038
Showing with 66 additions and 2 deletions.
  1. +2 −0 src/DI/ContainerBuilder.php
  2. +1 −1 src/DI/PhpGenerator.php
  3. +1 −1 src/DI/Resolver.php
  4. +62 −0 tests/DI/Compiler.parameters.phpt
@@ -317,6 +317,8 @@ public function complete(): void
foreach ($this->definitions as $def) {
$resolver->completeDefinition($def);
}
$this->parameters = $resolver->completeArguments($this->parameters);
}
@@ -47,7 +47,7 @@ public function generate(string $className): Nette\PhpGenerator\ClassType
$class->setExtends(Container::class);
$class->addMethod('__construct')
->addBody('parent::__construct($params);')
->addBody('$this->parameters += ?;', [$this->builder->parameters])
->addBody($this->formatPhp('$this->parameters += ?;', [$this->builder->parameters]))
->addParameter('params', [])
->setTypeHint('array');
@@ -258,7 +258,7 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo
}
private function completeArguments(array $arguments): array
public function completeArguments(array $arguments): array
{
array_walk_recursive($arguments, function (&$val): void {
if ($val instanceof Statement) {
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
use Nette\DI;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class Service
{
public $arg;
public function __construct($arg)
{
$this->arg = $arg;
}
}
test(function () {
$compiler = new DI\Compiler;
$container = createContainer($compiler, '
parameters:
bar: ::trim(" a ")
services:
one: Service(%bar%)
');
Assert::same('a', $container->getService('one')->arg);
});
test(function () {
$compiler = new DI\Compiler;
$container = createContainer($compiler, '
parameters:
bar: @two
services:
one: Service(%bar%)
two: Service(two)
');
Assert::same($container->getService('two'), $container->getService('one')->arg);
});
test(function () {
$compiler = new DI\Compiler;
$container = createContainer($compiler, '
parameters:
bar: typed(Service)
services:
one: Service(%bar%)
two: Service(two)
');
Assert::same([$container->getService('two')], $container->getService('one')->arg);
});

0 comments on commit c1a0261

Please sign in to comment.
You can’t perform that action at this time.