Skip to content

[DependencyInjection] Support for parameter injection #20738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
class AutowirePass implements CompilerPassInterface
{
/**
* @var ContainerBuilder
*/
private $container;
private $reflectionClasses = array();
private $definedTypes = array();
Expand All @@ -40,6 +43,7 @@ public function process(ContainerBuilder $container)

try {
$this->container = $container;

foreach ($container->getDefinitions() as $id => $definition) {
if ($definition->isAutowired()) {
$this->completeDefinition($id, $definition);
Expand Down Expand Up @@ -107,6 +111,11 @@ private function completeDefinition($id, Definition $definition)
continue;
}

if ($this->container->hasParameter($parameter->name)) {
$arguments[$index] = $this->container->getParameter($parameter->name);
continue;
}

try {
if (!$typeHint = $parameter->getClass()) {
// no default value? Then fail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,20 @@ public function testIgnoreServiceWithClassNotExisting()

$this->assertTrue($container->hasDefinition('bar'));
}

public function testParameterInjection()
{
$container = new ContainerBuilder();
$container->setParameter('myParameter', 'SymfonyCon Berlin Hackday!');

$parameterInjectionDefinition = $container->register(ParameterInjection::class, ParameterInjection::class);
$parameterInjectionDefinition->setAutowired(true);

$pass = new AutowirePass();
$pass->process($container);

$this->assertEquals('SymfonyCon Berlin Hackday!', $parameterInjectionDefinition->getArgument(1));
}
}

class Foo
Expand Down Expand Up @@ -654,3 +668,10 @@ public function __construct($foo, Bar $bar, $baz)
{
}
}

class ParameterInjection
{
public function __construct(Foo $foo, $myParameter)
{
}
}