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:
Fabien Potencier 2013-07-07 17:47:47 +02:00
commit eaf9ede606
4 changed files with 21 additions and 1 deletions

View File

@ -849,7 +849,11 @@ EOF;
private function addAliases()
{
if (!$aliases = $this->container->getAliases()) {
return '';
if ($this->container->isFrozen()) {
return "\n \$this->aliases = array();\n";
} else {
return '';
}
}
$code = " \$this->aliases = array(\n";

View File

@ -134,6 +134,18 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$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()
{
require_once self::$fixturesPath.'/php/services9.php';

View File

@ -36,6 +36,8 @@ class ProjectServiceContainer extends Container
$this->methodMap = array(
'test' => 'getTestService',
);
$this->aliases = array();
}
/**

View File

@ -34,6 +34,8 @@ class ProjectServiceContainer extends Container
$this->methodMap = array(
'foo' => 'getFooService',
);
$this->aliases = array();
}
/**