Skip to content

Commit

Permalink
Move Tag definition to Singleton attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed May 23, 2024
1 parent a81e28d commit 6ed309a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Container/GenericContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public function addInitializer(ReflectionClass|string $initializerClass): Contai
$initializeMethod = $initializerClass->getMethod('initialize');

// We resolve the optional Tag attribute from this initializer class
$tag = Attributes::find(Tag::class)->in($initializerClass)->first()
?? Attributes::find(Tag::class)->in($initializeMethod)->first();
$singleton = Attributes::find(Singleton::class)->in($initializerClass)->first()
?? Attributes::find(Singleton::class)->in($initializeMethod)->first();

// For normal Initializers, we'll use the return type
// to determine which dependency they resolve
Expand All @@ -134,7 +134,7 @@ public function addInitializer(ReflectionClass|string $initializerClass): Contai

/** @var ReflectionNamedType[] $returnTypes */
foreach ($returnTypes as $returnType) {
$this->initializers[$this->resolveTaggedName($returnType->getName(), $tag?->name)] = $initializerClass->getName();
$this->initializers[$this->resolveTaggedName($returnType->getName(), $singleton?->tag)] = $initializerClass->getName();
}

return $this;
Expand Down
4 changes: 4 additions & 0 deletions src/Container/Singleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
#[Attribute]
final readonly class Singleton
{
public function __construct(
public ?string $tag = null,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Tempest\Container\Container;
use Tempest\Container\Initializer;
use Tempest\Container\Tag;
use Tempest\Container\Singleton;

#[Tag('cli')]
#[Singleton('cli')]
final readonly class TaggedDependencyCliInitializer implements Initializer
{
public function initialize(Container $container): TaggedDependency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Tempest\Container\Container;
use Tempest\Container\Initializer;
use Tempest\Container\Tag;
use Tempest\Container\Singleton;

#[Tag('web')]
#[Singleton('web')]
final readonly class TaggedDependencyWebInitializer implements Initializer
{
public function initialize(Container $container): TaggedDependency
Expand Down

0 comments on commit 6ed309a

Please sign in to comment.