minor #23912 [DI] Rererence parameter arrays when possible (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Rererence parameter arrays when possible

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no (perf optim)
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This a minor optim that might save memory and CPU for cases where big array parameters are inlined by DI extensions. This re-references them when possible.

Commits
-------

62c1bb5d3b [DI] Rererence parameter arrays when possible
This commit is contained in:
Fabien Potencier 2017-08-18 10:12:20 +02:00
commit 9306fec43e
3 changed files with 5 additions and 2 deletions

View File

@ -1430,6 +1430,9 @@ EOF;
private function dumpValue($value, $interpolate = true)
{
if (is_array($value)) {
if ($value && $interpolate && false !== $param = array_search($value, $this->container->getParameterBag()->all(), true)) {
return $this->dumpValue("%$param%");
}
$code = array();
foreach ($value as $k => $v) {
$code[] = sprintf('%s => %s', $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate));

View File

@ -607,7 +607,7 @@ class PhpDumperTest extends TestCase
$container->setParameter('array_1', array(123));
$container->setParameter('array_2', array(__DIR__));
$container->register('bar', 'BarClass')
->addMethodCall('setBaz', array('%array_1%', '%array_2%', '%%array_1%%'));
->addMethodCall('setBaz', array('%array_1%', '%array_2%', '%%array_1%%', array(123)));
$container->compile();
$dumper = new PhpDumper($container);

View File

@ -75,7 +75,7 @@ class ProjectServiceContainer extends Container
{
$this->services['bar'] = $instance = new \BarClass();
$instance->setBaz($this->parameters['array_1'], $this->getParameter('array_2'), '%array_1%');
$instance->setBaz($this->parameters['array_1'], $this->getParameter('array_2'), '%array_1%', $this->parameters['array_1']);
return $instance;
}