Skip to content

Commit

Permalink
feature #18728 deprecate get() for uncompiled container builders (xab…
Browse files Browse the repository at this point in the history
…buh)

This PR was squashed before being merged into the 3.2-dev branch (closes #18728).

Discussion
----------

deprecate get() for uncompiled container builders

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #18673 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

f2e35cb deprecate get() for uncompiled container builders
  • Loading branch information
fabpot committed Jun 8, 2016
2 parents e4177a0 + f2e35cb commit 27f4680
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 37 deletions.
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.

* 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 @@ -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);
}

$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

0 comments on commit 27f4680

Please sign in to comment.