bug #12823 [DependencyInjection] fix PhpDumper (nicolas-grekas)

This PR was merged into the 2.6 branch.

Discussion
----------

[DependencyInjection] fix PhpDumper

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12815
| License       | MIT
| Doc PR        | none

Commits
-------

7887a46 [DependencyInjection] keep some of the reverted perf optim
2f0b355 Revert "minor #10241 [DependencyInjection] made some perf improvements (fabpot)"
This commit is contained in:
Fabien Potencier 2014-12-03 10:28:04 +01:00
commit 73149ae819
11 changed files with 79 additions and 144 deletions

View File

@ -823,6 +823,8 @@ $bagClass
*/
class $class extends $baseClass
{
private \$parameters;
EOF;
}
@ -837,14 +839,14 @@ EOF;
$code = <<<EOF
private static \$parameters = $parameters;
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(new ParameterBag(self::\$parameters));
\$this->parameters = $parameters;
parent::__construct(new ParameterBag(\$this->parameters));
EOF;
@ -876,8 +878,6 @@ EOF;
$code = <<<EOF
private static \$parameters = $parameters;
/**
* Constructor.
*/
@ -886,6 +886,7 @@ EOF;
\$this->services =
\$this->scopedServices =
\$this->scopeStacks = array();
\$this->parameters = $parameters;
\$this->set('service_container', \$this);
@ -1001,11 +1002,11 @@ EOF;
{
\$name = strtolower(\$name);
if (!(isset(self::\$parameters[\$name]) || array_key_exists(\$name, self::\$parameters))) {
if (!(isset(\$this->parameters[\$name]) || array_key_exists(\$name, \$this->parameters))) {
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', \$name));
}
return self::\$parameters[\$name];
return \$this->parameters[\$name];
}
/**
@ -1015,7 +1016,7 @@ EOF;
{
\$name = strtolower(\$name);
return isset(self::\$parameters[\$name]) || array_key_exists(\$name, self::\$parameters);
return isset(\$this->parameters[\$name]) || array_key_exists(\$name, \$this->parameters);
}
/**
@ -1032,7 +1033,7 @@ EOF;
public function getParameterBag()
{
if (null === \$this->parameterBag) {
\$this->parameterBag = new FrozenParameterBag(self::\$parameters);
\$this->parameterBag = new FrozenParameterBag(\$this->parameters);
}
return \$this->parameterBag;

View File

@ -37,21 +37,6 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
new PhpDumper($container);
}
public function testDumpFrozenContainerWithNoParameter()
{
$container = new ContainerBuilder();
$container->setResourceTracking(false);
$container->register('foo', 'stdClass');
$container->compile();
$dumper = new PhpDumper($container);
$dumpedString = $dumper->dump();
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services11.php', $dumpedString, '->dump() does not add getDefaultParameters() method call if container have no parameters.');
$this->assertNotRegexp("/function getDefaultParameters\(/", $dumpedString, '->dump() does not add getDefaultParameters() method definition.');
}
public function testDumpOptimizationString()
{
$definition = new Definition();

View File

@ -17,15 +17,17 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class Container extends AbstractContainer
{
private static $parameters = array(
);
private $parameters;
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(new ParameterBag(self::$parameters));
$this->parameters = array(
);
parent::__construct(new ParameterBag($this->parameters));
}
}

View File

@ -16,15 +16,17 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class ProjectServiceContainer extends Container
{
private static $parameters = array(
);
private $parameters;
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(new ParameterBag(self::$parameters));
$this->parameters = array(
);
parent::__construct(new ParameterBag($this->parameters));
}
}

View File

@ -16,10 +16,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*/
class ProjectServiceContainer extends Container
{
private static $parameters = array(
'empty_value' => '',
'some_string' => '-',
);
private $parameters;
/**
* Constructor.
@ -29,6 +26,10 @@ class ProjectServiceContainer extends Container
$this->services =
$this->scopedServices =
$this->scopeStacks = array();
$this->parameters = array(
'empty_value' => '',
'some_string' => '-',
);
$this->set('service_container', $this);
@ -69,11 +70,11 @@ class ProjectServiceContainer extends Container
{
$name = strtolower($name);
if (!(isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters))) {
if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) {
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
}
return self::$parameters[$name];
return $this->parameters[$name];
}
/**
@ -83,7 +84,7 @@ class ProjectServiceContainer extends Container
{
$name = strtolower($name);
return isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters);
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
}
/**
@ -100,7 +101,7 @@ class ProjectServiceContainer extends Container
public function getParameterBag()
{
if (null === $this->parameterBag) {
$this->parameterBag = new FrozenParameterBag(self::$parameters);
$this->parameterBag = new FrozenParameterBag($this->parameters);
}
return $this->parameterBag;

View File

@ -1,63 +0,0 @@
<?php
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
/**
* ProjectServiceContainer
*
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*/
class ProjectServiceContainer extends Container
{
private static $parameters = array(
);
/**
* Constructor.
*/
public function __construct()
{
$this->services =
$this->scopedServices =
$this->scopeStacks = array();
$this->set('service_container', $this);
$this->scopes = array();
$this->scopeChildren = array();
$this->methodMap = array(
'foo' => 'getFooService',
);
$this->aliases = array();
}
/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}
/**
* Gets the 'foo' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \stdClass A stdClass instance.
*/
protected function getFooService()
{
return $this->services['foo'] = new \stdClass();
}
}

View File

@ -16,16 +16,21 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*/
class ProjectServiceContainer extends Container
{
private $parameters;
/**
* Constructor.
*/
public function __construct()
{
$this->parameters = $this->getDefaultParameters();
$this->services =
$this->scopedServices =
$this->scopeStacks = array();
$this->parameters = array(
'foo' => ('wiz'.dirname(__DIR__)),
'bar' => __DIR__,
'baz' => (__DIR__.'/PhpDumperTest.php'),
);
$this->set('service_container', $this);
@ -38,6 +43,14 @@ class ProjectServiceContainer extends Container
$this->aliases = array();
}
/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}
/**
* Gets the 'test' service.
*
@ -94,17 +107,4 @@ class ProjectServiceContainer extends Container
return $this->parameterBag;
}
/**
* Gets the default parameters.
*
* @return array An array of the default parameters
*/
protected function getDefaultParameters()
{
return array(
'foo' => ('wiz'.dirname(__DIR__)),
'bar' => __DIR__,
'baz' => (__DIR__.'/PhpDumperTest.php'),
);
}
}

View File

@ -16,16 +16,18 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class ProjectServiceContainer extends Container
{
private static $parameters = array(
);
private $parameters;
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(new ParameterBag(self::$parameters));
$this->parameters = array(
);
parent::__construct(new ParameterBag($this->parameters));
$this->methodMap = array(
'service_from_anonymous_factory' => 'getServiceFromAnonymousFactoryService',
'service_with_method_call_and_factory' => 'getServiceWithMethodCallAndFactoryService',

View File

@ -16,7 +16,14 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class ProjectServiceContainer extends Container
{
private static $parameters = array(
private $parameters;
/**
* Constructor.
*/
public function __construct()
{
$this->parameters = array(
'foo' => '%baz%',
'baz' => 'bar',
'bar' => 'foo is %%foo bar',
@ -33,11 +40,6 @@ class ProjectServiceContainer extends Container
),
);
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(new ParameterBag(self::$parameters));
parent::__construct(new ParameterBag($this->parameters));
}
}

View File

@ -16,18 +16,20 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
*/
class ProjectServiceContainer extends Container
{
private static $parameters = array(
'baz_class' => 'BazClass',
'foo_class' => 'Bar\\FooClass',
'foo' => 'bar',
);
private $parameters;
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(new ParameterBag(self::$parameters));
$this->parameters = array(
'baz_class' => 'BazClass',
'foo_class' => 'Bar\\FooClass',
'foo' => 'bar',
);
parent::__construct(new ParameterBag($this->parameters));
$this->methodMap = array(
'bar' => 'getBarService',
'baz' => 'getBazService',

View File

@ -16,11 +16,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*/
class ProjectServiceContainer extends Container
{
private static $parameters = array(
'baz_class' => 'BazClass',
'foo_class' => 'Bar\\FooClass',
'foo' => 'bar',
);
private $parameters;
/**
* Constructor.
@ -30,6 +26,11 @@ class ProjectServiceContainer extends Container
$this->services =
$this->scopedServices =
$this->scopeStacks = array();
$this->parameters = array(
'baz_class' => 'BazClass',
'foo_class' => 'Bar\\FooClass',
'foo' => 'bar',
);
$this->set('service_container', $this);
@ -336,11 +337,11 @@ class ProjectServiceContainer extends Container
{
$name = strtolower($name);
if (!(isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters))) {
if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) {
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
}
return self::$parameters[$name];
return $this->parameters[$name];
}
/**
@ -350,7 +351,7 @@ class ProjectServiceContainer extends Container
{
$name = strtolower($name);
return isset(self::$parameters[$name]) || array_key_exists($name, self::$parameters);
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
}
/**
@ -367,7 +368,7 @@ class ProjectServiceContainer extends Container
public function getParameterBag()
{
if (null === $this->parameterBag) {
$this->parameterBag = new FrozenParameterBag(self::$parameters);
$this->parameterBag = new FrozenParameterBag($this->parameters);
}
return $this->parameterBag;