Skip to content

Commit 055ac70

Browse files
[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
1 parent 7e76a32 commit 055ac70

File tree

10 files changed

+58
-164
lines changed

10 files changed

+58
-164
lines changed

Dumper/PhpDumper.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ private function startClass($class, $baseClass)
764764
*/
765765
class $class extends $baseClass
766766
{
767+
private \$parameters;
768+
767769
EOF;
768770
}
769771

@@ -774,7 +776,7 @@ class $class extends $baseClass
774776
*/
775777
private function addConstructor()
776778
{
777-
$arguments = $this->container->getParameterBag()->all() ? 'new ParameterBag($this->getDefaultParameters())' : null;
779+
$parameters = $this->exportParameters($this->container->getParameterBag()->all());
778780

779781
$code = <<<EOF
780782
@@ -783,7 +785,9 @@ private function addConstructor()
783785
*/
784786
public function __construct()
785787
{
786-
parent::__construct($arguments);
788+
\$this->parameters = $parameters;
789+
790+
parent::__construct(new ParameterBag(\$this->parameters));
787791
788792
EOF;
789793

@@ -811,24 +815,19 @@ public function __construct()
811815
*/
812816
private function addFrozenConstructor()
813817
{
818+
$parameters = $this->exportParameters($this->container->getParameterBag()->all());
819+
814820
$code = <<<EOF
815821
816822
/**
817823
* Constructor.
818824
*/
819825
public function __construct()
820826
{
821-
EOF;
822-
823-
if ($this->container->getParameterBag()->all()) {
824-
$code .= "\n \$this->parameters = \$this->getDefaultParameters();\n";
825-
}
826-
827-
$code .= <<<EOF
828-
829827
\$this->services =
830828
\$this->scopedServices =
831829
\$this->scopeStacks = array();
830+
\$this->parameters = $parameters;
832831
833832
\$this->set('service_container', \$this);
834833
@@ -913,8 +912,6 @@ private function addDefaultParametersMethod()
913912
return '';
914913
}
915914

916-
$parameters = $this->exportParameters($this->container->getParameterBag()->all());
917-
918915
$code = '';
919916
if ($this->container->isFrozen()) {
920917
$code .= <<<EOF
@@ -962,22 +959,9 @@ public function getParameterBag()
962959
963960
return \$this->parameterBag;
964961
}
965-
EOF;
966-
}
967-
968-
$code .= <<<EOF
969-
970-
/**
971-
* Gets the default parameters.
972-
*
973-
* @return array An array of the default parameters
974-
*/
975-
protected function getDefaultParameters()
976-
{
977-
return $parameters;
978-
}
979962
980963
EOF;
964+
}
981965

982966
return $code;
983967
}

Tests/Dumper/PhpDumperTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@ public function testDump()
3737
new PhpDumper($container);
3838
}
3939

40-
public function testDumpFrozenContainerWithNoParameter()
41-
{
42-
$container = new ContainerBuilder();
43-
$container->setResourceTracking(false);
44-
$container->register('foo', 'stdClass');
45-
46-
$container->compile();
47-
48-
$dumper = new PhpDumper($container);
49-
50-
$dumpedString = $dumper->dump();
51-
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services11.php', $dumpedString, '->dump() does not add getDefaultParameters() method call if container have no parameters.');
52-
$this->assertNotRegexp("/function getDefaultParameters\(/", $dumpedString, '->dump() does not add getDefaultParameters() method definition.');
53-
}
54-
5540
public function testDumpOptimizationString()
5641
{
5742
$definition = new Definition();

Tests/Fixtures/php/services1-1.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
*/
1717
class Container extends AbstractContainer
1818
{
19+
private $parameters;
20+
1921
/**
2022
* Constructor.
2123
*/
2224
public function __construct()
2325
{
24-
parent::__construct();
26+
$this->parameters = array(
27+
28+
);
29+
30+
parent::__construct(new ParameterBag($this->parameters));
2531
}
2632
}

Tests/Fixtures/php/services1.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
1921
/**
2022
* Constructor.
2123
*/
2224
public function __construct()
2325
{
24-
parent::__construct();
26+
$this->parameters = array(
27+
28+
);
29+
30+
parent::__construct(new ParameterBag($this->parameters));
2531
}
2632
}

Tests/Fixtures/php/services10.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
1921
/**
2022
* Constructor.
2123
*/
2224
public function __construct()
2325
{
24-
$this->parameters = $this->getDefaultParameters();
25-
2626
$this->services =
2727
$this->scopedServices =
2828
$this->scopeStacks = array();
29+
$this->parameters = array(
30+
'empty_value' => '',
31+
'some_string' => '-',
32+
);
2933

3034
$this->set('service_container', $this);
3135

@@ -94,16 +98,4 @@ public function getParameterBag()
9498

9599
return $this->parameterBag;
96100
}
97-
/**
98-
* Gets the default parameters.
99-
*
100-
* @return array An array of the default parameters
101-
*/
102-
protected function getDefaultParameters()
103-
{
104-
return array(
105-
'empty_value' => '',
106-
'some_string' => '-',
107-
);
108-
}
109101
}

Tests/Fixtures/php/services11.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

Tests/Fixtures/php/services12.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
1921
/**
2022
* Constructor.
2123
*/
2224
public function __construct()
2325
{
24-
$this->parameters = $this->getDefaultParameters();
25-
2626
$this->services =
2727
$this->scopedServices =
2828
$this->scopeStacks = array();
29+
$this->parameters = array(
30+
'foo' => ('wiz'.dirname(__DIR__)),
31+
'bar' => __DIR__,
32+
'baz' => (__DIR__.'/PhpDumperTest.php'),
33+
);
2934

3035
$this->set('service_container', $this);
3136

@@ -94,17 +99,4 @@ public function getParameterBag()
9499

95100
return $this->parameterBag;
96101
}
97-
/**
98-
* Gets the default parameters.
99-
*
100-
* @return array An array of the default parameters
101-
*/
102-
protected function getDefaultParameters()
103-
{
104-
return array(
105-
'foo' => ('wiz'.dirname(__DIR__)),
106-
'bar' => __DIR__,
107-
'baz' => (__DIR__.'/PhpDumperTest.php'),
108-
);
109-
}
110102
}

Tests/Fixtures/php/services8.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,14 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
1921
/**
2022
* Constructor.
2123
*/
2224
public function __construct()
2325
{
24-
parent::__construct(new ParameterBag($this->getDefaultParameters()));
25-
}
26-
27-
/**
28-
* Gets the default parameters.
29-
*
30-
* @return array An array of the default parameters
31-
*/
32-
protected function getDefaultParameters()
33-
{
34-
return array(
26+
$this->parameters = array(
3527
'foo' => '%baz%',
3628
'baz' => 'bar',
3729
'bar' => 'foo is %%foo bar',
@@ -47,5 +39,7 @@ protected function getDefaultParameters()
4739
7 => 'null',
4840
),
4941
);
42+
43+
parent::__construct(new ParameterBag($this->parameters));
5044
}
5145
}

Tests/Fixtures/php/services9.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616
*/
1717
class ProjectServiceContainer extends Container
1818
{
19+
private $parameters;
20+
1921
/**
2022
* Constructor.
2123
*/
2224
public function __construct()
2325
{
24-
parent::__construct(new ParameterBag($this->getDefaultParameters()));
26+
$this->parameters = array(
27+
'baz_class' => 'BazClass',
28+
'foo_class' => 'FooClass',
29+
'foo' => 'bar',
30+
);
31+
32+
parent::__construct(new ParameterBag($this->parameters));
2533
$this->methodMap = array(
2634
'bar' => 'getBarService',
2735
'baz' => 'getBazService',
@@ -245,18 +253,4 @@ protected function getInlinedService()
245253

246254
return $instance;
247255
}
248-
249-
/**
250-
* Gets the default parameters.
251-
*
252-
* @return array An array of the default parameters
253-
*/
254-
protected function getDefaultParameters()
255-
{
256-
return array(
257-
'baz_class' => 'BazClass',
258-
'foo_class' => 'FooClass',
259-
'foo' => 'bar',
260-
);
261-
}
262256
}

0 commit comments

Comments
 (0)