From c11535bd6bff6d00ac5a0206e1c8d3f508bba8f3 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 3 Dec 2014 10:22:11 +0100 Subject: [PATCH 1/3] [DependencyInjection] backport perf optim Conflicts: src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services19.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php --- .../DependencyInjection/Dumper/PhpDumper.php | 36 ++++--------- .../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 | 51 ------------------- .../Tests/Fixtures/php/services12.php | 22 +++----- .../Tests/Fixtures/php/services8.php | 16 ++---- .../Tests/Fixtures/php/services9.php | 24 ++++----- .../Tests/Fixtures/php/services9_compiled.php | 22 +++----- 10 files changed, 58 insertions(+), 164 deletions(-) delete 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 5e9208e5cf..58b34e39e9 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -764,6 +764,8 @@ $bagClass */ class $class extends $baseClass { + private \$parameters; + EOF; } @@ -774,7 +776,7 @@ EOF; */ private function addConstructor() { - $arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null; + $parameters = $this->exportParameters($this->container->getParameterBag()->all()); $code = <<parameters = $parameters; + + parent::__construct(new ParameterBag(\$this->parameters)); EOF; @@ -811,6 +815,8 @@ EOF; */ private function addFrozenConstructor() { + $parameters = $this->exportParameters($this->container->getParameterBag()->all()); + $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); @@ -913,8 +912,6 @@ EOF; return ''; } - $parameters = $this->exportParameters($this->container->getParameterBag()->all()); - $code = ''; if ($this->container->isFrozen()) { $code .= <<parameterBag; } + EOF; } - $code .= <<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 547941eb95..560da91642 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php @@ -16,11 +16,17 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; */ class Container extends AbstractContainer { + private $parameters; + /** * Constructor. */ public function __construct() { - parent::__construct(); + $this->parameters = array( + + ); + + parent::__construct(new ParameterBag($this->parameters)); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php index 084e7891af..23d367f1f9 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php @@ -16,11 +16,17 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + /** * Constructor. */ public function __construct() { - parent::__construct(); + $this->parameters = array( + + ); + + parent::__construct(new ParameterBag($this->parameters)); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php index 37927a6e70..bf64894f58 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php @@ -16,16 +16,20 @@ 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( + 'empty_value' => '', + 'some_string' => '-', + ); $this->set('service_container', $this); @@ -94,16 +98,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( - '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 deleted file mode 100644 index 0dcd83845a..0000000000 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services11.php +++ /dev/null @@ -1,51 +0,0 @@ -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 52d2702afd..1829818dbc 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php @@ -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); @@ -94,17 +99,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'), - ); - } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php index f922d2000c..a6de38e381 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php @@ -16,22 +16,14 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + /** * Constructor. */ public function __construct() { - parent::__construct(new ParameterBag($this->getDefaultParameters())); - } - - /** - * Gets the default parameters. - * - * @return array An array of the default parameters - */ - protected function getDefaultParameters() - { - return array( + $this->parameters = array( 'foo' => '%baz%', 'baz' => 'bar', 'bar' => 'foo is %%foo bar', @@ -47,5 +39,7 @@ 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 a4b42d78ad..90b5476776 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -16,12 +16,20 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; */ class ProjectServiceContainer extends Container { + private $parameters; + /** * Constructor. */ public function __construct() { - parent::__construct(new ParameterBag($this->getDefaultParameters())); + $this->parameters = array( + 'baz_class' => 'BazClass', + 'foo_class' => 'FooClass', + 'foo' => 'bar', + ); + + parent::__construct(new ParameterBag($this->parameters)); $this->methodMap = array( 'bar' => 'getBarService', 'baz' => 'getBazService', @@ -245,18 +253,4 @@ 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 b881b5e300..97fe3ad8a0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -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( + 'baz_class' => 'BazClass', + 'foo_class' => 'FooClass', + 'foo' => 'bar', + ); $this->set('service_container', $this); @@ -272,17 +277,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( - 'baz_class' => 'BazClass', - 'foo_class' => 'FooClass', - 'foo' => 'bar', - ); - } } From fcd8ff9b67fd63159e5e37dd529de38ff73d6431 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 4 Dec 2014 19:27:52 +0100 Subject: [PATCH 2/3] [DependencyInjection] perf optim: call dirname() at most 5x --- .../DependencyInjection/Dumper/PhpDumper.php | 33 ++++++++++++++----- .../Tests/Dumper/PhpDumperTest.php | 3 +- .../Tests/Fixtures/php/services1-1.php | 1 + .../Tests/Fixtures/php/services1.php | 1 + .../Tests/Fixtures/php/services10.php | 1 + .../Tests/Fixtures/php/services12.php | 10 ++++-- .../Tests/Fixtures/php/services8.php | 1 + .../Tests/Fixtures/php/services9.php | 1 + .../Tests/Fixtures/php/services9_compiled.php | 1 + 9 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 58b34e39e9..0c7ac26cb8 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -104,17 +104,19 @@ class PhpDumper extends Dumper ), $options); if (!empty($options['file']) && is_dir($dir = dirname($options['file']))) { - // Build a regexp where the first two root dirs are mandatory, + // Build a regexp where the first root dirs are mandatory, // but every other sub-dir is optional up to the full path in $dir + // Mandate at least 2 root dirs and not more that 5 optional dirs. $dir = explode(DIRECTORY_SEPARATOR, realpath($dir)); $i = count($dir); if (3 <= $i) { $regex = ''; - $this->targetDirMaxMatches = $i - 3; + $lastOptionalDir = $i > 8 ? $i - 5 : 3; + $this->targetDirMaxMatches = $i - $lastOptionalDir; - while (2 < --$i) { + while (--$i >= $lastOptionalDir) { $regex = sprintf('(%s%s)?', preg_quote(DIRECTORY_SEPARATOR.$dir[$i], '#'), $regex); } @@ -765,6 +767,7 @@ $bagClass class $class extends $baseClass { private \$parameters; + private \$targetDirs; EOF; } @@ -777,6 +780,7 @@ EOF; private function addConstructor() { $parameters = $this->exportParameters($this->container->getParameterBag()->all()); + $targetDirs = $this->exportTargetDirs(); $code = <<parameters = $parameters; parent::__construct(new ParameterBag(\$this->parameters)); @@ -816,6 +820,7 @@ EOF; private function addFrozenConstructor() { $parameters = $this->exportParameters($this->container->getParameterBag()->all()); + $targetDirs = $this->exportTargetDirs(); $code = <<services = \$this->scopedServices = \$this->scopeStacks = array(); @@ -1335,16 +1340,28 @@ EOF; } } + private function exportTargetDirs() + { + return null === $this->targetDirRegex ? '' : <<targetDirMaxMatches}; ++\$i) { + \$this->targetDirs[\$i] = \$dir = dirname(\$dir); + } +EOF; + } + private function export($value) { if (null !== $this->targetDirRegex && is_string($value) && preg_match($this->targetDirRegex, $value, $matches, PREG_OFFSET_CAPTURE)) { $prefix = $matches[0][1] ? var_export(substr($value, 0, $matches[0][1]), true).'.' : ''; $suffix = $matches[0][1] + strlen($matches[0][0]); $suffix = isset($value[$suffix]) ? '.'.var_export(substr($value, $suffix), true) : ''; - $dirname = '__DIR__'; - for ($i = $this->targetDirMaxMatches - count($matches); 0 <= $i; --$i) { - $dirname = sprintf('dirname(%s)', $dirname); + if (0 < $dirname = 1 + $this->targetDirMaxMatches - count($matches)) { + $dirname = sprintf('$this->targetDirs[%d]', $dirname); + } else { + $dirname = '__DIR__'; } if ($prefix || $suffix) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 4eca6aa849..e240376fd2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -70,13 +70,14 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase $definition = new Definition(); $definition->setClass('stdClass'); $definition->addArgument('%foo%'); - $definition->addArgument(array('%foo%' => '%foo%')); + $definition->addArgument(array('%foo%' => '%buz%/')); $container = new ContainerBuilder(); $container->setDefinition('test', $definition); $container->setParameter('foo', 'wiz'.dirname(dirname(__FILE__))); $container->setParameter('bar', dirname(__FILE__)); $container->setParameter('baz', '%bar%/PhpDumperTest.php'); + $container->setParameter('buz', dirname(dirname(__DIR__))); $container->compile(); $dumper = new PhpDumper($container); 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 560da91642..2834baef42 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php @@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class Container extends AbstractContainer { private $parameters; + private $targetDirs; /** * Constructor. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php index 23d367f1f9..aa4e70f51e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1.php @@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class ProjectServiceContainer extends Container { private $parameters; + private $targetDirs; /** * Constructor. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php index bf64894f58..55196f882a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services10.php @@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; class ProjectServiceContainer extends Container { private $parameters; + private $targetDirs; /** * Constructor. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php index 1829818dbc..f38d7d9eaa 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services12.php @@ -17,19 +17,25 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; class ProjectServiceContainer extends Container { private $parameters; + private $targetDirs; /** * Constructor. */ public function __construct() { + $dir = __DIR__; + for ($i = 1; $i <= 5; ++$i) { + $this->targetDirs[$i] = $dir = dirname($dir); + } $this->services = $this->scopedServices = $this->scopeStacks = array(); $this->parameters = array( - 'foo' => ('wiz'.dirname(__DIR__)), + 'foo' => ('wiz'.$this->targetDirs[1]), 'bar' => __DIR__, 'baz' => (__DIR__.'/PhpDumperTest.php'), + 'buz' => $this->targetDirs[2], ); $this->set('service_container', $this); @@ -53,7 +59,7 @@ class ProjectServiceContainer extends Container */ protected function getTestService() { - return $this->services['test'] = new \stdClass(('wiz'.dirname(__DIR__)), array(('wiz'.dirname(__DIR__)) => ('wiz'.dirname(__DIR__)))); + return $this->services['test'] = new \stdClass(('wiz'.$this->targetDirs[1]), array(('wiz'.$this->targetDirs[1]) => ($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 a6de38e381..558c284e8b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services8.php @@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class ProjectServiceContainer extends Container { private $parameters; + private $targetDirs; /** * Constructor. diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php index 90b5476776..c993d96ec7 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php @@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class ProjectServiceContainer extends Container { private $parameters; + private $targetDirs; /** * Constructor. 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 97fe3ad8a0..12a0653c48 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; class ProjectServiceContainer extends Container { private $parameters; + private $targetDirs; /** * Constructor. From 375f83ece49e616f595a5f2b6bb6880e9c871ebc Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 4 Dec 2014 21:13:58 +0100 Subject: [PATCH 3/3] 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', + ); + } }