Skip to content

Commit

Permalink
Feature: Bind a service with tags (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter authored May 26, 2022
2 parents 30ae497 + ab5b99e commit 008e594
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
51 changes: 28 additions & 23 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function alias(string $alias, string $id): void
$this->services[self::ALIASES][$alias] = $id;
}

public function bind(string $abstract, ?string $concrete = null): void
public function bind(string $abstract, ?string $concrete = null, iterable $tags = []): void
{
if ('' === trim($abstract)) {
throw InvalidArgumentException::emptyServiceId();
Expand All @@ -129,6 +129,12 @@ public function bind(string $abstract, ?string $concrete = null): void

$this->services[self::FACTORIES][$abstract] =
static fn (ContainerInterface $container): object => $container->build($concrete ?? $abstract);

if ([] === $tags) {
return;
}

$this->tag($abstract, $tags);
}

public function build(string $class, array $arguments = []): object
Expand Down
4 changes: 3 additions & 1 deletion src/Contract/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ public function alias(string $alias, string $id): void;
/**
* Bind abstract classes or interfaces to concrete implementations.
*
* @param iterable<string> $tags
*
* @throws InvalidArgumentException if $abstract is empty
* @throws LogicException if $abstract is already registered
*/
public function bind(string $abstract, ?string $concrete = null): void;
public function bind(string $abstract, ?string $concrete = null, iterable $tags = []): void;

/**
* Create an object using the given Container to resolve dependencies.
Expand Down
5 changes: 4 additions & 1 deletion tests/Unit/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,19 +513,22 @@ public function testContainerAlias(): void
* @covers \Ghostwriter\Container\Container::getInstance
* @covers \Ghostwriter\Container\Container::has
* @covers \Ghostwriter\Container\Container::resolve
* @covers \Ghostwriter\Container\Container::tag
* @covers \Ghostwriter\Container\Container::tagged
*
* @throws Throwable
*/
public function testContainerBind(): void
{
$this->container->bind(DummyInterface::class, Foo::class);
$this->container->bind(DummyInterface::class, Foo::class, ['taggable']);
$this->container->bind(Baz::class);

self::assertTrue($this->container->has(DummyInterface::class));
self::assertTrue($this->container->has(Baz::class));

self::assertInstanceOf(Foo::class, $this->container->get(DummyInterface::class));
self::assertInstanceOf(Baz::class, $this->container->get(Baz::class));
self::assertCount(1, $this->container->tagged('taggable'));
}

/**
Expand Down

0 comments on commit 008e594

Please sign in to comment.