Skip to content

Commit

Permalink
ExtensionsExtension: Allow passing parameters to extension constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk authored and dg committed Dec 14, 2014
1 parent b75b07d commit 1b1c49f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/DI/Extensions/ExtensionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class ExtensionsExtension extends Nette\DI\CompilerExtension
public function loadConfiguration()
{
foreach ($this->getConfig() as $name => $class) {
$this->compiler->addExtension($name, new $class);
if ($class instanceof \stdClass) {
$rc = Nette\Reflection\ClassType::from($class->value);
$this->compiler->addExtension($name, $rc->newInstanceArgs($class->attributes));
} else {
$this->compiler->addExtension($name, new $class);
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/DI/Compiler.extension.extensions.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,36 @@ class FooExtension extends DI\CompilerExtension
}


class BarExtension extends DI\CompilerExtension
{
private $param;

public function __construct($param)
{
$this->param = $param;
}

function loadConfiguration()
{
$this->getContainerBuilder()->parameters['bar'] = $this->param;
}
}


$compiler = new DI\Compiler;
$compiler->addExtension('extensions', new Nette\DI\Extensions\ExtensionsExtension);
$container = createContainer($compiler, '
parameters:
param: test
extensions:
foo: FooExtension
bar: BarExtension(%param%)
foo:
key: value
');


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

0 comments on commit 1b1c49f

Please sign in to comment.