8000 deprecate get() for uncompiled container builders · symfony/symfony@f2e35cb · GitHub
[go: up one dir, main page]

Skip to content

Commit f2e35cb

Browse files
xabbuhfabpot
authored andcommitted
deprecate get() for uncompiled container builders
1 parent b85ab60 commit f2e35cb

File tree

9 files changed

+141
-37
lines changed

9 files changed

+141
-37
lines changed

UPGRADE-3.2.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
UPGRADE FROM 3.1 to 3.2
2+
=======================
3+
4+
DependencyInjection
5+
-------------------
6+
7+
* Calling `get()` on a `ContainerBuilder` instance before compiling the
8+
container is deprecated and will throw an exception in Symfony 4.0.

UPGRADE-4.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ UPGRADE FROM 3.x to 4.0
44
DependencyInjection
55
-------------------
66

7+
* Calling `get()` on a `ContainerBuilder` instance before compiling the
8+
container is not supported anymore and will throw an exception.
9+
710
* Using unsupported configuration keys in YAML configuration files raises an
811
exception.
912

src/Symfony/Bridge/ProxyManager/Tests/LazyProxy/ContainerBuilderTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function testCreateProxyServiceWithRuntimeInstantiator()
3333
$builder->register('foo1', 'ProxyManagerBridgeFooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
3434
$builder->getDefinition('foo1')->setLazy(true);
3535

36+
$builder->compile();
37+
3638
/* @var $foo1 \ProxyManager\Proxy\LazyLoadingInterface|\ProxyManager\Proxy\ValueHolderInterface */
3739
$foo1 = $builder->get('foo1');
3840

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ protected function resolveServiceDefinition(ContainerBuilder $builder, $serviceI
256256
return $builder->getAlias($serviceId);
257257
}
258258

259+
if ('service_container' === $serviceId) {
260+
return $builder;
261+
}
262+
259263
// the service has been injected in some special way, just return the service
260264
return $builder->get($serviceId);
261265
}

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public function testToolbarConfig($toolbarEnabled, $interceptRedirects, $listene
9696

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

99+
$this->assertSaneContainer($this->getDumpedContainer());
100+
99101
if ($listenerInjected) {
100102
$this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());
101103
}
102-
103-
$this->assertSaneContainer($this->getDumpedContainer());
104104
}
105105

106106
public function getDebugModes()

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
9494
*/
9595
private $usedTags = array();
9696

97+
private $compiled = false;
98+
9799
/**
98100
* Sets the track resources flag.
99101
*
@@ -409,6 +411,10 @@ public function has($id)
409411
*/
410412
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
411413
{
414+
if (!$this->compiled) {
415+
@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);
416+
}
417+
412418
$id = strtolower($id);
413419

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

545551
$compiler->compile($this);
552+
$this->compiled = true;
546553

547554
if ($this->trackResources) {
548555
foreach ($this->definitions as $definition) {

src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,17 @@ private function findNodes()
180180
}
181181

182182
foreach ($container->getServiceIds() as $id) {
183-
$service = $container->get($id);
184-
185183
if (array_key_exists($id, $container->getAliases())) {
186184
continue;
187185
}
188186

189187
if (!$container->hasDefinition($id)) {
190-
$class = ('service_container' === $id) ? get_class($this->container) : get_class($service);
188+
if ('service_container' === $id) {
189+
$class = get_class($this->container);
190+
} else {
191+
$class = get_class($container->get($id));
192+
}
193+
191194
$nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']);
192195
}
193196
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutoAliasServicePassTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public function testProcessWithNonExistingAlias()
5757
$pass->process($container);
5858

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

6362
public function testProcessWithExistingAlias()
@@ -75,7 +74,7 @@ public function testProcessWithExistingAlias()
7574

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

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

8887
$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
89-
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb');
88+
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb');
9089
$container->setAlias('example', 'mariadb.example');
9190
$container->setParameter('existing', 'mysql');
9291

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

9695
$this->assertTrue($container->hasAlias('example'));
9796
$this->assertEquals('mariadb.example', $container->getAlias('example'));
98-
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example'));
97+
$this->assertSame('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->getDefinition('mariadb.example')->getClass());
9998
}
10099
}
101100

0 commit comments

Comments
 (0)
0