From 375f83ece49e616f595a5f2b6bb6880e9c871ebc Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 4 Dec 2014 21:13:58 +0100 Subject: [PATCH] Revert "[DependencyInjection] backport perf optim" This reverts commit c11535bd6bff6d00ac5a0206e1c8d3f508bba8f3. Conflicts: src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php --- .../DependencyInjection/Dumper/PhpDumper.php | 42 ++++++++++----- .../Tests/Dumper/PhpDumperTest.php | 15 ++++++ .../Tests/Fixtures/php/services1-1.php | 8 +-- .../Tests/Fixtures/php/services1.php | 8 +-- .../Tests/Fixtures/php/services10.php | 20 +++++-- .../Tests/Fixtures/php/services11.php | 54 +++++++++++++++++++ .../Tests/Fixtures/php/services12.php | 24 ++++++--- .../Tests/Fixtures/php/services8.php | 16 ++++-- .../Tests/Fixtures/php/services9.php | 24 ++++++--- .../Tests/Fixtures/php/services9_compiled.php | 22 +++++--- 10 files changed, 179 insertions(+), 54 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 0c7ac26cb8..f3e5fdb3cf 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -767,7 +767,7 @@ $bagClass class $class extends $baseClass { private \$parameters; - private \$targetDirs; + private \$targetDirs = array(); EOF; } @@ -779,8 +779,8 @@ EOF; */ private function addConstructor() { - $parameters = $this->exportParameters($this->container->getParameterBag()->all()); $targetDirs = $this->exportTargetDirs(); + $arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null; $code = <<parameters = $parameters; - - parent::__construct(new ParameterBag(\$this->parameters)); + parent::__construct($arguments); EOF; @@ -819,7 +817,6 @@ EOF; */ private function addFrozenConstructor() { - $parameters = $this->exportParameters($this->container->getParameterBag()->all()); $targetDirs = $this->exportTargetDirs(); $code = <<container->getParameterBag()->all()) { + $code .= "\n \$this->parameters = \$this->getDefaultParameters();\n"; + } + + $code .= <<services = \$this->scopedServices = \$this->scopeStacks = array(); - \$this->parameters = $parameters; \$this->set('service_container', \$this); @@ -917,6 +921,8 @@ EOF; return ''; } + $parameters = $this->exportParameters($this->container->getParameterBag()->all()); + $code = ''; if ($this->container->isFrozen()) { $code .= <<parameterBag; } - EOF; } + $code .= <<targetDirMaxMatches - count($matches)) { - $dirname = sprintf('$this->targetDirs[%d]', $dirname); - } else { - $dirname = '__DIR__'; + if (0 < $offset = 1 + $this->targetDirMaxMatches - count($matches)) { + $dirname = sprintf('$this->targetDirs[%d]', $offset); } if ($prefix || $suffix) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index e240376fd2..905e600e78 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -37,6 +37,21 @@ 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(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php index 2834baef42..e83498f2c5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php @@ -17,17 +17,13 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class Container extends AbstractContainer { private $parameters; - private $targetDirs; + private $targetDirs = array(); /** * Constructor. */ public function __construct() { - $this->parameters = array( - - ); - - parent::__construct(new ParameterBag($this->parameters)); + parent::__construct(); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php index aa4e70f51e..90b59ff4eb 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php @@ -17,17 +17,13 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class ProjectServiceContainer extends Container { private $parameters; - private $targetDirs; + private $targetDirs = array(); /** * Constructor. */ public function __construct() { - $this->parameters = array( - - ); - - parent::__construct(new ParameterBag($this->parameters)); + parent::__construct(); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php index 55196f882a..43a56eec51 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php @@ -17,20 +17,18 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; class ProjectServiceContainer extends Container { private $parameters; - private $targetDirs; + private $targetDirs = array(); /** * Constructor. */ public function __construct() { + $this->parameters = $this->getDefaultParameters(); + $this->services = $this->scopedServices = $this->scopeStacks = array(); - $this->parameters = array( - 'empty_value' => '', - 'some_string' => '-', - ); $this->set('service_container', $this); @@ -99,4 +97,16 @@ class ProjectServiceContainer extends Container return $this->parameterBag; } + /** + * Gets the default parameters. + * + * @return array An array of the default parameters + */ + protected function getDefaultParameters() + { + return array( + 'empty_value' => '', + 'some_string' => '-', + ); + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php new file mode 100644 index 0000000000..23bae47920 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php @@ -0,0 +1,54 @@ +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(); + } + + /** + * 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(); + } +} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php index f38d7d9eaa..3081abd6b0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php @@ -17,7 +17,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; class ProjectServiceContainer extends Container { private $parameters; - private $targetDirs; + private $targetDirs = array(); /** * Constructor. @@ -28,15 +28,11 @@ class ProjectServiceContainer extends Container for ($i = 1; $i <= 5; ++$i) { $this->targetDirs[$i] = $dir = dirname($dir); } + $this->parameters = $this->getDefaultParameters(); + $this->services = $this->scopedServices = $this->scopeStacks = array(); - $this->parameters = array( - 'foo' => ('wiz'.$this->targetDirs[1]), - 'bar' => __DIR__, - 'baz' => (__DIR__.'/PhpDumperTest.php'), - 'buz' => $this->targetDirs[2], - ); $this->set('service_container', $this); @@ -105,4 +101,18 @@ 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'.$this->targetDirs[1]), + 'bar' => __DIR__, + 'baz' => (__DIR__.'/PhpDumperTest.php'), + 'buz' => $this->targetDirs[2], + ); + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php index 558c284e8b..1b86dfc3f8 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php @@ -17,14 +17,24 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class ProjectServiceContainer extends Container { private $parameters; - private $targetDirs; + private $targetDirs = array(); /** * Constructor. */ public function __construct() { - $this->parameters = array( + parent::__construct(new ParameterBag($this->getDefaultParameters())); + } + + /** + * Gets the default parameters. + * + * @return array An array of the default parameters + */ + protected function getDefaultParameters() + { + return array( 'foo' => '%baz%', 'baz' => 'bar', 'bar' => 'foo is %%foo bar', @@ -40,7 +50,5 @@ class ProjectServiceContainer extends Container 7 => 'null', ), ); - - parent::__construct(new ParameterBag($this->parameters)); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index c993d96ec7..ecbd626abe 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -17,20 +17,14 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class ProjectServiceContainer extends Container { private $parameters; - private $targetDirs; + private $targetDirs = array(); /** * Constructor. */ public function __construct() { - $this->parameters = array( - 'baz_class' => 'BazClass', - 'foo_class' => 'FooClass', - 'foo' => 'bar', - ); - - parent::__construct(new ParameterBag($this->parameters)); + parent::__construct(new ParameterBag($this->getDefaultParameters())); $this->methodMap = array( 'bar' => 'getBarService', 'baz' => 'getBazService', @@ -254,4 +248,18 @@ class ProjectServiceContainer extends Container return $instance; } + + /** + * Gets the default parameters. + * + * @return array An array of the default parameters + */ + protected function getDefaultParameters() + { + return array( + 'baz_class' => 'BazClass', + 'foo_class' => 'FooClass', + 'foo' => 'bar', + ); + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 12a0653c48..2e7e10480b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -17,21 +17,18 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; class ProjectServiceContainer extends Container { private $parameters; - private $targetDirs; + private $targetDirs = array(); /** * Constructor. */ public function __construct() { + $this->parameters = $this->getDefaultParameters(); + $this->services = $this->scopedServices = $this->scopeStacks = array(); - $this->parameters = array( - 'baz_class' => 'BazClass', - 'foo_class' => 'FooClass', - 'foo' => 'bar', - ); $this->set('service_container', $this); @@ -278,4 +275,17 @@ class ProjectServiceContainer extends Container return $this->parameterBag; } + /** + * Gets the default parameters. + * + * @return array An array of the default parameters + */ + protected function getDefaultParameters() + { + return array( + 'baz_class' => 'BazClass', + 'foo_class' => 'FooClass', + 'foo' => 'bar', + ); + } }