merged branch WouterJ/di_alias_dumper_bug (PR #8435)
This PR was squashed before being merged into the 2.3 branch (closes #8435).
Discussion
----------
[DI] Fixed bug requesting non existing service from dumped frozen container
When dumping a frozen container without aliases, the method `Container::has()` will throw a *"Warning: array_key_exists() expects parameter 2 to be array, null given"* warning. It's because `PhpDumper::addAliases` returns `''` if no aliases are given. This will work for a normal constructor, because it calls the `Container::__construct` method, but the frozen constructor doesn't do that. So it requires a `$this->aliases = array();` in the constructor.
This PR fixes this bug (and adds the test). Bug is introduced in #8252 and 63367a3d5615ff65251ccee4f73223107275f65f
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Commits
-------
8bb4e4d
[DI] Fixed bug requesting non existing service from dumped frozen container
This commit is contained in:
commit
eaf9ede606
@ -849,7 +849,11 @@ EOF;
|
|||||||
private function addAliases()
|
private function addAliases()
|
||||||
{
|
{
|
||||||
if (!$aliases = $this->container->getAliases()) {
|
if (!$aliases = $this->container->getAliases()) {
|
||||||
return '';
|
if ($this->container->isFrozen()) {
|
||||||
|
return "\n \$this->aliases = array();\n";
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$code = " \$this->aliases = array(\n";
|
$code = " \$this->aliases = array(\n";
|
||||||
|
@ -134,6 +134,18 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame($foo, $container->get('alias_for_alias'));
|
$this->assertSame($foo, $container->get('alias_for_alias'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFrozenContainerWithoutAliases()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->compile();
|
||||||
|
|
||||||
|
$dumper = new PhpDumper($container);
|
||||||
|
eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases')));
|
||||||
|
|
||||||
|
$container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases();
|
||||||
|
$this->assertFalse($container->has('foo'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testOverrideServiceWhenUsingADumpedContainer()
|
public function testOverrideServiceWhenUsingADumpedContainer()
|
||||||
{
|
{
|
||||||
require_once self::$fixturesPath.'/php/services9.php';
|
require_once self::$fixturesPath.'/php/services9.php';
|
||||||
|
@ -36,6 +36,8 @@ class ProjectServiceContainer extends Container
|
|||||||
$this->methodMap = array(
|
$this->methodMap = array(
|
||||||
'test' => 'getTestService',
|
'test' => 'getTestService',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->aliases = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,6 +34,8 @@ class ProjectServiceContainer extends Container
|
|||||||
$this->methodMap = array(
|
$this->methodMap = array(
|
||||||
'foo' => 'getFooService',
|
'foo' => 'getFooService',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->aliases = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user