8000 bug #25284 [DI] Cast ids to string, as done on 3.4 (nicolas-grekas, s… · symfony/symfony@86fb66f · GitHub
[go: up one dir, main page]

Skip to content

Commit 86fb66f

Browse files
bug #25284 [DI] Cast ids to string, as done on 3.4 (nicolas-grekas, sroze)
This PR was merged into the 4.0 branch. Discussion ---------- [DI] Cast ids to string, as done on 3.4 | Q | A | ------------- | --- | Branch? | 4.0 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - As reported on slack, we're now missing cast-to-string. This PR puts them explicitly at the places where 3.4 has a call to the removed "normalizeId" method. ![2017-12-03 09-25-38](https://user-images.githubusercontent.com/243674/33524777-29f5c716-d823-11e7-8ca3-806e83595f5a.png) Commits ------- 11c6b38 Ensure services & aliases can be referred to with `__toString`able objects 483dd13 [DI] Cast ids to string, as done on 3.4
2 parents 1dc63f4 + 11c6b38 commit 86fb66f

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ public function getCompiler()
467467
*/
468468
public function set($id, $service)
469469
{
470+
$id = (string) $id;
471+
470472
if ($this->isCompiled() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) {
471473
// setting a synthetic service on a compiled container is alright
472474
throw new BadMethodCallException(sprintf('Setting service "%s" for an unknown or non-synthetic service definition on a compiled container is not allowed.', $id));
@@ -484,7 +486,7 @@ public function set($id, $service)
484486
*/
485487
public function removeDefinition($id)
486488
{
487-
if (isset($this->definitions[$id])) {
489+
if (isset($this->definitions[$id = (string) $id])) {
488490
unset($this->definitions[$id]);
489491
$this->removedIds[$id] = true;
490492
}
@@ -499,6 +501,8 @@ public function removeDefinition($id)
499501
*/
500502
public function has($id)
501503
{
504+
$id = (string) $id;
505+
502506
return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || parent::has($id);
503507
}
504508

@@ -519,7 +523,7 @@ public function has($id)
519523
*/
520524
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
521525
{
522-
if ($this->isCompiled() && isset($this->removedIds[$id]) && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
526+
if ($this->isCompiled() && isset($this->removedIds[$id = (string) $id]) && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
523527
return parent::get($id);
524528
}
525529

@@ -779,6 +783,8 @@ public function setAliases(array $aliases)
779783
*/
780784
public function setAlias($alias, $id)
781785
{
786+
$alias = (string) $alias;
787+
782788
if (is_string($id)) {
783789
$id = new Alias($id);
784790
} elseif (!$id instanceof Alias) {
@@ -801,7 +807,7 @@ public function setAlias($alias, $id)
801807
*/
802808
public function removeAlias($alias)
803809
{
804-
if (isset($this->aliasDefinitions[$alias])) {
810+
if (isset($this->aliasDefinitions[$alias = (string) $alias])) {
805811
unset($this->aliasDefinitions[$alias]);
806812
$this->removedIds[$alias] = true;
807813
}
@@ -816,7 +822,7 @@ public function removeAlias($alias)
816822
*/
817823
public function hasAlias($id)
818824
{
819-
return isset($this->aliasDefinitions[$id]);
825+
return isset($this->aliasDefinitions[$id = (string) $id]);
820826
}
821827

822828
/**
@@ -840,6 +846,8 @@ public function getAliases()
840846
*/
841847
public function getAlias($id)
842848
{
849+
$id = (string) $id;
850+
843851
if (!isset($this->aliasDefinitions[$id])) {
844852
throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
845853
}
@@ -928,6 +936,8 @@ public function setDefinition($id, Definition $definition)
928936
throw new BadMethodCallException('Adding definition to a compiled container is not allowed');
929937
}
930938

939+
$id = (string) $id;
940+
931941
unset($this->aliasDefinitions[$id], $this->removedIds[$id]);
932942

933943
return $this->definitions[$id] = $definition;
@@ -942,7 +952,7 @@ public function setDefinition($id, Definition $definition)
942952
*/
943953
public function hasDefinition($id)
944954
{
945-
return isset($this->definitions[$id]);
955+
return isset($this->definitions[(string) $id]);
946956
}
947957

948958
/**
@@ -956,6 +966,8 @@ public function hasDefinition($id)
956966
*/
957967
public function getDefinition($id)
958968
{
969+
$id = (string) $id;
970+
959971
if (!isset($this->definitions[$id])) {
960972
throw new ServiceNotFoundException($id);
961973
}
@@ -976,6 +988,8 @@ public function getDefinition($id)
976988
*/
977989
public function findDefinition($id)
978990
{
991+
$id = (string) $id;
992+
979993
while (isset($this->aliasDefinitions[$id])) {
980994
$id = (string) $this->aliasDefinitions[$id];
981995
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,24 @@ public function testArgumentsHaveHigherPriorityThanBindings()
13101310
$this->assertSame('via-argument', $container->get('foo')->class1->identifier);
13111311
$this->assertSame('via-bindings', $container->get('foo')->class2->identifier);
13121312
}
1313+
1314+
public function testIdCanBeAnObjectAsLongAsItCanBeCastToString()
1315+
{
1316+
$id = new Reference('another_service');
1317+
$aliasId = new Reference('alias_id');
1318+
1319+
$container = new ContainerBuilder();
1320+
$container->set($id, new \stdClass());
1321+
$container->setAlias($aliasId, 'another_service');
1322+
1323+
$this->assertTrue($container->has('another_service'));
1324+
$this->assertTrue($container->has($id));
1325+
$this->assertTrue($container->hasAlias('alias_id'));
1326+
$this->assertTrue($container->hasAlias($aliasId));
1327+
1328+
$container->removeAlias($aliasId);
1329+
$container->removeDefinition($id);
1330+
}
13131331
}
13141332

13151333
class FooClass

0 commit comments

Comments
 (0)
0