From 3bfbf45ed2403b18b9c1934f37c4c0557c555f24 Mon Sep 17 00:00:00 2001 From: Diego Saint Esteben Date: Sat, 25 Apr 2015 13:37:49 -0300 Subject: [PATCH] [DependencyInjection] Removed extra strtolower calls --- .../DependencyInjection/ContainerBuilder.php | 14 ++++++++------ .../Tests/ContainerBuilderTest.php | 7 +++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 0d63c0be39e5..3936ec585df6 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -470,7 +470,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id)); } - if (!$this->hasDefinition($id) && isset($this->aliasDefinitions[$id])) { + if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) { return $this->get($this->aliasDefinitions[$id]); } @@ -686,7 +686,7 @@ public function setAlias($alias, $id) throw new InvalidArgumentException('$id must be a string, or an Alias object.'); } - if ($alias === strtolower($id)) { + if ($alias === (string) $id) { throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias)); } @@ -748,7 +748,7 @@ public function getAlias($id) { $id = strtolower($id); - if (!$this->hasAlias($id)) { + if (!isset($this->aliasDefinitions[$id])) { throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id)); } @@ -866,7 +866,7 @@ public function getDefinition($id) { $id = strtolower($id); - if (!$this->hasDefinition($id)) { + if (!array_key_exists($id, $this->definitions)) { throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id)); } @@ -888,8 +888,10 @@ public function getDefinition($id) */ public function findDefinition($id) { - while ($this->hasAlias($id)) { - $id = (string) $this->getAlias($id); + $id = strtolower($id); + + while (isset($this->aliasDefinitions[$id])) { + $id = (string) $this->aliasDefinitions[$id]; } return $this->getDefinition($id); diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index 22f4b5bd3266..2d4eee0a61c2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -189,6 +189,13 @@ public function testAliases() $this->assertTrue($builder->has('bar'), '->setAlias() defines a new service'); $this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one'); + try { + $builder->setAlias('foobar', 'foobar'); + $this->fail('->setAlias() throws an InvalidArgumentException if the alias references itself'); + } catch (\InvalidArgumentException $e) { + $this->assertEquals('An alias can not reference itself, got a circular reference on "foobar".', $e->getMessage(), '->setAlias() throws an InvalidArgumentException if the alias references itself'); + } + try { $builder->getAlias('foobar'); $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');