10000 feature #20533 [DI] Revert "deprecate get() for uncompiled container … · symfony/symfony@05c3e86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 05c3e86

Browse files
committed
feature #20533 [DI] Revert "deprecate get() for uncompiled container builders" (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [DI] Revert "deprecate get() for uncompiled container builders" | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18728 | License | MIT | Doc PR | - ping @xabbuh This reverts commit 27f4680, reversing changes made to e4177a0. While upgrading a few projects to 3.2, I found it impossible to remove this deprecation without duplicating "by hand" the instantiation logic of some service needed in a compiler pass. See https://github.com/dustin10/VichUploaderBundle/blob/master/DependencyInjection/Compiler/RegisterPropelModelsPass.php#L30 for such an example into the wild. I think we should revert this deprecation. Using the container builder before it is compiled is tricky. Yet, bootstrapping in general is tricky and full of chicken-and-egg issues. In this case, using some services while bootstrapping the container works well. Deprecating would be fine if we were to provide some viable alternative. Duplicating the instantiation logic doesn't qualify as such to me, hence the proposed revert. @xabbuh, if you'd like to keep some test cases that may be unrelated to the revert, please add comments on the PR, or better 8000 : push on my fork, branch revert-get-deprec. Commits ------- e449af0 Revert "feature #18728 deprecate get() for uncompiled container builders (xabbuh)"
2 parents 6a6e330 + e449af0 commit 05c3e86

File tree

4 files changed

+1
-66
lines changed

4 files changed

+1
-66
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/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 6D40

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: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,7 @@ private function findNodes()
185185
}
186186

187187
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-
188+
$class = get_class('service_container' === $id ? $this->container : $container->get($id));
194189
$nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']);
195190
}
196191
}

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

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function testCreateDeprecatedService()
6969

7070
$builder = new ContainerBuilder();
7171
$builder->setDefinition('deprecated_foo', $definition);
72-
$builder->compile();
7372
$builder->get('deprecated_foo');
7473
}
7574

@@ -98,14 +97,12 @@ public function testHas()
9897
public function testGetThrowsExceptionIfServiceDoesNotExist()
9998
{
10099
$builder = new ContainerBuilder();
101-
$builder->compile();
102100
$builder->get('foo');
103101
}
104102

105103
public function testGetReturnsNullIfServiceDoesNotExistAndInvalidReferenceIsUsed()
106104
{
107105
$builder = new ContainerBuilder();
108-
$builder->compile();
109106

110107
$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');
111108
}
@@ -117,15 +114,13 @@ public function testGetThrowsCircularReferenceExceptionIfServiceHasReferenceToIt
117114
{
118115
$builder = new ContainerBuilder();
119116
$builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
120-
$builder->compile();
121117
$builder->get('baz');
122118
}
123119

124120
public function testGetReturnsSameInstanceWhenServiceIsShared()
125121
{
126122
$builder = new ContainerBuilder();
127123
$builder->register('bar', 'stdClass');
128-
$builder->compile();
129124

130125
$this->assertTrue($builder->get('bar') === $builder->get('bar'), '->get() always returns the same instance if the service is shared');
131126
}
@@ -134,7 +129,6 @@ public function testGetCreatesServiceBasedOnDefinition()
134129
{
135130
$builder = new ContainerBuilder();
136131
$builder->register('foo', 'stdClass');
137-
$builder->compile();
138132

139133
$this->assertInternalType('object', $builder->get('foo'), '->get() returns the service definition associated with the id');
140134
}
@@ -143,7 +137,6 @@ public function testGetReturnsRegisteredService()
143137
{
144138
$builder = new ContainerBuilder();
145139
$builder->set('bar', $bar = new \stdClass());
146-
$builder->compile();
147140

148141
$this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id');
149142
}
@@ -153,7 +146,6 @@ public function testRegisterDoesNotOverrideExistingService()
153146
$builder = new ContainerBuilder();
154147
$builder->set('bar', $bar = new \stdClass());
155148
$builder->register('bar', 'stdClass');
156-
$builder->compile();
157149

158150
$this->assertSame($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
159151
}
@@ -163,8 +155,6 @@ public function testNonSharedServicesReturnsDifferentInstances()
163155
$builder = new ContainerBuilder();
164156
$builder->register('bar', 'stdClass')->setShared(false);
165157

166-
$builder->compile();
167-
168158
$this->assertNotSame($builder->get('bar'), $builder->get('bar'));
169159
}
170160

@@ -177,8 +167,6 @@ public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
177167
$builder = new ContainerBuilder();
178168
$builder->register('foo', 'stdClass')->setSynthetic(true);
179169

180-
$builder->compile();
181-
182170
// we expect a RuntimeException here as foo is synthetic
183171
try {
184172
$builder->get('foo');
@@ -207,9 +195,6 @@ public function testAliases()
207195
$this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
208196
$this->assertEquals('foo', (string) $builder->getAlias('bar'), '->getAlias() returns the aliased service');
209197
$this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
210-
211-
$builder->compile();
212-
213198
$this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');
214199

215200
try {
@@ -277,9 +262,6 @@ public function testSetReplacesAlias()
277262
$builder->set('aliased', new \stdClass());
278263

279264
$builder->set('alias', $foo = new \stdClass());
280-
281-
$builder->compile();
282-
283265
$this->assertSame($foo, $builder->get('alias'), '->set() replaces an existing alias');
284266
}
285267

@@ -303,9 +285,6 @@ public function testCreateService()
303285
$builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
304286
$builder->register('foo2', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
305287
$builder->setParameter('file', 'foo');
306-
307-
$builder->compile();
308-
309288
$this->assertInstanceOf('\Bar\FooClass', $builder->get('foo1'), '->createService() requires the file defined by the service definition');
310289
$this->assertInstanceOf('\Bar\FooClass', $builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
311290
}
@@ -317,8 +296,6 @@ public function testCreateProxyWithRealServiceInstantiator()
317296
$builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
318297
$builder->getDefinition('foo1')->setLazy(true);
319298

320-
$builder->compile();
321-
322299
$foo1 = $builder->get('foo1');
323300

324301
$this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
@@ -330,9 +307,6 @@ public function testCreateServiceClass()
330307
$builder = new ContainerBuilder();
331308
$builder->register('foo1', '%class%');
332309
$builder->setParameter('class', 'stdClass');
333-
334-
$builder->compile();
335-
336310
$this->assertInstanceOf('\stdClass', $builder->get('foo1'), '->createService() replaces parameters in the class provided by the service definition');
337311
}
338312

@@ -342,9 +316,6 @@ public function testCreateServiceArguments()
342316
$builder->register('bar', 'stdClass');
343317
$builder->register('foo1', 'Bar\FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'), '%%unescape_it%%'));
344318
$builder->setParameter('value', 'bar');
345-
346-
$builder->compile();
347-
348319
$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');
349320
}
350321

@@ -356,8 +327,6 @@ public function testCreateServiceFactory()
356327
$builder->register('bar', 'Bar\FooClass')->setFactory(array(new Definition('Bar\FooClass'), 'getInstance'));
357328
$builder->register('baz', 'Bar\FooClass')->setFactory(array(new Reference('bar'), 'getInstance'));
358329

359-
$builder->compile();
360-
361330
$this->assertTrue($builder->get('foo')->called, '->createService() calls the factory method to create the service instance');
362331
$this->assertTrue($builder->get('qux')->called, '->createService() calls the factory method to create the service instance');
363332
$this->assertTrue($builder->get('bar')->called, '->createService() uses anonymous service as factory');
@@ -370,9 +339,6 @@ public function testCreateServiceMethodCalls()
370339
$builder->register('bar', 'stdClass');
371340
$builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar'))));
372341
$builder->setParameter('value', < 10000 span class=pl-s>'bar');
373-
374-
$builder->compile();
375-
376342
$this->assertEquals(array('bar', $builder->get('bar')), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
377343
}
378344

@@ -382,9 +348,6 @@ public function testCreateServiceMethodCallsWithEscapedParam()
382348
$builder->register('bar', 'stdClass');
383349
$builder->register('foo1', 'Bar\FooClass')->addMethodCall('setBar', array(array('%%unescape_it%%')));
384350
$builder->setParameter('value', 'bar');
385-
386-
$builder->compile();
387-
388351
$this->assertEquals(array('%unescape_it%'), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
389352
}
390353

@@ -394,9 +357,6 @@ public function testCreateServiceProperties()
394357
$builder->register('bar', 'stdClass');
395358
$builder->register('foo1', 'Bar\FooClass')->setProperty('bar', array('%value%', new Reference('bar'), '%%unescape_it%%'));
396359
$builder->setParameter('value', 'bar');
397-
398-
$builder->compile();
399-
400360
$this->assertEquals(array('bar', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->bar, '->createService() replaces the values in the properties');
401361
}
402362

@@ -411,8 +371,6 @@ public function testCreateServiceConfigurator()
411371
$builder->register('foo4', 'Bar\FooClass')->setConfigurator(array($builder->getDefinition('baz'), 'configure'));
412372
$builder->register('foo5', 'Bar\FooClass')->setConfigurator('foo');
413373

414-
$builder->compile();
415-
416374
$this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator');
417375
$this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator');
418376
$this->assertTrue($builder->get('foo3')->configured, '->createService() calls the configurator');
@@ -433,9 +391,6 @@ public function testCreateSyntheticService()
433391
{
434392
$builder = new ContainerBuilder();
435393
$builder->register('foo', 'Bar\FooClass')->setSynthetic(true);
436-
437-
$builder->compile();
438-
439394
$builder->get('foo');
440395
}
441396

@@ -445,18 +400,13 @@ public function testCreateServiceWithExpression()
445400
$builder->setParameter('bar', 'bar');
446401
$builder->register('bar', 'BarClass');
447402
$builder->register('foo', 'Bar\FooClass')->addArgument(array('foo' => new Expression('service("bar").foo ~ parameter("bar")')));
448-
449-
$builder->compile();
450-
451403
$this->assertEquals('foobar', $builder->get('foo')->arguments['foo']);
452404
}
453405

454406
public function testResolveServices()
455407
{
456408
$builder = new ContainerBuilder();
457409
$builder->register('foo', 'Bar\FooClass');
458-
$builder->compile();
459-
460410
$this->assertEquals($builder->get('foo'), $builder->resolveServices(new Reference('foo')), '->resolveServices() resolves service references to service instances');
461411
$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');
462412
$this->assertEquals($builder->get('foo'), $builder->resolveServices(new Expression('service("foo")')), '->resolveServices() resolves expressions');

0 commit comments

Comments
 (0)
0