Skip to content

deprecate get() for uncompiled container builders #18728

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 3 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
8 changes: 8 additions & 0 deletions UPGRADE-3.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
UPGRADE FROM 3.1 to 3.2
=======================

DependencyInjection
-------------------

* Calling `get()` on a `ContainerBuilder` instance before compiling the
container is deprecated and will throw an exception in Symfony 4.0.
3 changes: 3 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ UPGRADE FROM 3.x to 4.0
DependencyInjection
-------------------

* Calling `get()` on a `ContainerBuilder` instance before compiling the
container is not supported anymore and will throw an exception.
Copy link
Contributor

Choose a reason for hiding this comment

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

wdyt about replacing and will throw by and throws ?


* Using unsupported configuration keys in YAML configuration files raises an
exception.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function testCreateProxyServiceWithRuntimeInstantiator()
$builder->register('foo1', 'ProxyManagerBridgeFooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
$builder->getDefinition('foo1')->setLazy(true);

$builder->compile();

/* @var $foo1 \ProxyManager\Proxy\LazyLoadingInterface|\ProxyManager\Proxy\ValueHolderInterface */
$foo1 = $builder->get('foo1');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ protected function resolveServiceDefinition(ContainerBuilder $builder, $serviceI
return $builder->getAlias($serviceId);
}

if ('service_container' === $serviceId) {
return $builder;
}

// the service has been injected in some special way, just return the service
return $builder->get($serviceId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public function testToolbarConfig($toolbarEnabled, $interceptRedirects, $listene

$this->assertSame($listenerInjected, $this->container->has('web_profiler.debug_toolbar'));

$this->assertSaneContainer($this->getDumpedContainer());

if ($listenerInjected) {
$this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());
}

$this->assertSaneContainer($this->getDumpedContainer());
}

public function getDebugModes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*/
private $usedTags = array();

private $compiled = false;

/**
* Sets the track resources flag.
*
Expand Down Expand Up @@ -409,6 +411,10 @@ 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);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

We may want to think about moving this down after the call to the parent class' get() method (so you would be able to retrieve services that have been added using set() before). Alternatively, deprecating set() too might be another way to go.

Copy link
Member

Choose a reason for hiding this comment

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

See #19192


$id = strtolower($id);

if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
Expand Down Expand Up @@ -543,6 +549,7 @@ public function compile()
}

$compiler->compile($this);
$this->compiled = true;

if ($this->trackResources) {
foreach ($this->definitions as $definition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,17 @@ private function findNodes()
}

foreach ($container->getServiceIds() as $id) {
$service = $container->get($id);

if (array_key_exists($id, $container->getAliases())) {
continue;
}

if (!$container->hasDefinition($id)) {
$class = ('service_container' === $id) ? get_class($this->container) : get_class($service);
if ('service_container' === $id) {
$class = get_class($this->container);
} else {
$class = get_class($container->get($id));
}

$nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function testProcessWithNonExistingAlias()
$pass->process($container);

$this->assertEquals('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->getDefinition('example')->getClass());
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassDefault', $container->get('example'));
}

public function testProcessWithExistingAlias()
Expand All @@ -75,7 +74,7 @@ public function testProcessWithExistingAlias()

$this->assertTrue($container->hasAlias('example'));
$this->assertEquals('mysql.example', $container->getAlias('example'));
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->get('example'));
$this->assertSame('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql', $container->getDefinition('mysql.example')->getClass());
}

public function testProcessWithManualAlias()
Expand All @@ -86,7 +85,7 @@ public function testProcessWithManualAlias()
->addTag('auto_alias', array('format' => '%existing%.example'));

$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb');
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb');
$container->setAlias('example', 'mariadb.example');
$container->setParameter('existing', 'mysql');

Expand All @@ -95,7 +94,7 @@ public function testProcessWithManualAlias()

$this->assertTrue($container->hasAlias('example'));
$this->assertEquals('mariadb.example', $container->getAlias('example'));
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example'));
$this->assertSame('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->getDefinition('mariadb.example')->getClass());
}
}

Expand Down
Loading