Skip to content

Commit 1612bc8

Browse files
committed
wip
1 parent fcf35f2 commit 1612bc8

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"aws/aws-sdk-php": "^3.338.0",
5151
"azure-oss/storage-blob-flysystem": "^1.2",
5252
"brianium/paratest": "^7.14",
53-
"carthage-software/mago": "1.0.0-beta.28",
53+
"carthage-software/mago": "1.0.0-rc.9",
5454
"depotwarehouse/oauth2-twitch": "^1.3",
5555
"guzzlehttp/psr7": "^2.6.1",
5656
"league/flysystem-aws-s3-v3": "^3.25.1",

packages/container/src/GenericContainer.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,30 @@ public function __clone(): void
635635
$this->chain = $this->chain?->clone();
636636
}
637637

638+
public function deepClone(): self
639+
{
640+
$clonedSingletons = new ArrayIterator();
641+
642+
foreach ($this->singletons as $index => $singleton) {
643+
$clonedSingletons[$index] = is_object($singleton) ? clone $singleton : $singleton;
644+
}
645+
646+
$clonedDefinitions = new ArrayIterator();
647+
648+
foreach ($this->definitions as $index => $definition) {
649+
$clonedDefinitions[$index] = is_object($definition) ? clone $definition : $definition;
650+
}
651+
652+
$clone = clone ($this, [
653+
'singletons' => $clonedSingletons,
654+
'definitions' => $clonedDefinitions,
655+
]);
656+
657+
$clone->singleton(Container::class, $clone);
658+
659+
return $clone;
660+
}
661+
638662
private function resolveTaggedName(string $className, null|string|UnitEnum $tag): string
639663
{
640664
if ($tag instanceof UnitEnum) {

packages/container/tests/ContainerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use ReflectionClass;
9+
use Tempest\Container\Container;
910
use Tempest\Container\Exceptions\CircularDependencyEncountered;
1011
use Tempest\Container\Exceptions\DecoratorDidNotImplementInterface;
1112
use Tempest\Container\Exceptions\DependencyCouldNotBeAutowired;
@@ -39,6 +40,7 @@
3940
use Tempest\Container\Tests\Fixtures\DecoratorInvalid;
4041
use Tempest\Container\Tests\Fixtures\DecoratorSecondClass;
4142
use Tempest\Container\Tests\Fixtures\DecoratorWithoutConstructor;
43+
use Tempest\Container\Tests\Fixtures\DependencyForDeepClone;
4244
use Tempest\Container\Tests\Fixtures\DependencyWithBuiltinDependencies;
4345
use Tempest\Container\Tests\Fixtures\DependencyWithTaggedDependency;
4446
use Tempest\Container\Tests\Fixtures\EnumTag;
@@ -689,4 +691,17 @@ public function test_returns_decorator_without_constructor(): void
689691

690692
$this->assertInstanceOf(DecoratorWithoutConstructor::class, $instance);
691693
}
694+
695+
public function test_deep_clone_container(): void
696+
{
697+
$dependencyA = new DependencyForDeepClone();
698+
$container = new GenericContainer();
699+
$container->singleton(DependencyForDeepClone::class, $dependencyA);
700+
$container->singleton(Container::class, $container);
701+
702+
$clone = $container->deepClone();
703+
704+
$this->assertNotSame($clone->get(Container::class), $container->get(Container::class));
705+
$this->assertNotSame($clone->get(DependencyForDeepClone::class), $container->get(DependencyForDeepClone::class));
706+
}
692707
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Tempest\Container\Tests\Fixtures;
4+
5+
final class DependencyForDeepClone
6+
{
7+
8+
}

0 commit comments

Comments
 (0)