Skip to content

Commit

Permalink
feature #22803 [DI] Deprecate Container::initialized() for privates (…
Browse files Browse the repository at this point in the history
…ro0NL)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Deprecate Container::initialized() for privates

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes-ish
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

See #22801 (comment)

Failing test seems unrelated.

Commits
-------

e0eb247 [DI] Deprecate Container::initialized() for privates
  • Loading branch information
nicolas-grekas committed May 22, 2017
2 parents 98a2d3c + e0eb247 commit 5f29144
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.4.0
-----

* deprecated the ability to check for the initialization of a private service with the `Container::initialized()` method

3.3.0
-----

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -370,6 +370,10 @@ public function initialized($id)
$id = $this->aliases[$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 isset($this->services[$id]);
}

Expand Down
25 changes: 23 additions & 2 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Expand Up @@ -147,7 +147,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 @@ -363,6 +363,17 @@ 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'));
}

public function testReset()
{
$c = new Container();
Expand Down Expand Up @@ -504,6 +515,7 @@ class ProjectServiceContainer extends Container
'circular' => 'getCircularService',
'throw_exception' => 'getThrowExceptionService',
'throws_exception_on_service_configuration' => 'getThrowsExceptionOnServiceConfigurationService',
'internal_dependency' => 'getInternalDependencyService',
);

public function __construct()
Expand All @@ -520,7 +532,7 @@ public function __construct()

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

protected function getBarService()
Expand Down Expand Up @@ -554,6 +566,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

0 comments on commit 5f29144

Please sign in to comment.