8000 feature #33137 [DI] deprecate support for non-object services (nicola… · symfony/symfony@50c5911 · GitHub
[go: up one dir, main page]

Skip to content

Commit 50c5911

Browse files
feature #33137 [DI] deprecate support for non-object services (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [DI] deprecate support for non-object services | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32411 | License | MIT | Doc PR | - Follows #32432 /cc @derrabus Prepares for adding the `?object` return-type on master. Commits ------- 7c01c4c [DI] deprecate support for non-object services
2 parents 32e0a25 + 7c01c4c commit 50c5911

File tree

6 files changed

+48
-33
lines changed

6 files changed

+48
-33
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public function setParameter($name, $value)
141141
* Setting a synthetic service to null resets it: has() returns false and get()
142142
* behaves in the same way as if the service was never created.
143143
*
144-
* @param string $id The service identifier
145-
* @param object $service The service instance
144+
* @param string $id The service identifier
145+
* @param object|null $service The service instance
146146
*/
147147
public function set($id, $service)
148148
{
@@ -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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,17 @@ public function getCompiler()
487487
/**
488488
* Sets a service.
489489
*
490-
* @param string $id The service identifier
491-
* @param object $service The service instance
490+
* @param string $id The service identifier
491+
* @param object|null $service The service instance
492492
*
493493
* @throws BadMethodCallException When this ContainerBuilder is compiled
494494
*/
495495
public function set($id, $service)
496496
{
497+
if (!\is_object($service) && null !== $service) {
498+
@trigger_error(sprintf('Non-object services are deprecated since Symfony 4.4, setting the "%s" service to a value of type "%s" should be avoided.', $id, \gettype($service)), E_USER_DEPRECATED);
499+
}
500+
497501
$id = (string) $id;
498502

499503
if ($this->isCompiled() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) {
@@ -539,7 +543,7 @@ public function has($id)
539543
* @param string $id The service identifier
540544
* @param int $invalidBehavior The behavior when the service does not exist
541545
*
542-
* @return object The associated service
546+
* @return object|null The associated service
543547
*
544548
* @throws InvalidArgumentException when no definitions are available
545549
* @throws ServiceCircularReferenceException When a circular reference is detected
@@ -554,7 +558,13 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
554558
return parent::get($id);
555559
}
556560

557-
return $this->doGet($id, $invalidBehavior);
561+
$service = $this->doGet($id, $invalidBehavior);
562+
563+
if (!\is_object($service) && null !== $service) {
564+
@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);
565+
}
566+
567+
return $service;
558568
}
559569

560570
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: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ public function testGetAliases()
322322
$builder->register('bar', 'stdClass');
323323
$this->assertFalse($builder->hasAlias('bar'));
324324

325-
$builder->set('foobar', 'stdClass');
326-
$builder->set('moo', 'stdClass');
325+
$builder->set('foobar', new \stdClass());
326+
$builder->set('moo', new \stdClass());
327327
$this->assertCount(2, $builder->getAliases(), '->getAliases() does not return aliased services that have been overridden');
328328
}
329329

@@ -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)
0