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

Skip to content

[DI] Optimize use of private and pre-defined services #23561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ class Container implements ResettableContainerInterface
protected $aliases = array();
protected $loading = array();

/**
* @internal
*/
protected $privates = array();

private $envCache = array();
private $compiled = false;

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

if (isset($this->privates[$id])) {
throw new InvalidArgumentException(sprintf('You cannot set the private service "%s".', $id));
}

if (isset($this->methodMap[$id])) {
throw new InvalidArgumentException(sprintf('You cannot set the pre-defined service "%s".', $id));
}
Expand All @@ -185,9 +176,6 @@ public function set($id, $service)
*/
public function has($id)
{
if (isset($this->privates[$id])) {
return false;
}
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
Expand Down Expand Up @@ -224,13 +212,6 @@ public function has($id)
*/
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
{
if (isset($this->privates[$id])) {
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
throw new ServiceNotFoundException($id);
}

return;
}
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
Expand Down Expand Up @@ -293,10 +274,6 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
*/
public function initialized($id)
{
if (isset($this->privates[$id])) {
return false;
}

if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
Expand Down
52 changes: 12 additions & 40 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ private function addServiceInstance($id, Definition $definition, $isSimpleInstan
$instantiation = '';

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

// with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
$isProxyCandidate = $this->getProxyDumper()->isProxyCandidate($definition);
$visibility = $isProxyCandidate ? 'public' : 'protected';
$visibility = $isProxyCandidate ? 'public' : ($definition->isPublic() ? 'protected' : 'private');
$methodName = $this->generateMethodName($id);
$code = <<<EOF

Expand Down Expand Up @@ -792,6 +792,7 @@ class $class extends $baseClass
{
private \$parameters;
private \$targetDirs = array();
private \$privates = array();

EOF;
}
Expand All @@ -818,9 +819,8 @@ public function __construct()
$code .= "\n \$this->parameters = \$this->getDefaultParameters();\n";
}

$code .= "\n \$this->services = array();\n";
$code .= "\n \$this->services = \$this->privates = array();\n";
$code .= $this->addMethodMap();
$code .= $this->addPrivateServices();
$code .= $this->addAliases();

$code .= <<<'EOF'
Expand Down Expand Up @@ -886,40 +886,12 @@ private function addMethodMap()
$code = " \$this->methodMap = array(\n";
ksort($definitions);
foreach ($definitions as $id => $definition) {
$code .= ' '.$this->export($id).' => '.$this->export($this->generateMethodName($id)).",\n";
}

return $code." );\n";
}

/**
* Adds the privates property definition.
*
* @return string
*/
private function addPrivateServices()
{
if (!$definitions = $this->container->getDefinitions()) {
return '';
}

$code = '';
ksort($definitions);
foreach ($definitions as $id => $definition) {
if (!$definition->isPublic()) {
$code .= ' '.$this->export($id)." => true,\n";
if ($definition->isPublic()) {
$code .= ' '.$this->export($id).' => '.$this->export($this->generateMethodName($id)).",\n";
}
}

if (empty($code)) {
return '';
}

$out = " \$this->privates = array(\n";
$out .= $code;
$out .= " );\n";

return $out;
return $code." );\n";
}

/**
Expand Down Expand Up @@ -1535,8 +1507,12 @@ private function getServiceCall($id, Reference $reference = null)
return '$this';
}

if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
if ($this->container->hasDefinition($id)) {
$code = sprintf('$this->%s()', $this->generateMethodName($id));

if ($this->container->getDefinition($id)->isShared()) {
$code = sprintf('($this->%s[\'%s\'] ?? %s)', $this->container->getDefinition($id)->isPublic() ? 'services' : 'privates', $id, $code);
}
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
} else {
Expand All @@ -1547,10 +1523,6 @@ private function getServiceCall($id, Reference $reference = null)
$code = sprintf('$this->get(\'%s\')', $id);
}

if ($this->container->hasDefinition($id) && $this->container->getDefinition($id)->isShared()) {
$code = "(\$this->services['$id'] ?? $code)";
}

return $code;
}

Expand Down
29 changes: 4 additions & 25 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function testGetServiceIds()

$sc = new ProjectServiceContainer();
$sc->set('foo', $obj = new \stdClass());
$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()');
$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()');
}

public function testSet()
Expand Down Expand Up @@ -340,26 +340,6 @@ public function testThatCloningIsNotSupported()
$this->assertTrue($clone->isPrivate());
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage You cannot set the private service "internal".
*/
public function testUnsetInternalPrivateService()
{
$c = new ProjectServiceContainer();
$c->set('internal', null);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage You cannot set the private service "internal".
*/
public function testChangeInternalPrivateService()
{
$c = new ProjectServiceContainer();
$c->set('internal', new \stdClass());
}

public function testCheckExistenceOfAnInternalPrivateService()
{
$c = new ProjectServiceContainer();
Expand Down Expand Up @@ -396,7 +376,6 @@ class ProjectServiceContainer extends Container
public $__foo_baz;
public $__internal;
protected $methodMap = array(
'internal' => 'getInternalService',
'bar' => 'getBarService',
'foo_bar' => 'getFooBarService',
'foo.baz' => 'getFoo_BazService',
Expand All @@ -414,13 +393,13 @@ public function __construct()
$this->__foo_bar = new \stdClass();
$this->__foo_baz = new \stdClass();
$this->__internal = new \stdClass();
$this->privates = array('internal' => true);
$this->privates = array();
$this->aliases = array('alias' => 'bar');
}

protected function getInternalService()
{
return $this->services['internal'] = $this->__internal;
return $this->privates['internal'] = $this->__internal;
}

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

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

return $instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ class Container extends AbstractContainer
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
*/
public function __construct()
{
$this->services = array();
$this->services = $this->privates = array();

$this->aliases = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
*/
public function __construct()
{
$this->services = array();
$this->services = $this->privates = array();

$this->aliases = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
Expand All @@ -28,7 +29,7 @@ public function __construct()
{
$this->parameters = $this->getDefaultParameters();

$this->services = array();
$this->services = $this->privates = array();
$this->methodMap = array(
'test' => 'getTestService',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
Expand All @@ -32,7 +33,7 @@ public function __construct()
}
$this->parameters = $this->getDefaultParameters();

$this->services = array();
$this->services = $this->privates = array();
$this->methodMap = array(
'test' => 'getTestService',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
*/
public function __construct()
{
$this->services = array();
$this->services = $this->privates = array();
$this->methodMap = array(
'bar' => 'getBarService',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
*/
public function __construct()
{
$this->services = array();
$this->services = $this->privates = array();
$this->methodMap = array(
'service_from_anonymous_factory' => 'getServiceFromAnonymousFactoryService',
'service_with_method_call_and_factory' => 'getServiceWithMethodCallAndFactoryService',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
*/
public function __construct()
{
$this->services = array();
$this->services = $this->privates = array();
$this->methodMap = array(
'foo' => 'getFooService',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
Expand All @@ -28,7 +29,7 @@ public function __construct()
{
$this->parameters = $this->getDefaultParameters();

$this->services = array();
$this->services = $this->privates = array();
$this->methodMap = array(
'test' => 'getTestService',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
*/
public function __construct()
{
$this->services = array();
$this->services = $this->privates = array();
$this->methodMap = array(
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Container33\\Foo' => 'getSymfony_Component_DependencyInjection_Tests_Fixtures_Container33_FooService',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ProjectServiceContainer extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

/**
* Constructor.
Expand All @@ -28,7 +29,7 @@ public function __construct()
{
$this->parameters = $this->getDefaultParameters();

$this->services = array();
$this->services = $this->privates = array();

$this->aliases = array();
}
Expand Down
Loading