Skip to content
Merged
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
11 changes: 1 addition & 10 deletions src/AggregateContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ final class AggregateContainer implements ContainerInterface, DelegateContainerA
private array $containers = [];
/** @var string[] */
private array $ids = [];
/** @var array<string, mixed> */
private array $cache = [];
/** @var array<string, callable> */
private array $factoryCache = [];
/** @var float[] */
Expand Down Expand Up @@ -58,27 +56,20 @@ public function haveContainer(ContainerInterface $container): bool
*/
public function get(string $id)
{
if (isset($this->cache[$id])) {
return $this->cache[$id];
}

if (!$this->has($id)) {
throw NotFoundException::withIdentifier($id);
}

$factory = $this->factoryCache[$id];
unset($this->factoryCache[$id]);
return $this->cache[$id] = $factory();
return $factory();
}

/**
* @inheritDoc
*/
public function has(string $id): bool
{
if (isset($this->cache[$id])) {
return true;
}
if (isset($this->factoryCache[$id])) {
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Attributes/NoCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Stefna\DependencyInjection\Attributes;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_FUNCTION)]
final class NoCache
{
}
11 changes: 11 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Stefna\DependencyInjection;

use Stefna\DependencyInjection\Attributes\NoCache;
use Stefna\DependencyInjection\Definition\DefinitionSource;
use Stefna\DependencyInjection\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -35,6 +36,16 @@ final public function get(string $id)
}
if (!isset($this->cache[$id])) {
$factory = $this->definition->getDefinition($id);
$reflection = null;
if ($factory instanceof \Closure) {
$reflection = new \ReflectionFunction($factory);
}
elseif (is_object($factory)) {
$reflection = new \ReflectionClass($factory);
}
if ($reflection?->getAttributes(NoCache::class) && $factory) {
return $factory($this->rootContainer, $id);
}
$this->cache[$id] = $factory ? $factory($this->rootContainer, $id) : null;
}
return $this->cache[$id];
Expand Down
53 changes: 53 additions & 0 deletions tests/AggregateContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Psr\Container\ContainerInterface;
use Stefna\DependencyInjection\AggregateContainer;
use Stefna\DependencyInjection\Attributes\NoCache;
use Stefna\DependencyInjection\Container;
use Stefna\DependencyInjection\ContainerBuilder;
use Stefna\DependencyInjection\Definition\DefinitionArray;
use Stefna\DependencyInjection\Exception\DuplicateEntryException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -114,4 +116,55 @@ public function testNested2AggregateContainerGetUsedAsRootContainerWhenCallingFa
$entity = $rootAggregateContainer->get(TestWithArgs::class);
$this->assertSame($time, $entity->date);
}

public function testDefaultCacheService(): void
{
$time = new \DateTimeImmutable();
$rootAggregateContainer = new AggregateContainer('1');
$aggregateContainer1 = new AggregateContainer('2');
$aggregateContainer2 = new AggregateContainer('3');

$aggregateContainer1->addContainer(new Container(new DefinitionArray([
\DateTimeImmutable::class => fn () => $time,
])));
$aggregateContainer2->addContainer(new Container(new DefinitionArray([
TestWithArgs::class => function (ContainerInterface $c) use ($rootAggregateContainer) {
$this->assertSame($rootAggregateContainer, $c);
return new TestWithArgs($c->get(\DateTimeImmutable::class));
},
])));

$rootAggregateContainer->addContainer($aggregateContainer2);
$rootAggregateContainer->addContainer($aggregateContainer1);

$this->assertSame(
$rootAggregateContainer->get(\DateTimeImmutable::class),
$rootAggregateContainer->get(TestWithArgs::class)->date,
);
}

public function testDisableServiceCache(): void
{
$rootAggregateContainer = new AggregateContainer('1');
$aggregateContainer1 = new AggregateContainer('2');
$aggregateContainer2 = new AggregateContainer('3');

$aggregateContainer1->addContainer(new Container(new DefinitionArray([
\DateTimeImmutable::class => #[NoCache] fn () => new \DateTimeImmutable(),
])));
$aggregateContainer2->addContainer(new Container(new DefinitionArray([
TestWithArgs::class => function (ContainerInterface $c) use ($rootAggregateContainer) {
$this->assertSame($rootAggregateContainer, $c);
return new TestWithArgs($c->get(\DateTimeImmutable::class));
},
])));

$rootAggregateContainer->addContainer($aggregateContainer2);
$rootAggregateContainer->addContainer($aggregateContainer1);

$this->assertNotSame(
$rootAggregateContainer->get(\DateTimeImmutable::class),
$rootAggregateContainer->get(TestWithArgs::class)->date,
);
}
}
39 changes: 39 additions & 0 deletions tests/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Stefna\DependencyInjection\Tests;

use Stefna\DependencyInjection\AggregateContainer;
use Stefna\DependencyInjection\Attributes\NoCache;
use Stefna\DependencyInjection\Container;
use Stefna\DependencyInjection\ContainerBuilder;
use Stefna\DependencyInjection\Definition\DefinitionArray;
Expand Down Expand Up @@ -119,4 +120,42 @@ public function testDefinitionOrder(): void
$this->assertSame(4, $container->get('test3'));
$this->assertSame(12, $container->get('test4'));
}

public function testDefaultCacheService(): void
{
$builder = new ContainerBuilder();
$builder->addDefinition([
\DateTimeInterface::class => fn () => new \DateTimeImmutable(),
]);

$container = $builder->build();

$this->assertInstanceOf(Container::class, $container);

$this->assertTrue($container->has(\DateTimeInterface::class));

$this->assertSame(
$container->get(\DateTimeInterface::class),
$container->get(\DateTimeInterface::class)
);
}

public function testDisableServiceCache(): void
{
$builder = new ContainerBuilder();
$builder->addDefinition([
\DateTimeInterface::class => #[NoCache] fn () => new \DateTimeImmutable(),
]);

$container = $builder->build();

$this->assertInstanceOf(Container::class, $container);

$this->assertTrue($container->has(\DateTimeInterface::class));

$this->assertNotSame(
$container->get(\DateTimeInterface::class),
$container->get(\DateTimeInterface::class)
);
}
}