8000 Revert "feature #18728 deprecate get() for uncompiled container build… · symfony/symfony@7f32e6d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f32e6d

Browse files
Revert "feature #18728 deprecate get() for uncompiled container builders (xabbuh)"
This reverts commit 27f4680, reversing changes made to e4177a0.
1 parent de78754 commit 7f32e6d

File tree

6 files changed

+36
-125
lines changed

6 files changed

+36
-125
lines changed

UPGRADE-4.0.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ Debug
1616
DependencyInjection
1717
-------------------
1818

19-
* Calling `get()` on a `ContainerBuilder` instance before compiling the
20-
container is not supported anymore and will throw an exception.
21-
2219
* Using unsupported configuration keys in YAML configuration files raises an
2320
exception.
2421

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

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

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

101-
$this->assertSaneContainer($this->getDumpedContainer());
102-
103101
if ($listenerInjected) {
104102
$this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled());
105103
}
104+
105+
$this->assertSaneContainer($this->getDumpedContainer());
106106
}
107107

108108
public function getDebugModes()

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
100100
*/
101101
private $envCounters = array();
102102

103-
private $compiled = false;
104-
105103
/**
106104
* Sets the track resources flag.
107105
*
@@ -419,10 +417,6 @@ public function has($id)
419417
*/
420418
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
421419
{
422-
if (!$this->compiled) {
423-
@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);
424-
}
425-
426420
$id = strtolower($id);
427421

428422
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
@@ -569,7 +563,6 @@ public function compile()
569563
}
570564

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

574567
foreach ($this->definitions as $id => $definition) {
575568
if (!$definition->isPublic()) {

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

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

182182
foreach ($container->getServiceIds() as $id) {
183+
$service = $container->get($id);
184+
183185
if (array_key_exists($id, $container->getAliases())) {
184186
continue;
185187
}
186188

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ 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'));
6061
}
6162

6263
public function testProcessWithExistingAlias()
@@ -74,7 +75,7 @@ public function testProcessWithExistingAlias()
7475

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

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

8788
$container->register('mysql.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMysql');
88-
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb');
89+
$container->register('mariadb.example', 'Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariadb');
8990
$container->setAlias('example', 'mariadb.example');
9091
$container->setParameter('existing', 'mysql');
9192

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

9596
$this->assertTrue($container->hasAlias('example'));
9697
$this->assertEquals('mariadb.example', $container->getAlias('example'));
97-
$this->assertSame('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->getDefinition('mariadb.example')->getClass());
98+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Tests\Compiler\ServiceClassMariaDb', $container->get('example'));
9899
}
99100
}
100101

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 27 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\DependencyInjection\ContainerInterface;
2222
use Symfony\Component\DependencyInjection\Definition;
2323
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
24+
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
2425
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2526
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
2627
use Symfony\Component\DependencyInjection\Reference;
@@ -69,7 +70,6 @@ public function testCreateDeprecatedService()
6970

7071
$builder = new ContainerBuilder();
7172
$builder->setDefinition('deprecated_foo', $definition);
72-
$builder->compile();
7373
$builder->get('deprecated_foo');
7474
}
7575

@@ -91,80 +91,41 @@ public function testHas()
9191
$this->assertTrue($builder->has('bar'), '->has() returns true if a service exists');
9292
}
9393

94-
/**
95-
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
96-
* @expectedExceptionMessage You have requested a non-existent service "foo".
97-
*/
98-
public function testGetThrowsExceptionIfServiceDoesNotExist()
94+
public function testGet()
9995
{
10096
$builder = new ContainerBuilder();
101-
$builder->compile();
102-
$builder->get('foo');
103-
}
104-
105-
public function testGetReturnsNullIfServiceDoesNotExistAndInvalidReferenceIsUsed()
106-
{
107-
$builder = new ContainerBuilder();
108-
$builder->compile();
97+
try {
98+
$builder->get('foo');
99+
$this->fail('->get() throws a ServiceNotFoundException if the service does not exist');
100+
} catch (ServiceNotFoundException $e) {
101+
$this->assertEquals('You have requested a non-existent service "foo".', $e->getMessage(), '->get() throws a ServiceNotFoundException if the service does not exist');
102+
}
109103

110104
$this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument');
111-
}
112-
113-
/**
114-
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
115-
*/
116-
public function testGetThrowsCircularReferenceExceptionIfServiceHasReferenceToItself()
117-
{
118-
$builder = new ContainerBuilder();
119-
$builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
120-
$builder->compile();
121-
$builder->get('baz');
122-
}
123-
124-
public function testGetReturnsSameInstanceWhenServiceIsShared()
125-
{
126-
$builder = new ContainerBuilder();
127-
$builder->register('bar', 'stdClass');
128-
$builder->compile();
129-
130-
$this->assertTrue($builder->get('bar') === $builder->get('bar'), '->get() always returns the same instance if the service is shared');
131-
}
132105

133-
public function testGetCreatesServiceBasedOnDefinition()
134-
{
135-
$builder = new ContainerBuilder();
136106
$builder->register('foo', 'stdClass');
137-
$builder->compile();
138-
139107
$this->assertInternalType('object', $builder->get('foo'), '->get() returns the service definition associated with the id');
140-
}
141-
142-
public function testGetReturnsRegisteredService()
143-
{
144-
$builder = new ContainerBuilder();
145-
$builder->set('bar', $bar = new \stdClass());
146-
$builder->compile();
147-
148-
$this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id');
149-
}
150-
151-
public function testRegisterDoesNotOverrideExistingService()
152-
{
153-
$builder = new ContainerBuilder();
154108
$builder->set('bar', $bar = new \stdClass());
109+
$this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id');
155110
$builder->register('bar', 'stdClass');
156-
$builder->compile();
111+
$this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
112+
113+
$builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
114+
try {
115+
@$builder->get('baz');
116+
$this->fail('->get() throws a ServiceCircularReferenceException if the service has a circular reference to itself');
117+
} catch (\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException $e) {
118+
$this->assertEquals('Circular reference detected for service "baz", path: "baz".', $e->getMessage(), '->get() throws a LogicException if the service has a circular reference to itself');
119+
}
157120

158-
$this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
121+
$this->assertTrue($builder->get('bar') === $builder->get('bar'), '->get() always returns the same instance if the service is shared');
159122
}
160123

161124
public function testNonSharedServicesReturnsDifferentInstances()
162125
{
163126
$builder = new ContainerBuilder();
164127
$builder->register('bar', 'stdClass')->setShared(false);
165128

166-
$builder->compile();
167-
168129
$this->assertNotSame($builder->get('bar'), $builder->get('bar'));
169130
}
170131

@@ -177,8 +138,6 @@ public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
177138
$builder = new ContainerBuilder();
178139
$builder->register('foo', 'stdClass')->setSynthetic(true);
179140

180-
$builder->compile();
181-
182141
// we expect a RuntimeException here as foo is synthetic
183142
try {
184143
$builder->get('foo');
@@ -207,9 +166,6 @@ public function testAliases()
207166
$this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
208167
$this->assertEquals('foo', (string) $builder->getAlias('bar'), '->getAlias() returns the aliased service');
209168
$this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
210-
211-
$builder->compile();
212-
213169
$this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');
214170

215171
try {
@@ -277,9 +233,6 @@ public function testSetReplacesAlias()
277233
$builder->set('aliased', new \stdClass());
278234

279235
$builder->set('alias', $foo = new \stdClass());
280-
281-
$builder->compile();
282-
283236
$this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias');
284237
}
285238

@@ -301,12 +254,9 @@ public function testCreateService()
301254
{
302255
$builder = new ContainerBuilder();
303256
$builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
257+
$this->assertInstanceOf('\Bar\FooClass', $builder->get('foo1'), '->createService() requires the file defined by the service definition');
304258
$builder->register('foo2', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
305259
$builder->setParameter('file', 'foo');
306-
307-
$builder->compile();
308-
309-
$this->assertInstanceOf('\Bar\FooClass', $builder->get('foo1'), '->createService() requires the file defined by the service definition');
310260
$this->assertInstanceOf('\Bar\FooClass', $builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
311261
}
312262

@@ -317,8 +267,6 @@ public function testCreateProxyWithRealServiceInstantiator()
317267
$builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
318268
$builder->getDefinition('foo1')->setLazy(true);
319269

320-
$builder->compile();
321-
322270
$foo1 = $builder->get('foo1');
323271

324272
$this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
@@ -330,9 +278,6 @@ public function testCreateServiceClass()
330278
$builder = new ContainerBuilder();
331279
$builder->register('foo1', '%class%');
332280
$builder->setParameter('class', 'stdClass');
333-
334-
$builder->compile();
335-
336281
$this->assertInstanceOf('\stdClass', $builder->get('foo1'), '->createService() replaces parameters in the class provided by the service definition');
337282
}
338283

@@ -342,9 +287,6 @@ public function testCreateServiceArguments()
342287
$builder->register('bar', 'stdClass');
343288
$builder->register('foo1', 'Bar\FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'), '%%unescape_it%%'));
344289
$builder->setParameter('value', 'bar');
345-
346-
$builder->compile();
347-
348290
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->arguments, '->createService() replaces parameters and service references in the arguments provided by the service definition');
349291
}
350292

@@ -356,8 +298,6 @@ public function testCreateServiceFactory()
356298
$builder->register('bar', 'Bar\FooClass')->setFactory(array(new Definition('Bar\FooClass'), 'getInstance'));
357299
$builder->register('baz', 'Bar\FooClass')->setFactory(array(new Reference('bar'), 'getInstance'));
358300

359-
$builder->compile();
360-
361301
$this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance');
362302
$this->assertTrue($builder->get('qux')->called, '->createService() calls the factory method to create the service instance');
363303
$this->assertTrue($builder->get('bar')->called, '->createService() uses anonymous service as factory');
@@ -370,9 +310,6 @@ public function testCreateServiceMethodCalls()
370310
$builder->register('bar', 'stdClass');
371311
$builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar'))));
372312
$builder->setParameter('value', 'bar');
373-
374-
$builder->compile();
375-
376313
$this->assertEquals(array('bar', $builder->get('bar')), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
377314
}
378315

@@ -382,9 +319,6 @@ public function testCreateServiceMethodCallsWithEscapedParam()
382319
$builder->register('bar', 'stdClass');
383320
$builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%%unescape_it%%')));
384321
$builder->setParameter('value', 'bar');
385-
386-
$builder->compile();
387-
388322
$this->assertEquals(array('%unescape_it%'), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
389323
}
390324

@@ -394,30 +328,27 @@ public function testCreateServiceProperties()
394328
$builder->register('bar', 'stdClass');
395329
$builder->register('foo1', 'Bar\FooClass')->setProperty('bar', array('%value%', new Reference('bar'), '%%unescape_it%%'));
396330
$builder->setParameter('value', 'bar');
397-
398-
$builder->compile();
399-
400331
$this->assertEquals(array('bar', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->bar, '->createService() replaces the values in the properties');
401332
}
402333

403334
public function testCreateServiceConfigurator()
404335
{
405336
$builder = new ContainerBuilder();
406337
$builder->register('foo1', 'Bar\FooClass')->setConfigurator('sc_configure');
338+
$this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator');
339+
407340
$builder->register('foo2', 'Bar\FooClass')->setConfigurator(array('%class%', 'configureStatic'));
408341
$builder->setParameter('class', 'BazClass');
342+
$this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator');
343+
409344
$builder->register('baz', 'BazClass');
410345
$builder->register('foo3', 'Bar\FooClass')->setConfigurator(array(new Reference('baz'), 'configure'));
411-
$builder->register('foo4', 'Bar\FooClass')->setConfigurator(array($builder->getDefinition('baz'), 'configure'));
412-
$builder->register('foo5', 'Bar\FooClass')->setConfigurator('foo');
413-
414-
$builder->compile();
415-
416-
$this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator');
417-
$this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator');
418346
$this->assertTrue($builder->get('foo3')->configured, '->createService() calls the configurator');
347+
348+
$builder->register('foo4', 'Bar\FooClass')->setConfigurator(array($builder->getDefinition('baz'), 'configure'));
419349
$this->assertTrue($builder->get('foo4')->configured, '->createService() calls the configurator');
420350

351+
$builder->register('foo5', 'Bar\FooClass')->setConfigurator('foo');
421352
try {
422353
$builder->get('foo5');
423354
$this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
@@ -433,9 +364,6 @@ public function testCreateSyntheticService()
433364
{
434365
$builder = new ContainerBuilder();
435366
$builder->register('foo', 'Bar\FooClass')->setSynthetic(true);
436-
437-
$builder->compile();
438-
439367
$builder->get('foo');
440368
}
441369

@@ -445,18 +373,13 @@ public function testCreateServiceWithExpression()
445373
$builder->setParameter('bar', 'bar');
446374
$builder->register('bar', 'BarClass');
447375
$builder->register('foo', 'Bar\FooClass')->addArgument(array('foo' => new Expression('service("bar").foo ~ parameter("bar")')));
448-
449-
$builder->compile();
450-
451376
$this->assertEquals('foobar', $builder->get('foo')->arguments['foo']);
452377
}
453378

454379
public function testResolveServices()
455380
{
456381
$builder = new ContainerBuilder();
457382
$builder->register('foo', 'Bar\FooClass');
458-
$builder->compile();
459-
460383
$this->assertEquals($builder->get('foo'), $builder->resolveServices(new Reference('foo')), '->resolveServices() resolves service references to service instances');
461384
$this->assertEquals(array('foo' => array('foo', $builder->get('foo'))), $builder->resolveServices(array('foo' => array('foo', new Reference('foo')))), '->resolveServices() resolves service references to service instances in nested arrays');
462385
$this->assertEquals($builder->get('foo'), $builder->resolveServices(new Expression('service("foo")')), '->resolveServices() resolves expressions');

0 commit comments

Comments
 (0)
0