Skip to content

Commit e2196ad

Browse files
[DI] deprecate support for non-object services
1 parent d6773bc commit e2196ad

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function has($id)
210210
* @param string $id The service identifier
211211
* @param int $invalidBehavior The behavior when the service does not exist
212212
*
213-
* @return object The associated service
213+
* @return object|null The associated service
214214
*
215215
* @throws ServiceCircularReferenceException When a circular reference is detected
216216
* @throws ServiceNotFoundException When the service is not defined
@@ -220,9 +220,15 @@ public function has($id)
220220
*/
221221
public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERENCE */ 1)
222222
{
223-
return $this->services[$id]
223+
$service = $this->services[$id]
224224
?? $this->services[$id = $this->aliases[$id] ?? $id]
225225
?? ('service_container' === $id ? $this : ($this->factories[$id] ?? [$this, 'make'])($id, $invalidBehavior));
226+
227+
if (!\is_object($service) && null !== $service) {
228+
@trigger_error(sprintf('Non-object services are deprecated since Symfony 4.4, please fix the "%s" service which is of type "%s" right now.', $id, \gettype($service)), E_USER_DEPRECATED);
229+
}
230+
231+
return $service;
226232
}
227233

228234
/**

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ public function has($id)
539539
* @param string $id The service identifier
540540
* @param int $invalidBehavior The behavior when the service does not exist
541541
*
542-
* @return object The associated service
542+
* @return object|null The associated service
543543
*
544544
* @throws InvalidArgumentException when no definitions are available
545545
* @throws ServiceCircularReferenceException When a circular reference is detected
@@ -554,7 +554,13 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
554554
return parent::get($id);
555555
}
556556

557-
return $this->doGet($id, $invalidBehavior);
557+
$service = $this->doGet($id, $invalidBehavior);
558+
559+
if (!\is_object($service) && null !== $service) {
560+
@trigger_error(sprintf('Non-object services are deprecated since Symfony 4.4, please fix the "%s" service which is of type "%s" right now.', $id, \gettype($service)), E_USER_DEPRECATED);
561+
}
562+
563+
return $service;
558564
}
559565

560566
private function doGet(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, array &$inlineServices = null, bool $isConstructorArgument = false)

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,16 +1573,17 @@ public function testDecoratedSelfReferenceInvolvingPrivateServices()
15731573

15741574
public function testScalarService()
15751575
{
1576-
$c = new ContainerBuilder();
1577-
$c->register('foo', 'string')
1578-
->setPublic(true)
1576+
$container = new ContainerBuilder();
1577+
$container->register('foo', 'string')
15791578
->setFactory([ScalarFactory::class, 'getSomeValue'])
15801579
;
1580+
$container->register('bar', 'stdClass')
1581+
->setProperty('foo', new Reference('foo'))
1582+
->setPublic(true)
1583+
;
1584+
$container->compile();
15811585

1582-
$c->compile();
1583-
1584-
$this->assertTrue($c->has('foo'));
1585-
$this->assertSame('some value', $c->get('foo'));
1586+
$this->assertSame('some value', $container->get('bar')->foo);
15861587
}
15871588

15881589
public function testWither()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ public function testHas()
288288
$this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
289289
}
290290

291+
/**
292+
* @group legacy
293+
*/
291294
public function testScalarService()
292295
{
293296
$c = new Container();

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,19 +1282,20 @@ public function testScalarService()
12821282
{
12831283
$container = new ContainerBuilder();
12841284
$container->register('foo', 'string')
1285-
->setPublic(true)
12861285
->setFactory([ScalarFactory::class, 'getSomeValue'])
12871286
;
1288-
1287+
$container->register('bar', 'stdClass')
1288+
->setProperty('foo', new Reference('foo'))
1289+
->setPublic(true)
1290+
;
12891291
$container->compile();
12901292

12911293
$dumper = new PhpDumper($container);
12921294
eval('?>'.$dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Scalar_Service']));
12931295

12941296
$container = new \Symfony_DI_PhpDumper_Test_Scalar_Service();
12951297

1296-
$this->assertTrue($container->has('foo'));
1297-
$this->assertSame('some value', $container->get('foo'));
1298+
$this->assertSame('some value', $container->get('bar')->foo);
12981299
}
12991300

13001301
public function testWither()

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public function provideBindings()
306306
public function testBindScalarValueToControllerArgument($bindingKey)
307307
{
308308
$container = new ContainerBuilder();
309-
$resolver = $container->register('argument_resolver.service')->addArgument([]);
309+
$resolver = $container->register('argument_resolver.service', 'stdClass')->addArgument([]);
310310

311311
$container->register('foo', ArgumentWithoutTypeController::class)
312312
->setBindings([$bindingKey => '%foo%'])
@@ -317,19 +317,13 @@ public function testBindScalarValueToControllerArgument($bindingKey)
317317
$pass = new RegisterControllerArgumentLocatorsPass();
318318
$pass->process($container);
319319

320-
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
320+
$locatorId = (string) $resolver->getArgument(0);
321+
$container->getDefinition($locatorId)->setPublic(true);
321322

322-
$locator = $container->getDefinition((string) $locator['foo::fooAction']->getValues()[0]);
323+
$container->compile();
323324

324-
// assert the locator has a someArg key
325-
$arguments = $locator->getArgument(0);
326-
$this->assertArrayHasKey('someArg', $arguments);
327-
$this->assertInstanceOf(ServiceClosureArgument::class, $arguments['someArg']);
328-
// get the Reference that someArg points to
329-
$reference = $arguments['someArg']->getValues()[0];
330-
// make sure this service *does* exist and returns the correct value
331-
$this->assertTrue($container->has((string) $reference));
332-
$this->assertSame('foo_val', $container->get((string) $reference));
325+
$locator = $container->get($locatorId);
326+
$this->assertSame('foo_val', $locator->get('foo::fooAction')->get('someArg'));
333327
}
334328

335329
public function provideBindScalarValueToControllerArgument()

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