Merge branch '3.2' into 3.3

* 3.2:
  [Yaml] Add missing deprecation annotation
  [DI] Check for privates before shared services
This commit is contained in:
Nicolas Grekas 2017-05-24 16:31:45 +02:00
commit d7981e5838
6 changed files with 40 additions and 16 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

@ -231,6 +231,9 @@ class Container implements ResettableContainerInterface
if ('service_container' === $id) {
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->aliases[$id])) {
$id = $this->aliases[$id];
}
@ -238,10 +241,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;
}
@ -293,6 +292,10 @@ class Container implements ResettableContainerInterface
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
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);
}
// Re-use shared service instance if it exists.
if (isset($this->services[$id])) {
return $this->services[$id];
@ -331,9 +334,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

@ -736,9 +736,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

@ -147,7 +147,7 @@ class ContainerTest extends TestCase
$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', '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', '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()');
}
/**
@ -453,7 +453,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);
}
/**
@ -463,7 +464,8 @@ class ContainerTest extends TestCase
public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->has('internal');
$c->get('internal_dependency');
$this->assertTrue($c->has('internal'));
}
/**
@ -473,6 +475,7 @@ class ContainerTest extends TestCase
public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->get('internal_dependency');
$c->get('internal');
}
@ -504,6 +507,7 @@ class ProjectServiceContainer extends Container
'circular' => 'getCircularService',
'throw_exception' => 'getThrowExceptionService',
'throws_exception_on_service_configuration' => 'getThrowsExceptionOnServiceConfigurationService',
'internal_dependency' => 'getInternalDependencyService',
);
public function __construct()
@ -520,7 +524,7 @@ class ProjectServiceContainer extends Container
protected function getInternalService()
{
return $this->__internal;
return $this->services['internal'] = $this->__internal;
}
protected function getBarService()
@ -554,6 +558,15 @@ class ProjectServiceContainer extends Container
throw new \Exception('Something was terribly wrong while trying to configure the service!');
}
protected function getInternalDependencyService()
{
$this->services['internal_dependency'] = $instance = new \stdClass();
$instance->internal = isset($this->services['internal']) ? $this->services['internal'] : $this->getInternalService();
return $instance;
}
}
class LegacyProjectServiceContainer extends Container

View File

@ -41,6 +41,8 @@ class Dumper
* Sets the indentation.
*
* @param int $num The amount of spaces to use for indentation of nested nodes
*
* @deprecated since version 3.1, to be removed in 4.0. Pass the indentation to the constructor instead.
*/
public function setIndentation($num)
{