Skip to content

Commit d0d11d0

Browse files
committed
ExtensionsExtension: Allow passing parameters to extension constructor
1 parent 7da1abe commit d0d11d0

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/DI/Extensions/ExtensionsExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ class ExtensionsExtension extends Nette\DI\CompilerExtension
2121
public function loadConfiguration()
2222
{
2323
foreach ($this->getConfig() as $name => $class) {
24-
$this->compiler->addExtension($name, new $class);
24+
if ($class instanceof Nette\DI\Statement) {
25+
$rc = Nette\Reflection\ClassType::from($class->entity);
26+
$this->compiler->addExtension($name, $rc->newInstanceArgs($class->arguments));
27+
} else {
28+
$this->compiler->addExtension($name, new $class);
29+
}
2530
}
2631
}
2732

tests/DI/Compiler.extension.extensions.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,36 @@ class FooExtension extends DI\CompilerExtension
2020
}
2121

2222

23+
class BarExtension extends DI\CompilerExtension
24+
{
25+
private $param;
26+
27+
public function __construct($param)
28+
{
29+
$this->param = $param;
30+
}
31+
32+
function loadConfiguration()
33+
{
34+
$this->getContainerBuilder()->parameters['bar'] = $this->param;
35+
}
36+
}
37+
38+
2339
$compiler = new DI\Compiler;
2440
$compiler->addExtension('extensions', new Nette\DI\Extensions\ExtensionsExtension);
2541
$container = createContainer($compiler, '
42+
parameters:
43+
param: test
44+
2645
extensions:
2746
foo: FooExtension
47+
bar: BarExtension(%param%)
2848
2949
foo:
3050
key: value
3151
');
3252

3353

3454
Assert::same( 'hello', $container->parameters['foo'] );
55+
Assert::same( 'test', $container->parameters['bar'] );

0 commit comments

Comments
 (0)