From b4dbbf027d9943aa71c436c46b5ef149048dbd1e Mon Sep 17 00:00:00 2001 From: Phil Bennett Date: Sun, 10 Nov 2024 12:39:55 +0000 Subject: [PATCH] Review changes from PR --- src/Definition/Definition.php | 13 ++++++- src/Definition/DefinitionAggregate.php | 4 +- src/Definition/Util.php | 17 --------- tests/Definition/DefinitionAggregateTest.php | 40 +++++++++++++++++--- 4 files changed, 47 insertions(+), 27 deletions(-) delete mode 100644 src/Definition/Util.php diff --git a/src/Definition/Definition.php b/src/Definition/Definition.php index 28e72da..ec1fca3 100644 --- a/src/Definition/Definition.php +++ b/src/Definition/Definition.php @@ -67,7 +67,7 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface */ public function __construct(string $id, $concrete = null) { - $id = Util::normalizeAlias($id); + $id = static::normaliseAlias($id); $concrete = $concrete ?? $id; $this->alias = $id; @@ -87,7 +87,7 @@ public function hasTag(string $tag): bool public function setAlias(string $id): DefinitionInterface { - $id = Util::normalizeAlias($id); + $id = static::normaliseAlias($id); $this->alias = $id; return $this; @@ -239,4 +239,13 @@ protected function invokeMethods(object $instance): object return $instance; } + + public static function normaliseAlias(string $alias): string + { + if (strpos($alias, '\\') === 0) { + return substr($alias, 1); + } + + return $alias; + } } diff --git a/src/Definition/DefinitionAggregate.php b/src/Definition/DefinitionAggregate.php index ba51060..d341354 100644 --- a/src/Definition/DefinitionAggregate.php +++ b/src/Definition/DefinitionAggregate.php @@ -43,7 +43,7 @@ public function addShared(string $id, $definition): DefinitionInterface public function has(string $id): bool { - $id = Util::normalizeAlias($id); + $id = Definition::normaliseAlias($id); foreach ($this->getIterator() as $definition) { if ($id === $definition->getAlias()) { @@ -67,7 +67,7 @@ public function hasTag(string $tag): bool public function getDefinition(string $id): DefinitionInterface { - $id = Util::normalizeAlias($id); + $id = Definition::normaliseAlias($id); foreach ($this->getIterator() as $definition) { if ($id === $definition->getAlias()) { diff --git a/src/Definition/Util.php b/src/Definition/Util.php deleted file mode 100644 index a289d5b..0000000 --- a/src/Definition/Util.php +++ /dev/null @@ -1,17 +0,0 @@ -resolveNew('alias'); } - public function testDefinitionPreceedingSlash(): void + public function testDefinitionPrecedingSlash(): void { - $container = $this->getMockBuilder(Container::class)->getMock(); - $aggregate = new DefinitionAggregate(); + $container = $this->getMockBuilder(Container::class)->getMock(); + $aggregate = new DefinitionAggregate(); $aggregate->setContainer($container); $some_class = "\\League\\Container\\Test\\Asset\\Foo"; @@ -243,10 +243,10 @@ public function testDefinitionPreceedingSlash(): void self::assertInstanceOf(Definition::class, $definition); } - public function testGetPreceedingSlash(): void + public function testGetPrecedingSlash(): void { - $container = $this->getMockBuilder(Container::class)->getMock(); - $aggregate = new DefinitionAggregate(); + $container = $this->getMockBuilder(Container::class)->getMock(); + $aggregate = new DefinitionAggregate(); $aggregate->setContainer($container); $some_class = Foo::class; @@ -256,4 +256,32 @@ public function testGetPreceedingSlash(): void self::assertInstanceOf(Definition::class, $definition); } + + public function testDefinitionPrecedingSlashSingularQuotes(): void + { + $container = $this->getMockBuilder(Container::class)->getMock(); + $aggregate = new DefinitionAggregate(); + $aggregate->setContainer($container); + + $some_class = '\\League\\Container\\Test\\Asset\\Foo'; + $aggregate->add($some_class, null); + + $definition = $aggregate->getDefinition(Foo::class); + + self::assertInstanceOf(Definition::class, $definition); + } + + public function testGetPrecedingSlashSingularQuote(): void + { + $container = $this->getMockBuilder(Container::class)->getMock(); + $aggregate = new DefinitionAggregate(); + $aggregate->setContainer($container); + + $some_class = Foo::class; + $aggregate->add($some_class, null); + + $definition = $aggregate->getDefinition('\\League\\Container\\Test\\Asset\\Foo'); + + self::assertInstanceOf(Definition::class, $definition); + } }