Skip to content

Commit

Permalink
Merge branch 'M1ke-preceeding-slash' into 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
philipobenito committed Nov 10, 2024
2 parents e29dbde + 493b6b8 commit dcfc926
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface
*/
public function __construct(string $id, $concrete = null)
{
$id = Util::normalizeAlias($id);

$concrete = $concrete ?? $id;
$this->alias = $id;
$this->concrete = $concrete;
Expand All @@ -85,6 +87,8 @@ public function hasTag(string $tag): bool

public function setAlias(string $id): DefinitionInterface
{
$id = Util::normalizeAlias($id);

$this->alias = $id;
return $this;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Definition/DefinitionAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function addShared(string $id, $definition): DefinitionInterface

public function has(string $id): bool
{
$id = Util::normalizeAlias($id);

foreach ($this->getIterator() as $definition) {
if ($id === $definition->getAlias()) {
return true;
Expand All @@ -65,6 +67,8 @@ public function hasTag(string $tag): bool

public function getDefinition(string $id): DefinitionInterface
{
$id = Util::normalizeAlias($id);

foreach ($this->getIterator() as $definition) {
if ($id === $definition->getAlias()) {
return $definition->setContainer($this->getContainer());
Expand Down
17 changes: 17 additions & 0 deletions src/Definition/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace League\Container\Definition;

class Util
{
public static function normalizeAlias(string $alias): string
{
if (strpos($alias, '\\') === 0) {
return substr($alias, 1);
}

return $alias;
}
}
30 changes: 29 additions & 1 deletion tests/Definition/DefinitionAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace League\Container\Test\Definition;

use League\Container\Container;
use League\Container\Definition\{DefinitionAggregate, DefinitionInterface};
use League\Container\Definition\{Definition, DefinitionAggregate, DefinitionInterface};
use League\Container\Exception\NotFoundException;
use League\Container\Test\Asset\Foo;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -228,4 +228,32 @@ public function testAggregateThrowsExceptionWhenCannotResolve(): void

$aggregate->resolveNew('alias');
}

public function testDefinitionPreceedingSlash(): 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 testGetPreceedingSlash(): 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);
}
}

0 comments on commit dcfc926

Please sign in to comment.