Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/container/src/GenericContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private function resolveDependency(string $className, string|UnitEnum|null $tag

// Check if the class has been registered as a singleton.
if ($singletonDefinition = $this->singletonDefinitions[$dependencyName] ?? null) {
$this->resolveChain()->add($class);
$this->resolveChain()->add($dependencyName);

try {
$instance = $singletonDefinition instanceof Closure ? $singletonDefinition($this) : $singletonDefinition;
Expand All @@ -408,7 +408,7 @@ private function resolveDependency(string $className, string|UnitEnum|null $tag

// Check if a callable has been registered to resolve this class.
if ($definition = $this->definitions[$dependencyName] ?? null) {
$this->resolveChain()->add(new FunctionReflector($definition));
$this->resolveChain()->add($dependencyName);

try {
return $definition($this);
Expand Down
40 changes: 40 additions & 0 deletions packages/container/tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,46 @@ public function tagged_singleton(): void
$this->assertSame('cli', $container->get(TaggedDependency::class, 'cli')->name);
}

#[Test]
public function tagged_singleton_can_depend_on_unresolved_default_singleton(): void
{
$container = new GenericContainer();
$container->singleton(TaggedDependency::class, fn () => new TaggedDependency('default'));
$container->singleton(
TaggedDependency::class,
fn (GenericContainer $container) => clone $container->get(TaggedDependency::class),
tag: 'copy',
);

$tagged = $container->get(TaggedDependency::class, tag: 'copy');
$default = $container->get(TaggedDependency::class);

$this->assertSame('default', $tagged->name);
$this->assertNotSame($default, $tagged);
$this->assertSame($default, $container->get(TaggedDependency::class));
$this->assertSame($tagged, $container->get(TaggedDependency::class, tag: 'copy'));
}

#[Test]
public function registered_factories_can_share_a_source_location(): void
{
$wrap = fn (callable $factory) => fn (GenericContainer $container) => $factory($container);

$container = new GenericContainer();
$container->register(
ContainerObjectB::class,
$wrap(fn (GenericContainer $container) => new ContainerObjectB($container->get(ContainerObjectA::class))),
);
$container->register(ContainerObjectA::class, $wrap(fn () => new ContainerObjectA()));

$first = $container->get(ContainerObjectB::class);
$second = $container->get(ContainerObjectB::class);

$this->assertInstanceOf(ContainerObjectA::class, $first->a);
$this->assertNotSame($first, $second);
$this->assertNotSame($first->a, $second->a);
}

#[Test]
public function tagged_singleton_with_enum(): void
{
Expand Down
Loading