Merge branch '3.4'

* 3.4:
  [DI] Check privates before aliases consistently
  [Yaml] Add missing deprecation annotation
  [DI] Check for privates before shared services
This commit is contained in:
Nicolas Grekas 2017-05-24 16:45:46 +02:00
commit 6da68536f0
5 changed files with 26 additions and 14 deletions

View File

@ -64,6 +64,15 @@ DependencyInjection
* Calling `get()` on a `ContainerBuilder` instance before compiling the
container is deprecated and will throw an exception in Symfony 4.0.
* Setting or unsetting a private service with the `Container::set()` method is
deprecated. Only public services can be set or unset in Symfony 4.0.
* Checking the existence of a private service with the `Container::has()`
method is deprecated and will return `false` in Symfony 4.0.
* Requesting a private service with the `Container::get()` method is deprecated
and will no longer be supported in Symfony 4.0.
ExpressionLanguage
-------------------

View File

@ -27,10 +27,13 @@ class WebProfilerExtensionTest extends TestCase
*/
private $container;
public static function assertSaneContainer(Container $container, $message = '')
public static function assertSaneContainer(Container $container, $message = '', $knownPrivates = array())
{
$errors = array();
foreach ($container->getServiceIds() as $id) {
if (in_array($id, $knownPrivates, true)) { // to be removed in 4.0
continue;
}
try {
$container->get($id);
} catch (\Exception $e) {
@ -98,7 +101,7 @@ class WebProfilerExtensionTest extends TestCase
$this->assertSame($listenerInjected, $this->container->has('web_profiler.debug_toolbar'));
$this->assertSaneContainer($this->getDumpedContainer());
$this->assertSaneContainer($this->getDumpedContainer(), '', array('web_profiler.csp.handler'));
if ($listenerInjected) {
$this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());

View File

@ -214,6 +214,9 @@ class Container implements ResettableContainerInterface
public function has($id)
{
for ($i = 2;;) {
if (isset($this->privates[$id])) {
@trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
}
if ('service_container' === $id) {
return true;
}
@ -224,10 +227,6 @@ class Container implements ResettableContainerInterface
return true;
}
if (isset($this->privates[$id])) {
@trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
}
if (isset($this->methodMap[$id])) {
return true;
}
@ -265,12 +264,16 @@ class Container implements ResettableContainerInterface
// this method can be called thousands of times during a request, avoid
// calling $this->normalizeId($id) unless necessary.
for ($i = 2;;) {
if (isset($this->privates[$id])) {
@trigger_error(sprintf('Requesting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
}
if ('service_container' === $id) {
return $this;
}
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
// Re-use shared service instance if it exists.
if (isset($this->services[$id])) {
return $this->services[$id];
@ -304,9 +307,6 @@ class Container implements ResettableContainerInterface
return;
}
if (isset($this->privates[$id])) {
@trigger_error(sprintf('Requesting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
}
$this->loading[$id] = true;

View File

@ -735,9 +735,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
$compiler->compile($this);
foreach ($this->definitions as $id => $definition) {
if (!$definition->isPublic()) {
$this->privates[$id] = true;
}
if ($this->trackResources && $definition->isLazy()) {
$this->getReflectionClass($definition->getClass());
}

View File

@ -387,7 +387,8 @@ class ContainerTest extends TestCase
public function testChangeInternalPrivateServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->set('internal', new \stdClass());
$c->set('internal', $internal = new \stdClass());
$this->assertSame($c->get('internal'), $internal);
}
/**
@ -397,7 +398,8 @@ class ContainerTest extends TestCase
public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->has('internal');
$c->get('internal_dependency');
$this->assertTrue($c->has('internal'));
}
/**
@ -407,6 +409,7 @@ class ContainerTest extends TestCase
public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->get('internal_dependency');
$c->get('internal');
}