Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cac0a0

Browse files
committed
feature #23561 [DI] Optimize use of private and pre-defined services (nicolas-grekas)
This PR was merged into the 4.0-dev branch. Discussion ---------- [DI] Optimize use of private and pre-defined services | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes (perf) | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - By making private services really private and taking into account that (un)setting pre-defined services is not allowed anymore, we can go one step further into optimizing the dumped container. Commits ------- c0c1881 [DI] Optimize use of private and pre-defined services
2 parents a19c120 + c0c1881 commit 1cac0a0

18 files changed

+77
-146
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ class Container implements ResettableContainerInterface
5050
protected $aliases = array();
5151
protected $loading = array();
5252

53-
/**
54-
* @internal
55-
*/
56-
protected $privates = array();
57-
5853
private $envCache = array();
5954
private $compiled = false;
6055

@@ -155,10 +150,6 @@ public function set($id, $service)
155150
throw new InvalidArgumentException('You cannot set service "service_container".');
156151
}
157152

158-
if (isset($this->privates[$id])) {
159-
throw new InvalidArgumentException(sprintf('You cannot set the private service "%s".', $id));
160-
}
161-
162153
if (isset($this->methodMap[$id])) {
163154
throw new InvalidArgumentException(sprintf('You cannot set the pre-defined service "%s".', $id));
164155
}
@@ -185,9 +176,6 @@ public function set($id, $service)
185176
*/
186177
public function has($id)
187178
{
188-
if (isset($this->privates[$id])) {
189-
return false;
190-
}
191179
if (isset($this->aliases[$id])) {
192180
$id = $this->aliases[$id];
193181
}
@@ -224,13 +212,6 @@ public function has($id)
224212
*/
225213
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
226214
{
227-
if (isset($this->privates[$id])) {
228-
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
229-
throw new ServiceNotFoundException($id);
230-
}
231-
232-
return;
233-
}
234215
if (isset($this->aliases[$id])) {
235216
$id = $this->aliases[$id];
236217
}
@@ -293,10 +274,6 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
293274
*/
294275
public function initialized($id)
295276
{
296-
if (isset($this->privates[$id])) {
297-
return false;
298-
}
299-
300277
if (isset($this->aliases[$id])) {
301278
$id = $this->aliases[$id];
302279
}

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

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ private function addServiceInstance($id, Definition $definition, $isSimpleInstan
401401
$instantiation = '';
402402

403403
if (!$isProxyCandidate && $definition->isShared()) {
404-
$instantiation = "\$this->services['$id'] = ".($isSimpleInstance ? '' : '$instance');
404+
$instantiation = sprintf('$this->%s[\'%s\'] = %s', $this->container->getDefinition($id)->isPublic() ? 'services' : 'privates', $id, $isSimpleInstance ? '' : '$instance');
405405
} elseif (!$isSimpleInstance) {
406406
$instantiation = '$instance';
407407
}
@@ -646,7 +646,7 @@ private function addService($id, Definition $definition)
646646

647647
// with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
648648
$isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
649-
$visibility = $isProxyCandidate ? 'public' : 'protected';
649+
$visibility = $isProxyCandidate ? 'public' : ($definition->isPublic() ? 'protected' : 'private');
650650
$methodName = $this->generateMethodName($id);
651651
$code = <<<EOF
652652
@@ -792,6 +792,7 @@ class $class extends $baseClass
792792
{
793793
private \$parameters;
794794
private \$targetDirs = array();
795+
private \$privates = array();
795796
796797
EOF;
797798
}
@@ -818,9 +819,8 @@ public function __construct()
818819
$code .= "\n \$this->parameters = \$this->getDefaultParameters();\n";
819820
}
820821

821-
$code .= "\n \$this->services = array();\n";
822+
$code .= "\n \$this->services = \$this->privates = array();\n";
822823
$code .= $this->addMethodMap();
823-
$code .= $this->addPrivateServices();
824824
$code .= $this->addAliases();
825825

826826
$code .= <<<'EOF'
@@ -886,40 +886,12 @@ private function addMethodMap()
886886
$code = " \$this->methodMap = array(\n";
887887
ksort($definitions);
888888
foreach ($definitions as $id => $definition) {
889-
$code .= ' '.$this->export($id).' => '.$this->export($this->generateMethodName($id)).",\n";
890-
}
891-
892-
return $code." );\n";
893-
}
894-
895-
/**
896-
* Adds the privates property definition.
897-
*
898-
* @return string
899-
*/
900-
private function addPrivateServices()
901-
{
902-
if (!$definitions = $this->container->getDefinitions()) {
903-
return '';
904-
}
905-
906-
$code = '';
907-
ksort($definitions);
908-
foreach ($definitions as $id => $definition) {
909-
if (!$definition->isPublic()) {
910-
$code .= ' '.$this->export($id)." => true,\n";
889+
if ($definition->isPublic()) {
890+
$code .= ' '.$this->export($id).' => '.$this->export($this->generateMethodName($id)).",\n";
911891
}
912892
}
913893

914-
if (empty($code)) {
915-
return '';
916-
}
917-
918-
$out = " \$this->privates = array(\n";
919-
$out .= $code;
920-
$out .= " );\n";
921-
922-
return $out;
894+
return $code." );\n";
923895
}
924896

925897
/**
@@ -1535,18 +1507,18 @@ private function getServiceCall($id, Reference $reference = null)
15351507
return '$this';
15361508
}
15371509

1538-
if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
1510+
if ($this->container->hasDefinition($id)) {
15391511
$code = sprintf('$this->%s()', $this->generateMethodName($id));
1512+
1513+
if ($this->container->getDefinition($id)->isShared()) {
1514+
$code = sprintf('($this->%s[\'%s\'] ?? %s)', $this->container->getDefinition($id)->isPublic() ? 'services' : 'privates', $id, $code);
1515+
}
15401516
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
15411517
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
15421518
} else {
15431519
$code = sprintf('$this->get(\'%s\')', $id);
15441520
}
15451521

1546-
if ($this->container->hasDefinition($id) && $this->container->getDefinition($id)->isShared()) {
1547-
$code = "(\$this->services['$id'] ?? $code)";
1548-
}
1549-
15501522
return $code;
15511523
}
15521524

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function testGetServiceIds()
134134

135135
$sc = new ProjectServiceContainer();
136136
$sc->set('foo', $obj = new \stdClass());
137-
$this->assertEquals(array('service_container', 'internal', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()');
137+
$this->assertEquals(array('service_container', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'internal_dependency', 'foo'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by factory methods in the method map, followed by service ids defined by set()');
138138
}
139139

140140
public function testSet()
@@ -340,26 +340,6 @@ public function testThatCloningIsNotSupported()
340340
$this->assertTrue($clone->isPrivate());
341341
}
342342

343-
/**
344-
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
345-
* @expectedExceptionMessage You cannot set the private service "internal".
346-
*/
347-
public function testUnsetInternalPrivateService()
348-
{
349-
$c = new ProjectServiceContainer();
350-
$c->set('internal', null);
351-
}
352-
353-
/**
354-
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
355-
* @expectedExceptionMessage You cannot set the private service "internal".
356-
*/
357-
public function testChangeInternalPrivateService()
358-
{
359-
$c = new ProjectServiceContainer();
360-
$c->set('internal', new \stdClass());
361-
}
362-
363343
public function testCheckExistenceOfAnInternalPrivateService()
364344
{
365345
$c = new ProjectServiceContainer();
@@ -396,7 +376,6 @@ class ProjectServiceContainer extends Container
396376
public $__foo_baz;
397377
public $__internal;
398378
protected $methodMap = array(
399-
'internal' => 'getInternalService',
400379
'bar' => 'getBarService',
401380
'foo_bar' => 'getFooBarService',
402381
'foo.baz' => 'getFoo_BazService',
@@ -414,13 +393,13 @@ public function __construct()
414393
$this->__foo_bar = new \stdClass();
415394
$this->__foo_baz = new \stdClass();
416395
$this->__internal = new \stdClass();
417-
$this->privates = array('internal' => true);
396+
$this->privates = array();
418397
$this->aliases = array('alias' => 'bar');
419398
}
420399

421400
protected function getInternalService()
422401
{
423-
return $this->services['internal'] = $this->__internal;
402+
return $this->privates['internal'] = $this->__internal;
424403
}
425404

426405
protected function getBarService()
@@ -459,7 +438,7 @@ protected function getInternalDependencyService()
459438
{
460439
$this->services['internal_dependency'] = $instance = new \stdClass();
461440

462-
$instance->internal = isset($this->services['internal']) ? $this->services['internal'] : $this->getInternalService();
441+
$instance->internal = $this->privates['internal'] ?? $this->getInternalService();
463442

464443
return $instance;
465444
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ class Container extends AbstractContainer
2222
{
2323
private $parameters;
2424
private $targetDirs = array();
25+
private $privates = array();
2526

2627
/**
2728
* Constructor.
2829
*/
2930
public function __construct()
3031
{
31-
$this->services = array();
32+
$this->services = $this->privates = array();
3233

3334
$this->aliases = array();
3435
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
2627
*/
2728
public function __construct()
2829
{
29-
$this->services = array();
30+
$this->services = $this->privates = array();
3031

3132
$this->aliases = array();
3233
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
@@ -28,7 +29,7 @@ public function __construct()
2829
{
2930
$this->parameters = $this->getDefaultParameters();
3031

31-
$this->services = array();
32+
$this->services = $this->privates = array();
3233
$this->methodMap = array(
3334
'test' => 'getTestService',
3435
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
@@ -32,7 +33,7 @@ public function __construct()
3233
}
3334
$this->parameters = $this->getDefaultParameters();
3435

35-
$this->services = array();
36+
$this->services = $this->privates = array();
3637
$this->methodMap = array(
3738
'test' => 'getTestService',
3839
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
2627
*/
2728
public function __construct()
2829
{
29-
$this->services = array();
30+
$this->services = $this->privates = array();
3031
$this->methodMap = array(
3132
'bar' => 'getBarService',
3233
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
2627
*/
2728
public function __construct()
2829
{
29-
$this->services = array();
30+
$this->services = $this->privates = array();
3031
$this->methodMap = array(
3132
'service_from_anonymous_factory' => 'getServiceFromAnonymousFactoryService',
3233
'service_with_method_call_and_factory' => 'getServiceWithMethodCallAndFactoryService',

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
2627
*/
2728
public function __construct()
2829
{
29-
$this->services = array();
30+
$this->services = $this->privates = array();
3031
$this->methodMap = array(
3132
'foo' => 'getFooService',
3233
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
@@ -28,7 +29,7 @@ public function __construct()
2829
{
2930
$this->parameters = $this->getDefaultParameters();
3031

31-
$this->services = array();
32+
$this->services = $this->privates = array();
3233
$this->methodMap = array(
3334
'test' => 'getTestService',
3435
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
2627
*/
2728
public function __construct()
2829
{
29-
$this->services = array();
30+
$this->services = $this->privates = array();
3031
$this->methodMap = array(
3132
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Container33\\Foo' => 'getSymfony_Component_DependencyInjection_Tests_Fixtures_Container33_FooService',
3233
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container
2020
{
2121
private $parameters;
2222
private $targetDirs = array();
23+
private $privates = array();
2324

2425
/**
2526
* Constructor.
@@ -28,7 +29,7 @@ public function __construct()
2829
{
2930
$this->parameters = $this->getDefaultParameters();
3031

31-
$this->services = array();
32+
$this->services = $this->privates = array();
3233

3334
$this->aliases = array();
3435
}

0 commit comments

Comments
 (0)