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
6 changes: 6 additions & 0 deletions src/Attributes/BindWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public function __construct(
public string|array $concrete,
public BindType $type = BindType::Transient,
public ?string $tag = null,
) {}

/**
Expand All @@ -29,4 +30,9 @@ public function getType(): BindType
{
return $this->type;
}

public function getTag(): ?string
{
return $this->tag;
}
}
15 changes: 15 additions & 0 deletions src/Contexts/BindContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
private string $abstract,
private array $concrete,
private BindType $type,
private ?string $tag = null,
) {}

/**
Expand Down Expand Up @@ -47,4 +48,18 @@ public function isSingleton(): bool
{
return $this->type === BindType::Singleton;
}

public function getTag(): string
{
if ($this->tag === null) {
return $this->abstract.'_tag';
}

return $this->tag;
}

public function hasTag(): bool
{
return $this->tag !== null;
}
}
4 changes: 2 additions & 2 deletions src/Providers/BindifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ private function beforeResolvingCallback(): Closure
);
}

if (count($binding->getConcrete()) > 1) {
if ($binding->hasTag() || count($binding->getConcrete()) > 1) {
$container->tag(
$binding->getConcrete(),
$binding->getAbstract().'_tag',
$binding->getTag(),
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/Resolvers/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public function resolve(string $abstract): ?BindContext
return null;
}

return new BindContext($abstract, $concretes, $bindWith->getType());
return new BindContext($abstract, $concretes, $bindWith->getType(), $bindWith->getTag());
}
}
24 changes: 6 additions & 18 deletions tests/Feature/BindifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,13 @@
it('return null when invalid classes binding', function () {
$this->assertNull($this->attributeResolver->resolve(InvalidClassBindingsContract::class));
});

it('binds multiple valid services to the same interface', function () {
/** @var BindContext $binding */
$binding = $this->attributeResolver->resolve(MultiBindContract::class);
app(MultiBindContract::class);

$expected = BindContext::create(
MultiBindContract::class,
[
DefaultImplementationService::class,
AlternativeImplementationService::class,
],
BindType::Transient
);
$instances = $this->app->tagged('custom_tag');

expect($expected)
->toEqual($binding)
->and($binding->getConcrete())
->toBeArray()
->toHaveCount(2)
->and($binding->getConcrete())
->toEqual([DefaultImplementationService::class, AlternativeImplementationService::class]);
foreach ($instances as $instance) {
expect(get_class($instance))
->toBeIn([DefaultImplementationService::class, AlternativeImplementationService::class]);
}
});
2 changes: 1 addition & 1 deletion workbench/app/Contracts/MultiBindContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
#[BindWith([
DefaultImplementationService::class,
AlternativeImplementationService::class,
], BindType::Transient)]
], BindType::Transient, tag: 'custom_tag')]
interface MultiBindContract {}