8000 [DI] Allow getting synthetic before compilation by ro0NL · Pull Request #19715 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Allow getting synthetic before compilation #19715

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,6 @@ public function has($id)
*/
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
if (!$this->compiled) {
@trigger_error(sprintf('Calling %s() before compiling the container is deprecated since version 3.2 and will throw an exception in 4.0.', __METHOD__), E_USER_DEPRECATED);
}

$id = strtolower($id);

if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
Expand All @@ -415,6 +411,10 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
return $this->get($this->aliasDefinitions[$id]);
}

if (!$this->compiled) {
@trigger_error(sprintf('Calling %s() before compiling the container is deprecated for non-synthetic services since version 3.2 and will throw an exception in 4.0.', __METHOD__), E_USER_DEPRECATED);
}

try {
$definition = $this->getDefinition($id);
} catch (ServiceNotFoundException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,29 @@ public function testGetReturnsRegisteredService()
{
$builder = new ContainerBuilder();
$builder->set('bar', $bar = new \stdClass());

$this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id before compilation');

$builder->compile();

$this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id');
$this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id after compilation');
}

/**
* @group legacy
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
*/
public function testGetReturnsRegisteredServiceFromDefinitionBeforeCompilationIsDeprecated()
{
$deprecations = array(
'Calling Symfony\Component\DependencyInjection\ContainerBuilder::get() before compiling the container is deprecated for non-synthetic services since version 3.2 and will throw an exception in 4.0.',
);

ErrorAssert::assertDeprecationsAreTriggered($deprecations, function () {
$builder = new ContainerBuilder();
$builder->register('foo', 'stdClass');
$this->assertEquals(new \stdClass(), $builder->get('foo'));
});
}

public function testRegisterDoesNotOverrideExistingService()
Expand Down
0