Skip to content

Commit 7569f7d

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 7569f7d

File tree

6 files changed

+37
-125
lines changed

6 files changed

+37
-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: 28 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
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\InactiveScopeException;
25+
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
2426
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2527
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
2628
use Symfony\Component\DependencyInjection\Reference;
@@ -69,7 +71,6 @@ public function testCreateDeprecatedService()
6971

7072
$builder = new ContainerBuilder();
7173
$builder->setDefinition('deprecated_foo', $definition);
72-
$builder->compile();
7374
$builder->get('deprecated_foo');
7475
}
7576

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

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

110105
$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-
}
132106

133-
public function testGetCreatesServiceBasedOnDefinition()
134-
{
135-
$builder = new ContainerBuilder();
136107
$builder->register('foo', 'stdClass');
137-
$builder->compile();
138-
139108
$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();
154109
$builder->set('bar', $bar = new \stdClass());
110+
$this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id');
155111
$builder->register('bar', 'stdClass');
156-
$builder->compile();
112+
$this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
113+
114+
$builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
115+
try {
116+
@$builder->get('baz');
117+
$this->fail('->get() throws a ServiceCircularReferenceException if the service has a circular reference to itself');
118+
} catch (\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException $e) {
119+
$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');
120+
}
157121

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

161125
public function testNonSharedServicesReturnsDifferentInstances()
162126
{
163127
$builder = new ContainerBuilder();
164128
$builder->register('bar', 'stdClass')->setShared(false);
165129

166-
$builder->compile();
167-
168130
$this->assertNotSame($builder->get('bar'), $builder->get('bar'));
169131
}
170132

@@ -177,8 +139,6 @@ public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
177139
$builder = new ContainerBuilder();
178140
$builder->register('foo', 'stdClass')->setSynthetic(true);
179141

180-
$builder->compile();
181-
182142
// we expect a RuntimeException here as foo is synthetic
183143
try {
184144
$builder->get('foo');
@@ -207,9 +167,6 @@ public function testAliases()
207167
$this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
208168
$this->assertEquals('foo', (string) $builder->getAlias('bar'), '->getAlias() returns the aliased service');
209169
$this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
210-
211-
$builder->compile();
212-
213170
$this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');
214171

215172
try {
@@ -277,9 +234,6 @@ public function testSetReplacesAlias()
277234
$builder->set('aliased', new \stdClass());
278235

279236
$builder->set('alias', $foo = new \stdClass());
280-
281-
$builder->compile();
282-
283237
$this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias');
284238
}
285239

@@ -301,12 +255,9 @@ public function testCreateService()
301255
{
302256
$builder = new ContainerBuilder();
303257
$builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
258+
$this->assertInstanceOf('\Bar\FooClass', $builder->get('foo1'), '->createService() requires the file defined by the service definition');
304259
$builder->register('foo2', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
305260
$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');
310261
$this->assertInstanceOf('\Bar\FooClass', $builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
311262
}
312263

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

320-
$builder->compile();
321-
322271
$foo1 = $builder->get('foo1');
323272

324273
$this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
@@ -330,9 +279,6 @@ public function testCreateServiceClass()
330279
$builder = new ContainerBuilder();
331280
$builder->register('foo1', '%class%');
332281
$builder->setParameter('class', 'stdClass');
333-
334-
$builder->compile();
335-
336282
$this->assertInstanceOf('\stdClass', $builder->get('foo1'), '->createService() replaces parameters in the class provided by the service definition');
337283
}
338284

@@ -342,9 +288,6 @@ public function testCreateServiceArguments()
342288
$builder->register('bar', 'stdClass');
343289
$builder->register('foo1', 'Bar\FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'), '%%unescape_it%%'));
344290
$builder->setParameter('value', 'bar');
345-
346-
$builder->compile();
347-
348291
$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');
349292
}
350293

@@ -356,8 +299,6 @@ public function testCreateServiceFactory()
356299
$builder->register('bar', 'Bar\FooClass')->setFactory(array(new Definition('Bar\FooClass'), 'getInstance'));
357300
$builder->register('baz', 'Bar\FooClass')->setFactory(array(new Reference('bar'), 'getInstance'));
358301

359-
$builder->compile();
360-
361302
$this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance');
362303
$this->assertTrue($builder->get('qux')->called, '->createService() calls the factory method to create the service instance');
363304
$this->assertTrue($builder->get('bar')->called, '->createService() uses anonymous service as factory');
@@ -370,9 +311,6 @@ public function testCreateServiceMethodCalls()
370311
$builder->register('bar', 'stdClass');
371312
$builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar'))));
372313
$builder->setParameter('value', 'bar');
373-
374-
$builder->compile();
375-
376314
$this->assertEquals(array('bar', $builder->get('bar')), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
377315
}
378316

@@ -382,9 +320,6 @@ public function testCreateServiceMethodCallsWithEscapedParam()
382320
$builder->register('bar', 'stdClass');
383321
$builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%%unescape_it%%')));
384322
$builder->setParameter('value', 'bar');
385-
386-
$builder->compile();
387-
388323
$this->assertEquals(array('%unescape_it%'), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
389324
}
390325

@@ -394,30 +329,27 @@ public function testCreateServiceProperties()
394329
$builder->register('bar', 'stdClass');
395330
$builder->register('foo1', 'Bar\FooClass')->setProperty('bar', array('%value%', new Reference('bar'), '%%unescape_it%%'));
396331
$builder->setParameter('value', 'bar');
397-
398-
$builder->compile();
399-
400332
$this->assertEquals(array('bar', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->bar, '->createService() replaces the values in the properties');
401333
}
402334

403335
public function testCreateServiceConfigurator()
404336
{
405337
$builder = new ContainerBuilder();
406338
$builder->register('foo1', 'Bar\FooClass')->setConfigurator('sc_configure');
339+
$this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator');
340+
407341
$builder->register('foo2', 'Bar\FooClass')->setConfigurator(array('%class%', 'configureStatic'));
408342
$builder->setParameter('class', 'BazClass');
343+
$this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator');
344+
409345
$builder->register('baz', 'BazClass');
410346
$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');
418347
$this->assertTrue($builder->get('foo3')->configured, '->createService() calls the configurator');
348+
349+
$builder->register('foo4', 'Bar\FooClass')->setConfigurator(array($builder->getDefinition('baz'), 'configure'));
419350
$this->assertTrue($builder->get('foo4')->configured, '->createService() calls the configurator');
420351

352+
$builder->register('foo5', 'Bar\FooClass')->setConfigurator('foo');
421353
try {
422354
$builder->get('foo5');
423355
$this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
@@ -433,9 +365,6 @@ public function testCreateSyntheticService()
433365
{
434366
$builder = new ContainerBuilder();
435367
$builder->register('foo', 'Bar\FooClass')->setSynthetic(true);
436-
437-
$builder->compile();
438-
439368
$builder->get('foo');
440369
}
441370

@@ -445,18 +374,13 @@ public function testCreateServiceWithExpression()
445374
$builder->setParameter('bar', 'bar');
446375
$builder->register('bar', 'BarClass');
447376
$builder->register('foo', 'Bar\FooClass')->addArgument(array('foo' => new Expression('service("bar").foo ~ parameter("bar")')));
448-
449-
$builder->compile();
450-
451377
$this->assertEquals('foobar', $builder->get('foo')->arguments['foo']);
452378
}
453379

454380
public function testResolveServices()
455381
{
456382
$builder = new ContainerBuilder();
457383
$builder->register('foo', 'Bar\FooClass');
458-
$builder->compile();
459-
460384
$this->assertEquals($builder->get('foo'), $builder->resolveServices(new Reference('foo')), '->resolveServices() resolves service references to service instances');
461385
$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');
462386
$this->assertEquals($builder->get('foo'), $builder->resolveServices(new Expression('service("foo")')), '->resolveServices() resolves expressions');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy