Skip to content

[DI] Deprecate (un)setting pre-defined services #21533

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
Feb 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public function testEvaluateWithoutAvailableRequest()
$loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
$engine = new PhpEngine(new TemplateNameParser(), $container, $loader, new GlobalVariables($container));

$container->set('request_stack', null);

$this->assertFalse($container->has('request_stack'));
$globals = $engine->getGlobals();
$this->assertEmpty($globals['app']->getRequest());
}
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ public function set($id, $service)
@trigger_error(sprintf('Unsetting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
unset($this->privates[$id]);
} else {
@trigger_error(sprintf('Setting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0. A new public service will be created instead.', $id), E_USER_DEPRECATED);
@trigger_error(sprintf('Setting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
Copy link
Member Author

Choose a reason for hiding this comment

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

Removing this tail:

A new public service will be created instead.

it's not obvious to me that this will be the behavior we should implement in 4.0 :)

}
} elseif (isset($this->methodMap[$id])) {
if (null === $service) {
@trigger_error(sprintf('Unsetting the "%s" pre-defined service is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
} else {
@trigger_error(sprintf('Setting the "%s" pre-defined service is deprecated since Symfony 3.3 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
}
}
}
Expand Down
38 changes: 32 additions & 6 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ public function testSetReplacesAlias()
$this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
}

/**
* @group legacy
* @expectedDeprecation Unsetting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
*/
public function testSetWithNullResetPredefinedService()
{
$sc = new Container();
$sc->set('foo', new \stdClass());
$sc->set('foo', null);
$this->assertFalse($sc->has('foo'), '->set() with null service resets the service');

$sc = new ProjectServiceContainer();
$sc->set('bar', null);
$this->assertTrue($sc->has('bar'), '->set() with null service resets the pre-defined service');
}

public function testGet()
{
$sc = new ProjectServiceContainer();
Expand All @@ -172,9 +188,6 @@ public function testGet()
$this->assertSame($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
$this->assertSame($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');

$sc->set('bar', $bar = new \stdClass());
$this->assertSame($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()');

try {
$sc->get('');
$this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
Expand Down Expand Up @@ -337,7 +350,7 @@ public function testInitialized()
$this->assertFalse($sc->initialized('bar'), '->initialized() returns false if a service is defined, but not currently loaded');
$this->assertFalse($sc->initialized('alias'), '->initialized() returns false if an aliased service is not initialized');

$sc->set('bar', new \stdClass());
$sc->get('bar');
$this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
}

Expand Down Expand Up @@ -426,7 +439,7 @@ public function testUnsetInternalPrivateServiceIsDeprecated()

/**
* @group legacy
* @expectedDeprecation Setting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0. A new public service will be created instead.
* @expectedDeprecation Setting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
*/
public function testChangeInternalPrivateServiceIsDeprecated()
{
Expand All @@ -453,6 +466,19 @@ public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
$c = new ProjectServiceContainer();
$c->get('internal');
}

/**
* @group legacy
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
*/
public function testReplacingAPreDefinedServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->set('bar', new \stdClass());
$c->set('bar', $bar = new \stdClass());

$this->assertSame($bar, $c->get('bar'), '->set() replaces a pre-defined service');
}
}

class ProjectServiceContainer extends Container
Expand Down Expand Up @@ -490,7 +516,7 @@ protected function getInternalService()

protected function getBarService()
{
return $this->__bar;
return $this->services['bar'] = $this->__bar;
}

protected function getFooBarService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ public function provideInvalidFactories()
public function testAliases()
{
$container = include self::$fixturesPath.'/containers/container9.php';
$container->setParameter('foo_bar', 'foo_bar');
$container->compile();
$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases')));

$container = new \Symfony_DI_PhpDumper_Test_Aliases();
$container->set('foo', $foo = new \stdClass());
$this->assertSame($foo, $container->get('foo'));
$foo = $container->get('foo');
$this->assertSame($foo, $container->get('alias_for_foo'));
$this->assertSame($foo, $container->get('alias_for_alias'));
}
Expand All @@ -264,6 +264,10 @@ public function testFrozenContainerWithoutAliases()
$this->assertFalse($container->has('foo'));
}

/**
* @group legacy
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
*/
public function testOverrideServiceWhenUsingADumpedContainer()
{
require_once self::$fixturesPath.'/php/services9.php';
Expand All @@ -276,6 +280,10 @@ public function testOverrideServiceWhenUsingADumpedContainer()
$this->assertSame($bar, $container->get('bar'), '->set() overrides an already defined service');
}

/**
* @group legacy
* @expectedDeprecation Setting the "bar" pre-defined service is deprecated since Symfony 3.3 and won't be supported anymore in Symfony 4.0.
*/
public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
{
require_once self::$fixturesPath.'/php/services9.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once __DIR__.'/../includes/classes.php';
require_once __DIR__.'/../includes/foo.php';

use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
Expand Down