8000 minor #28753 [DI] minor optim for dumped container (nicolas-grekas) · symfony/symfony@6a4c8b9 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 6a4c8b9

Browse files
committed
minor #28753 [DI] minor optim for dumped container (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [DI] minor optim for dumped container | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Spotted while reviewing a real-world dumped container. Commits ------- c1863e3 [DI] minor optim for dumped container
2 parents eb59771 + c1863e3 commit 6a4c8b9

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class PhpDumper extends Dumper
7777
private $circularReferences = array();
7878
private $singleUsePrivateIds = array();
7979
private $addThrow = false;
80+
private $addGetService = false;
8081
private $locatedIds = array();
8182
private $serviceLocatorTag;
8283

@@ -136,7 +137,7 @@ public function dump(array $options = array())
136137
'build_time' => time(),
137138
), $options);
138139

139-
$this->addThrow = false;
140+
$this->addThrow = $this->addGetService = false;
140141
$this->namespace = $options['namespace'];
141142
$this->asFiles = $options['as_files'];
142143
$this->hotPathTag = $options['hot_path_tag'];
@@ -213,6 +214,15 @@ public function dump(array $options = array())
213214
$this->addDefaultParametersMethod()
214215
;
215216

217+
if ($this->addGetService) {
218+
$code = preg_replace(
219+
"/(\n\n public function __construct.+?\\{\n)/s",
220+
"\n private \$getService;$1 \$this->getService = \\Closure::fromCallable(array(\$this, 'getService'));\n",
221+
$code,
222+
1
223+
);
224+
}
225+
216226
if ($this->asFiles) {
217227
$fileStart = <<<EOF
218228
<?php
@@ -1463,8 +1473,9 @@ private function dumpValue($value, bool $interpolate = true): string
14631473
);
14641474
$this->locatedIds[$id] = true;
14651475
}
1476+
$this->addGetService = true;
14661477

1467-
return sprintf('new \%s(\Closure::fromCallable(array($this, \'getService\')), array(%s%s))', ServiceLocator::class, $serviceMap, $serviceMap ? "\n " : '');
1478+
return sprintf('new \%s($this->getService, array(%s%s))', ServiceLocator::class, $serviceMap, $serviceMap ? "\n " : '');
14681479
}
14691480
} finally {
14701481
list($this->definitionVariables, $this->referenceVariables) = $scope;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_rot13_env.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ class Symfony_DI_PhpDumper_Test_Rot13Parameters extends Container
1818
{
1919
private $parameters;
2020
private $targetDirs = array();
21+
private $getService;
2122

2223
public function __construct()
2324
{
25+
$this->getService = \Closure::fromCallable(array($this, 'getService'));
2426
$this->parameters = $this->getDefaultParameters();
2527

2628
$this->services = $this->privates = array();
@@ -68,7 +70,7 @@ protected function getRot13EnvVarProcessorService()
6870
*/
6971
protected function getContainer_EnvVarProcessorsLocatorService()
7072
{
71-
return $this->services['container.env_var_processors_locator'] = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator(\Closure::fromCallable(array($this, 'getService')), array(
73+
return $this->services['container.env_var_processors_locator'] = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($this->getService, array(
7274
'rot13' => array('services', 'Symfony\\Component\\DependencyInjection\\Tests\\Dumper\\Rot13EnvVarProcessor', 'getRot13EnvVarProcessorService', false),
7375
));
7476
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_service_locator_argument.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ class Symfony_DI_PhpDumper_Service_Locator_Argument extends Container
1818
{
1919
private $parameters;
2020
private $targetDirs = array();
21+
private $getService;
2122

2223
public function __construct()
2324
{
25+
$this->getService = \Closure::fromCallable(array($this, 'getService'));
2426
$this->services = $this->privates = array();
2527
$this->syntheticIds = array(
2628
'foo5' => true,
@@ -64,7 +66,7 @@ protected function getBarService()
6466
{
6567
$this->services['bar'] = $instance = new \stdClass();
6668

67-
$instance->locator = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator(\Closure::fromCallable(array($this, 'getService')), array(
69+
$instance->locator = new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($this->getService, array(
6870
'foo1' => array('services', 'foo1', 'getFoo1Service', false),
6971
'foo2' => array('privates', 'foo2', 'getFoo2Service', false),
7072
'foo3' => array(false, 'foo3', 'getFoo3Service', false),

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ class ProjectServiceContainer extends Container
1818
{
1919
private $parameters;
2020
private $targetDirs = array();
21+
private $getService;
2122

2223
public function __construct()
2324
{
25+
$this->getService = \Closure::fromCallable(array($this, 'getService'));
2426
$this->services = $this->privates = array();
2527
$this->methodMap = array(
2628
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'getTestServiceSubscriberService',
@@ -68,7 +70,7 @@ protected function getTestServiceSubscriberService()
6870
*/
6971
protected function getFooServiceService()
7072
{
71-
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber((new \Symfony\Component\DependencyInjection\Argument\ServiceLocator(\Closure::fromCallable(array($this, 'getService')), array(
73+
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber((new \Symfony\Component\DependencyInjection\Argument\ServiceLocator($this->getService, array(
7274
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => array('privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false),
7375
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => array('services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false),
7476
'bar' => array('services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false),

0 commit comments

Comments
 (0)
0