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

Skip to content

[DI] Check for privates before shared services #22866

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
May 24, 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
9 changes: 9 additions & 0 deletions UPGRADE-3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -98,7 +101,7 @@ public function testToolbarConfig($toolbarEnabled, $interceptRedirects, $listene

$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());
Expand Down
15 changes: 8 additions & 7 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,17 @@ public function set($id, $service)
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
|| isset($this->aliases[$id])
|| isset($this->services[$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->methodMap[$id])) {
return true;
}
Expand Down Expand Up @@ -262,6 +262,10 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
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];
Expand Down Expand Up @@ -300,9 +304,6 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE

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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,6 @@ public function compile()
$compiler->compile($this);

foreach ($this->definitions as $id => $definition) {
if (!$definition->isPublic()) {
$this->privates[$id] = true;
}
if ($this->trackResources && $definition->isLazy() && ($class = $definition->getClass()) && class_exists($class)) {
$this->addClassResource(new \ReflectionClass($class));
}
Expand Down
21 changes: 17 additions & 4 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function testGetServiceIds()

$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()');
}

/**
Expand Down Expand Up @@ -397,7 +397,8 @@ public function testUnsetInternalPrivateServiceIsDeprecated()
public function testChangeInternalPrivateServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->set('internal', new \stdClass());
$c->set('internal', $internal = new \stdClass());
$this->assertSame($c->get('internal'), $internal);
}

/**
Expand All @@ -407,7 +408,8 @@ public function testChangeInternalPrivateServiceIsDeprecated()
public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->has('internal');
$c->get('internal_dependency');
$this->assertTrue($c->has('internal'));
}

/**
Expand All @@ -417,6 +419,7 @@ public function testCheckExistenceOfAnInternalPrivateServiceIsDeprecated()
public function testRequestAnInternalSharedPrivateServiceIsDeprecated()
{
$c = new ProjectServiceContainer();
$c->get('internal_dependency');
$c->get('internal');
}
}
Expand All @@ -435,6 +438,7 @@ class ProjectServiceContainer extends Container
'circular' => 'getCircularService',
'throw_exception' => 'getThrowExceptionService',
'throws_exception_on_service_configuration' => 'getThrowsExceptionOnServiceConfigurationService',
'internal_dependency' => 'getInternalDependencyService',
);

public function __construct()
Expand All @@ -451,7 +455,7 @@ public function __construct()

protected function getInternalService()
{
return $this->__internal;
return $this->services['internal'] = $this->__internal;
}

protected function getBarService()
Expand Down Expand Up @@ -485,6 +489,15 @@ protected function getThrowsExceptionOnServiceConfigurationService()

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
Expand Down