Skip to content

Commit

Permalink
bug #24850 [DI] Fix cannot bind env var (ogizanagi)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Fix cannot bind env var

| Q             | A
| ------------- | ---
| Branch?       | 3.4 <!-- see comment below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #24845 <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

In #24602 we removed the processing of bindings from the `AbstractRecursivePass`. But there is actually one case where we want a recursive pass to process them: to resolve env param placeholders.

Commits
-------

f8f3a15 [DI] Fix cannot bind env var
  • Loading branch information
nicolas-grekas committed Nov 7, 2017
2 parents 573475d + 07045ce commit d937729
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions Compiler/ResolveParameterPlaceHoldersPass.php
Expand Up @@ -66,6 +66,7 @@ protected function processValue($value, $isRoot = false)
return $this->resolveArrays || !$v || !is_array($v) ? $v : $value;
}
if ($value instanceof Definition) {
$value->setBindings($this->processValue($value->getBindings()));
$changes = $value->getChanges();
if (isset($changes['class'])) {
$value->setClass($this->bag->resolveValue($value->getClass()));
Expand Down
8 changes: 8 additions & 0 deletions Tests/Compiler/ResolveParameterPlaceHoldersPassTest.php
Expand Up @@ -64,6 +64,13 @@ public function testAliasParametersShouldBeResolved()
$this->assertSame('foo', $this->container->getAlias('bar')->__toString());
}

public function testBindingsShouldBeResolved()
{
list($boundValue) = $this->container->getDefinition('foo')->getBindings()['$baz']->getValues();

$this->assertSame($this->container->getParameterBag()->resolveValue('%env(BAZ)%'), $boundValue);
}

private function createContainerBuilder()
{
$containerBuilder = new ContainerBuilder();
Expand All @@ -84,6 +91,7 @@ private function createContainerBuilder()
$fooDefinition->addMethodCall('%foo.method%', array('%foo.arg1%', '%foo.arg2%'));
$fooDefinition->setProperty('%foo.property.name%', '%foo.property.value%');
$fooDefinition->setFile('%foo.file%');
$fooDefinition->setBindings(array('$baz' => '%env(BAZ)%'));

$containerBuilder->setAlias('%alias.id%', 'foo');

Expand Down
11 changes: 11 additions & 0 deletions Tests/Fixtures/php/services26.php
Expand Up @@ -29,6 +29,7 @@ public function __construct()

$this->services = array();
$this->methodMap = array(
'bar' => 'getBarService',
'test' => 'getTestService',
);

Expand Down Expand Up @@ -60,6 +61,16 @@ public function isFrozen()
return true;
}

/**
* Gets the public 'bar' shared service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\Bar
*/
protected function getBarService()
{
return $this->services['bar'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\Bar($this->getEnv('QUZ'));
}

/**
* Gets the public 'test' shared service.
*
Expand Down
5 changes: 5 additions & 0 deletions Tests/Fixtures/yaml/services26.yml
Expand Up @@ -15,3 +15,8 @@ services:
- '%env(Bar)%'
- 'foo%bar%baz'
- '%baz%'
bar:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar
public: true
bind:
$quz: '%env(QUZ)%'

0 comments on commit d937729

Please sign in to comment.