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

Skip to content

[DI] Mark service_container a private service #22806

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function setContainer(ContainerInterface $container)
$expected = self::getSubscribedServices();

foreach ($container->getServiceIds() as $id) {
if ('service_container' === $id) {
if ('service_container' === $id) { // to be removed in 4.0
continue;
}
if (!isset($expected[$id])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static function assertSaneContainer(Container $container, $message = '')
{
$errors = array();
foreach ($container->getServiceIds() as $id) {
if ('service_container' === $id) { // to be removed in 4.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having to add such check is because there is no way to opt-in in the new behavior of getServiceIds, and then we have to skip the private services to avoid deprecations later. This causes issues (see also #22866 which faces the same issue).

Can we implement a hidden argument allowing to opt-in for the new behavior instead, to get only public ids ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure.. wouldnt this be over complex? <4.0 getServiceIds includes privates, using those with get() etc. is non-crashing, in >=4.0 those id's are exlcuded as they are crashing. I could live with that.

For the SF codebase i think explicitly skippping is just fine =/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is non-crashing, but it triggers deprecation warnings everywhere. And there is no way to avoid this unless you know the ids of all private services (and then, you could as well know the ids of all public services and avoid calling getServiceIds)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the hidden arg makes sense, it could be added to #22866

continue;
}
try {
$container->get($id);
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $id => $definition) {
// synthetic service is public
if ($definition->isSynthetic() && !$definition->isPublic()) {
if ($definition->isSynthetic() && !$definition->isPublic() && 'service_container' !== $id) {
Copy link
Contributor

@sstok sstok May 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be moved before isSynthetic() as this it's faster (micro optimization).

throw new RuntimeException(sprintf('A synthetic service ("%s") must be public.', $id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ protected function processValue($value, $isRoot = false)
*/
private function isInlineableDefinition($id, Definition $definition, ServiceReferenceGraph $graph)
{
if ('service_container' === $id) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same.. probably a bugfix?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service_container was public, so this wouldn't change anything as a bug fix

return false;
}

if (!$definition->isShared()) {
return true;
}
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function set($id, $service)
$id = $this->normalizeId($id);

if ('service_container' === $id) {
throw new InvalidArgumentException('You cannot set service "service_container".');
throw new InvalidArgumentException('You cannot set the private service "service_container".');
}

if (isset($this->aliases[$id])) {
Expand Down Expand Up @@ -229,6 +229,8 @@ public function has($id)
{
for ($i = 2;;) {
if ('service_container' === $id) {
@trigger_error('Checking for the existence of the "service_container" private service is deprecated since Symfony 3.4 and won\'t be supported anymore in Symfony 4.0.', E_USER_DEPRECATED);

return true;
}
if (isset($this->aliases[$id])) {
Expand Down Expand Up @@ -288,6 +290,8 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
// calling $this->normalizeId($id) unless necessary.
for ($i = 2;;) {
if ('service_container' === $id) {
@trigger_error('Requesting the "service_container" private service is deprecated since Symfony 3.4 and won\'t be supported anymore in Symfony 4.0.', E_USER_DEPRECATED);

return $this;
}
if (isset($this->aliases[$id])) {
Expand Down Expand Up @@ -363,6 +367,8 @@ public function initialized($id)
$id = $this->normalizeId($id);

if ('service_container' === $id) {
@trigger_error('Checking for the initialization of the "service_container" private service is deprecated since Symfony 3.4 and won\'t be supported anymore in Symfony 4.0.', E_USER_DEPRECATED);

return false;
}

Expand Down Expand Up @@ -401,7 +407,7 @@ public function getServiceIds()
}
}
}
$ids[] = 'service_container';
$ids[] = 'service_container'; // to be removed in 4.0

return array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->services)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function __construct(ParameterBagInterface $parameterBag = null)
parent::__construct($parameterBag);

$this->trackResources = interface_exists('Symfony\Component\Config\Resource\ResourceInterface');
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true));
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true)->setPublic(false));
$this->setAlias(PsrContainerInterface::class, new Alias('service_container', false));
$this->setAlias(ContainerInterface::class, new Alias('service_container', false));
}
Expand Down Expand Up @@ -1104,7 +1104,7 @@ private function createService(Definition $definition, $id, $tryProxy = true)
$callable[0] = $parameterBag->resolveValue($callable[0]);

if ($callable[0] instanceof Reference) {
$callable[0] = $this->get((string) $callable[0], $callable[0]->getInvalidBehavior());
$callable[0] = 'service_container' === (string) $callable[0] ? $this : $this->get((string) $callable[0], $callable[0]->getInvalidBehavior());
} elseif ($callable[0] instanceof Definition) {
$callable[0] = $this->createService($callable[0], null);
}
Expand Down Expand Up @@ -1187,10 +1187,10 @@ public function resolveServices($value)
}
}
$value = function () use ($id, $method) {
return call_user_func_array(array($this->get($id), $method), func_get_args());
return call_user_func_array(array('service_container' === $id ? $this : $this->get($id), $method), func_get_args());
};
} elseif ($value instanceof Reference) {
$value = $this->get((string) $value, $value->getInvalidBehavior());
$value = 'service_container' === (string) $value ? $this : $this->get((string) $value, $value->getInvalidBehavior());
} elseif ($value instanceof Definition) {
$value = $this->createService($value, null);
} elseif ($value instanceof Expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,15 @@ private function findNodes()
}

foreach ($container->getServiceIds() as $id) {
if ('service_container' === $id) { // to be removed in 4.0
continue;
}
if (array_key_exists($id, $container->getAliases())) {
continue;
}

if (!$container->hasDefinition($id)) {
$class = get_class('service_container' === $id ? $this->container : $container->get($id));
$nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']);
$nodes[$id] = array('class' => str_replace('\\', '\\\\', get_class($container->get($id))), 'attributes' => $this->options['node.instance']);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function testDefaultRegisteredDefinitions()
$definition = $builder->getDefinition('service_container');
$this->assertInstanceOf(Definition::class, $definition);
$this->assertTrue($definition->isSynthetic());
$this->assertFalse($definition->isPublic());
$this->assertSame(ContainerInterface::class, $definition->getClass());
$this->assertTrue($builder->hasAlias(PsrContainerInterface::class));
$this->assertTrue($builder->hasAlias(ContainerInterface::class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@

class ContainerTest extends TestCase
{
/**
* @group legacy
* @expectedDeprecation Requesting the "service_container" private service is deprecated since Symfony 3.4 and won't be supported anymore in Symfony 4.0.
*/
public function testConstructor()
{
$sc = new Container();
$this->assertSame($sc, $sc->get('service_container'), '__construct() automatically registers itself as a service');
}

public function testConstructorWithParameters()
{
$sc = new Container(new ParameterBag(array('foo' => 'bar')));
$this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public function testCrossCheck($fixture, $type)
$services2[$id] = serialize($service);
}

unset($services1['service_container'], $services2['service_container']);

$this->assertEquals($services2, $services1, 'Iterator on the containers returns the same services');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testDumpAnonymousServices()
$this->assertEquals('<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="false" synthetic="true"/>
<service id="foo" class="FooClass">
<argument type="service">
<service class="BarClass">
Expand All @@ -94,7 +94,7 @@ public function testDumpEntities()
$this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<container xmlns=\"http://symfony.com/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">
<services>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"false\" synthetic=\"true\"/>
<service id=\"foo\" class=\"FooClass\Foo\">
<tag name=\"foo&quot;bar\bar\" foo=\"foo&quot;barřž€\"/>
<argument>foo&lt;&gt;&amp;bar</argument>
Expand Down Expand Up @@ -123,7 +123,7 @@ public function provideDecoratedServicesData()
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<container xmlns=\"http://symfony.com/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">
<services>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"false\" synthetic=\"true\"/>
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\" decoration-inner-name=\"bar.woozy\"/>
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
Expand All @@ -133,7 +133,7 @@ public function provideDecoratedServicesData()
array("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<container xmlns=\"http://symfony.com/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">
<services>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" synthetic=\"true\"/>
<service id=\"service_container\" class=\"Symfony\Component\DependencyInjection\ContainerInterface\" public=\"false\" synthetic=\"true\"/>
<service id=\"foo\" class=\"FooClass\Foo\" decorates=\"bar\"/>
<service id=\"Psr\Container\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
<service id=\"Symfony\Component\DependencyInjection\ContainerInterface\" alias=\"service_container\" public=\"false\"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ digraph sc {
node [fontsize="11" fontname="Arial" shape="record"];
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];

node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerInterface\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo [label="foo\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_bar [label="bar\nBarClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo -> node_bar [label="" style="filled"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function __construct()
$this->methodMap = array(
'bar' => 'getBarService',
);
$this->privates = array(
'service_container' => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we actually dump it in this array ? It receives a special treatment when getting it anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be left out indeed, but im not sure if we care :) as it requires adding code; which defeats this PR :)

);

$this->aliases = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct()
'factory_simple' => true,
'inlined' => true,
'new_factory' => true,
'service_container' => true,
);
$this->aliases = array(
'Psr\\Container\\ContainerInterface' => 'service_container',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function __construct()
'new_factory_service' => 'getNewFactoryServiceService',
'service_from_static_method' => 'getServiceFromStaticMethodService',
);
$this->privates = array(
'service_container' => true,
);
$this->aliases = array(
'alias_for_alias' => 'foo',
'alias_for_foo' => 'foo',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service class="Symfony\Component\DependencyInjection\ContainerInterface" id="service_container" synthetic="true"/>
<service class="Symfony\Component\DependencyInjection\ContainerInterface" id="service_container" public="false" synthetic="true"/>
<service alias="service_container" id="Psr\Container\ContainerInterface" public="false"/>
<service alias="service_container" id="Symfony\Component\DependencyInjection\ContainerInterface" public="false"/>
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@
</parameter>
<parameter key="constant" type="constant">PHP_EOL</parameter>
</parameters>
<services>
<service class="Symfony\Component\DependencyInjection\ContainerInterface" id="service_container" public="false" synthetic="true"/>
<service alias="service_container" id="Psr\Container\ContainerInterface" public="false"/>
<service alias="service_container" id="Symfony\Component\DependencyInjection\ContainerInterface" public="false"/>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="false" synthetic="true"/>
<service id="foo" class="Foo">
<factory method="createFoo">
<service class="FooFactory">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="false" synthetic="true"/>
<service id="foo" class="Foo" autowire="true"/>
<service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service class="Symfony\Component\DependencyInjection\ContainerInterface" id="service_container" public="false" synthetic="true"/>
<service alias="service_container" id="Psr\Container\ContainerInterface" public="false"/>
<service alias="service_container" id="Symfony\Component\DependencyInjection\ContainerInterface" public="false"/>
<service id="foo" class="FooClass" />
<service id="baz" class="BazClass" />
<service id="not_shared" class="FooClass" shared="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</parameter>
</parameters>
<services>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="false" synthetic="true"/>
<service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parameter key="foo">bar</parameter>
</parameters>
<services>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" synthetic="true"/>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="false" synthetic="true"/>
<service id="foo" class="Bar\FooClass">
<tag name="foo" foo="foo"/>
<tag name="foo" bar="bar" baz="baz"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: false
synthetic: true
Psr\Container\ContainerInterface:
alias: service_container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: false
synthetic: true
foo:
class: Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: false
synthetic: true
Psr\Container\ContainerInterface:
alias: service_container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ parameters:
services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: false
synthetic: true
foo:
class: Bar\FooClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,6 @@ public function testDefaults()
$this->assertFalse($container->getDefinition('child_def')->isAutowired());

$definitions = $container->getDefinitions();
$this->assertSame('service_container', key($definitions));

array_shift($definitions);
$this->assertStringStartsWith('1_', key($definitions));

$anonymous = current($definitions);
Expand Down