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

Skip to content

[DI] Removed deprecated setting private/pre-defined services #22801

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
Jul 17, 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
2 changes: 2 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ CHANGELOG
* removed `ContainerBuilder::addClassResource()`, use the `addObjectResource()` or the `getReflectionClass()` method instead.
* removed support for top-level anonymous services
* removed silent behavior for unused attributes and elements
* removed support for setting and accessing private services in `Container`
* removed support for setting pre-defined services in `Container`

3.4.0
-----
Expand Down
37 changes: 18 additions & 19 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,25 @@ public function set($id, $service)
throw new InvalidArgumentException('You cannot set service "service_container".');
}

if (isset($this->privates[$id])) {
throw new InvalidArgumentException(sprintf('You cannot set the private service "%s".', $id));
}

if (isset($this->methodMap[$id])) {
throw new InvalidArgumentException(sprintf('You cannot set the pre-defined service "%s".', $id));
}

if (isset($this->aliases[$id])) {
unset($this->aliases[$id]);
}

$this->services[$id] = $service;

if (null === $service) {
unset($this->services[$id]);
}

if (isset($this->privates[$id])) {
if (null === $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.', $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);
}
return;
}

$this->services[$id] = $service;
}

/**
Expand All @@ -187,7 +182,7 @@ public function set($id, $service)
public function has($id)
{
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);
return false;
}
if ('service_container' === $id) {
return true;
Expand Down Expand Up @@ -226,7 +221,11 @@ public function has($id)
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
{
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 (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
throw new ServiceNotFoundException($id);
}

return;
}
if ('service_container' === $id) {
return $this;
Expand Down Expand Up @@ -295,7 +294,7 @@ public function initialized($id)
}

if (isset($this->privates[$id])) {
@trigger_error(sprintf('Checking for the initialization of the "%s" private service is deprecated since Symfony 3.4 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
return false;
Copy link
Contributor Author

@ro0NL ro0NL Jul 12, 2017

Choose a reason for hiding this comment

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

@nicolas-grekas made a mistake here... alias ID resolution happens first. We dont do that in has etc.

Although i think we actually should do that =/ (could be a new feature and/or patch 3.3/4; wdyt?)

edit: see #23492

}

if (isset($this->aliases[$id])) {
Expand Down
61 changes: 17 additions & 44 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function testSet()
public function testSetWithNullResetTheService()
{
$sc = new Container();
$sc->set('foo', new \stdClass());
$sc->set('foo', null);
$this->assertFalse($sc->has('foo'), '->set() with null service resets the service');
}
Expand All @@ -159,22 +160,6 @@ 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 Down Expand Up @@ -275,15 +260,11 @@ public function testInitialized()
$this->assertTrue($sc->initialized('alias'), '->initialized() returns true for alias if aliased service is initialized');
}

/**
* @group legacy
* @expectedDeprecation Checking for the initialization of the "internal" private service is deprecated since Symfony 3.4 and won't be supported anymore in Symfony 4.0.
*/
public function testInitializedWithPrivateService()
{
$sc = new ProjectServiceContainer();
$sc->get('internal_dependency');
$this->assertTrue($sc->initialized('internal'));
$this->assertFalse($sc->initialized('internal'));
}

public function testReset()
Expand Down Expand Up @@ -360,59 +341,51 @@ public function testThatCloningIsNotSupported()
}

/**
* @group legacy
* @expectedDeprecation Unsetting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage You cannot set the private service "internal".
*/
public function testUnsetInternalPrivateServiceIsDeprecated()
public function testUnsetInternalPrivateService()
{
$c = new ProjectServiceContainer();
$c->set('internal', null);
}

/**
* @group legacy
* @expectedDeprecation Setting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage You cannot set the private service "internal".
*/
public function testChangeInternalPrivateServiceIsDeprecated()
public function testChangeInternalPrivateService()
{
$c = new ProjectServiceContainer();
$c->set('internal', $internal = new \stdClass());
$this->assertSame($c->get('internal'), $internal);
$c->set('internal', new \stdClass());
}

/**
* @group legacy
* @expectedDeprecation Checking for the existence of the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
*/
public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
public function testCheckExistenceOfAnInternalPrivateService()
{
$c = new ProjectServiceContainer();
$c->get('internal_dependency');
$this->assertTrue($c->has('internal'));
$this->assertFalse($c->has('internal'));
}

/**
* @group legacy
* @expectedDeprecation Requesting the "internal" private service is deprecated since Symfony 3.2 and won't be supported anymore in Symfony 4.0.
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
* @expectedExceptionMessage You have requested a non-existent service "internal".
*/
public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
public function testRequestAnInternalSharedPrivateService()
{
$c = new ProjectServiceContainer();
$c->get('internal_dependency');
$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.
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage You cannot set the pre-defined service "bar".
*/
public function testReplacingAPreDefinedServiceIsDeprecated()
public function testReplacingAPreDefinedService()
{
$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');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ public function testFrozenContainerWithoutAliases()
}

/**
* @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.
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage You cannot set the pre-defined service "bar".
*/
public function testOverrideServiceWhenUsingADumpedContainer()
{
Expand All @@ -277,25 +277,6 @@ public function testOverrideServiceWhenUsingADumpedContainer()

$container = new \ProjectServiceContainer();
$container->set('bar', $bar = new \stdClass());
$container->setParameter('foo_bar', 'foo_bar');

$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';
require_once self::$fixturesPath.'/includes/foo.php';
require_once self::$fixturesPath.'/includes/classes.php';

$container = new \ProjectServiceContainer();
$container->set('bar', $bar = new \stdClass());

$this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
}

/**
Expand Down