Skip to content

Commit

Permalink
refactor!: change default behavior for register method
Browse files Browse the repository at this point in the history
`register` method not longer resolves in every call
remove `static` method
add `factory` method to define services that must be resolved on every call
  • Loading branch information
lombervid committed Jul 14, 2023
1 parent 7ecde08 commit f9ec5f2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
28 changes: 14 additions & 14 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class Container implements ContainerInterface
protected array $services = [];

/**
* The container's statics
* The container's factories
*
* @var callable[]
*/
protected array $statics = [];
protected array $factories = [];

public function has(string $id): bool
{
return $this->hasParameter($id) || $this->hasService($id) || $this->hasStatic($id);
return $this->hasParameter($id) || $this->hasService($id) || $this->hasFactory($id);
}

public function get(string $id): mixed
Expand All @@ -43,15 +43,15 @@ public function get(string $id): mixed
}

if ($this->hasService($id)) {
return $this->services[$id]($this);
}

if ($this->hasStatic($id)) {
$this->parameters[$id] = $this->statics[$id]($this);
$this->parameters[$id] = $this->services[$id]($this);

return $this->parameters[$id];
}

if ($this->hasFactory($id)) {
return $this->factories[$id]($this);
}

throw new EntryNotFoundException();
}

Expand All @@ -66,7 +66,7 @@ public function parameter(string $id, mixed $value): void
}

/**
* Register a service that is resolved in every call to `get()` method
* Register a service that is resolved only the first time `get()` is called
*/
public function register(string $id, callable $resolver): void
{
Expand All @@ -76,13 +76,13 @@ public function register(string $id, callable $resolver): void
}

/**
* Register a service that is resolved only the first time `get()` is called
* Register a service that is resolved in every call to `get()` method
*/
public function static(string $id, callable $resolver): void
public function factory(string $id, callable $resolver): void
{
$this->validateIdentifier($id);

$this->statics[$id] = $resolver;
$this->factories[$id] = $resolver;
}

/**
Expand Down Expand Up @@ -116,8 +116,8 @@ public function hasService(string $id): bool
/**
* Returns true is a static service exists under the given identifier.
*/
public function hasStatic(string $id): bool
public function hasFactory(string $id): bool
{
return isset($this->statics[$id]);
return isset($this->factories[$id]);
}
}
68 changes: 34 additions & 34 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public function testHasReturnsTrueOnRegisteredEntry(): void

$container->parameter('foo', fn() => 'bar');
$container->register('foo_service', fn() => 'bar_service');
$container->static('foo_static', fn() => 'bar_static');
$container->factory('foo_factory', fn() => 'bar_factory');

self::assertTrue($container->has('foo'));
self::assertTrue($container->has('foo_service'));
self::assertTrue($container->has('foo_static'));
self::assertTrue($container->has('foo_factory'));
}

public function testResolvesClosureEntry(): void
{
$container = new Container();

$container->register('foo', fn() => 'bar');
$container->factory('foo', fn() => 'bar');

$resolve = $container->get('foo');

Expand All @@ -56,11 +56,26 @@ public function testThrowsExceptionWhenTryingToResolveMissingEntry(): void
$container->get('foo');
}

public function testServicesShouldBeDifferent(): void
public function testServiceShouldBeTheSame(): void
{
$container = new Container();

$container->register('service', fn() => new Service());
$container->register('service', fn () => new Service());

$serviceOne = $container->get('service');
self::assertInstanceOf(Service::class, $serviceOne);

$serviceTwo = $container->get('service');
self::assertInstanceOf(Service::class, $serviceTwo);

self::assertSame($serviceOne, $serviceTwo);
}

public function testFactoriesShouldBeDifferent(): void
{
$container = new Container();

$container->factory('service', fn() => new Service());

$serviceOne = $container->get('service');
self::assertInstanceOf(Service::class, $serviceOne);
Expand All @@ -71,7 +86,7 @@ public function testServicesShouldBeDifferent(): void
self::assertNotSame($serviceOne, $serviceTwo);
}

public function testParameters(): void
public function testSettingParameters(): void
{
$container = new Container();

Expand Down Expand Up @@ -102,68 +117,53 @@ public function testResolvesNullValueParameters(): void
self::assertNull($container->get('foo'));
}

public function testServiceShouldBeTheSame(): void
public function testContainerShouldBeInjectedToServiceResolver(): void
{
$container = new Container();

$container->static('service', fn() => new Service());

$serviceOne = $container->get('service');
self::assertInstanceOf(Service::class, $serviceOne);
$container->parameter('value2', 78);
$container->register('service2', fn(Container $c) => new Service($c->get('value2')));

$serviceTwo = $container->get('service');
self::assertInstanceOf(Service::class, $serviceTwo);
$service2 = $container->get('service2');

self::assertSame($serviceOne, $serviceTwo);
self::assertInstanceOf(Service::class, $service2);
self::assertSame(78, $service2->value);
}

public function testContainerShouldBeInjectedToServiceResolver(): void
public function testContainerShouldBeInjectedToFactoryResolver(): void
{
$container = new Container();

$container->parameter('value', 67);
$container->register('service', fn(Container $c) => new Service($c->get('value')));
$container->factory('service', fn (Container $c) => new Service($c->get('value')));

$service = $container->get('service');

self::assertInstanceOf(Service::class, $service);
self::assertSame(67, $service->value);
}

public function testContainerShouldBeInjectedToStaticResolver(): void
{
$container = new Container();

$container->parameter('value2', 78);
$container->static('service2', fn(Container $c) => new Service($c->get('value2')));

$service2 = $container->get('service2');

self::assertInstanceOf(Service::class, $service2);
self::assertSame(78, $service2->value);
}

public function testExceptionShouldBeThrownRegisteringParameterWithEmptyId(): void
public function testExceptionShouldBeThrownSettingParameterWithEmptyId(): void
{
$container = new Container();

self::expectException(InvalidArgumentException::class);
$container->parameter('', 'empty');
}

public function testExceptionShouldBeThrownRegisteringServiceWithEmptyId(): void
public function testExceptionShouldBeThrownSettingServiceWithEmptyId(): void
{
$container = new Container();

self::expectException(InvalidArgumentException::class);
$container->register('', fn() => new Service());
$container->register('', fn () => new Service());
}

public function testExceptionShouldBeThrownRegisteringStaticWithEmptyId(): void
public function testExceptionShouldBeThrownSettingFactoryWithEmptyId(): void
{
$container = new Container();

self::expectException(InvalidArgumentException::class);
$container->static('', fn () => new Service());
$container->factory('', fn() => new Service());
}
}

0 comments on commit f9ec5f2

Please sign in to comment.