diff --git a/src/Container/GenericContainer.php b/src/Container/GenericContainer.php index d44eb35..8031963 100644 --- a/src/Container/GenericContainer.php +++ b/src/Container/GenericContainer.php @@ -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 @@ -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; diff --git a/src/Container/Singleton.php b/src/Container/Singleton.php index fa96ec3..83da622 100644 --- a/src/Container/Singleton.php +++ b/src/Container/Singleton.php @@ -9,4 +9,8 @@ #[Attribute] final readonly class Singleton { + public function __construct( + public ?string $tag = null, + ) { + } } diff --git a/tests/Unit/Container/Fixtures/TaggedDependencyCliInitializer.php b/tests/Unit/Container/Fixtures/TaggedDependencyCliInitializer.php index 572e74d..eed449b 100644 --- a/tests/Unit/Container/Fixtures/TaggedDependencyCliInitializer.php +++ b/tests/Unit/Container/Fixtures/TaggedDependencyCliInitializer.php @@ -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 diff --git a/tests/Unit/Container/Fixtures/TaggedDependencyWebInitializer.php b/tests/Unit/Container/Fixtures/TaggedDependencyWebInitializer.php index db3ee7a..7dc7aec 100644 --- a/tests/Unit/Container/Fixtures/TaggedDependencyWebInitializer.php +++ b/tests/Unit/Container/Fixtures/TaggedDependencyWebInitializer.php @@ -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