Skip to content

Commit

Permalink
DI\ContainerBuilder: added support for aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 24, 2011
1 parent 6710fc4 commit a0032d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
26 changes: 21 additions & 5 deletions Nette/DI/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ class ContainerBuilder extends Nette\Object
public function addDefinitions(IContainer $container, array $definitions)
{
foreach ($definitions as $name => $definition) {
if (!is_array($definition)) {
$definition = array('class' => $definition);
if (is_scalar($definition)) {
if ($definition[0] === '@') {
$definition = array('alias' => substr($definition, 1));
} else {
$definition = array('class' => $definition);
}
}

$arguments = isset($definition['arguments']) ? $definition['arguments'] : array();
Expand Down Expand Up @@ -75,6 +79,10 @@ public function addDefinitions(IContainer $container, array $definitions)
$factory = $arguments[0]; $arguments[0] = $container;
return call_user_func_array($factory, $arguments);
};
} elseif (isset($definition['alias'])) {
$factory = function($container) use ($definition) {
return $container->getService($definition['alias']);
};
} else {
throw new Nette\InvalidStateException("The definition of service '$name' is missing factory method.");
}
Expand All @@ -97,9 +105,13 @@ public function generateCode(array $definitions)
foreach ($definitions as $name => $definition) {
$name = $this->varExport($name);
if (is_scalar($definition)) {
$factory = $this->varExport($definition);
$code .= "\$container->addService($name, $factory);\n\n";
continue;
if ($definition[0] === '@') {
$definition = array('alias' => substr($definition, 1));
} else {
$factory = $this->varExport($definition);
$code .= "\$container->addService($name, $factory);\n\n";
continue;
}
}

$arguments = $this->argsExport(isset($definition['arguments']) ? $definition['arguments'] : array());
Expand All @@ -118,6 +130,10 @@ public function generateCode(array $definitions)
$factory = $this->argsExport(array($definition['factory']));
$factory = "function(\$container) {\n\treturn call_user_func(\n\t\t$factory,\n\t\t\$container"
. ($arguments ? ",\n\t\t$arguments" : '') . "\n\t);\n}";

} elseif (isset($definition['alias'])) {
$factory = $this->varExport($definition['alias']);
$factory = "function(\$container) {\n\treturn \$container->getService($factory);\n}";
} else {
throw new Nette\InvalidStateException("The definition of service '$name' is missing factory method.");
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Nette/DI/ContainerBuilder.code.expand.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ $code = $builder->generateCode(array(
'three' => array(
'factory' => array('%serviceClass%', 'create'),
),
'four' => '@three',
));

file_put_contents(TEMP_DIR . '/code.php', "<?php\n$code");
Expand All @@ -84,6 +85,7 @@ Assert::true( $container->getService('two') instanceof Service );
Assert::equal( array(array(1 => 'a', $container->getService('one'))), $container->getService('two')->args );

Assert::true( $container->getService('three') instanceof Service );
Assert::true( $container->getService('four') instanceof Service );


$builder->addDefinitions($container, array(
Expand Down
2 changes: 2 additions & 0 deletions tests/Nette/DI/ContainerBuilder.expand.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ $builder->addDefinitions($container, array(
'three' => array(
'factory' => array('%serviceClass%', 'create'),
),
'four' => '@three',
));

Assert::true( $container->getService('one') instanceof Service );
Expand All @@ -82,6 +83,7 @@ Assert::true( $container->getService('two') instanceof Service );
Assert::equal( array(array(1 => 'a', $container->getService('one'))), $container->getService('two')->args );

Assert::true( $container->getService('three') instanceof Service );
Assert::true( $container->getService('four') instanceof Service );


$builder->addDefinitions($container, array(
Expand Down

0 comments on commit a0032d5

Please sign in to comment.