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

This PR was merged into the 3.3-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | no
| Fixed tickets | #19192
| License       | MIT
| Doc PR        | -

This PR is the subset of #19668 that fixes #19192: it deprecates (un)setting pre-defined services.
This opens the path to some optimizations in the dumped container in 4.0.

Commits
-------

fdb2140b81 [DI] Deprecate (un)setting pre-defined services
This commit is contained in:
Fabien Potencier 2017-02-12 12:52:54 +01:00
commit 915cca84b6
5 changed files with 51 additions and 11 deletions

View File

@ -38,8 +38,7 @@ class PhpEngineTest extends TestCase
$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());
}

View File

@ -190,7 +190,13 @@ class Container implements ResettableContainerInterface
@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);
}
} 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);
}
}
}

View File

@ -163,6 +163,22 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$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();
@ -172,9 +188,6 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$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');
@ -337,7 +350,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$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');
}
@ -426,7 +439,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
/**
* @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()
{
@ -453,6 +466,19 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$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
@ -490,7 +516,7 @@ class ProjectServiceContainer extends Container
protected function getBarService()
{
return $this->__bar;
return $this->services['bar'] = $this->__bar;
}
protected function getFooBarService()

View File

@ -241,13 +241,13 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
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'));
}
@ -264,6 +264,10 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$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';
@ -276,6 +280,10 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$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';

View File

@ -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;