Skip to content

Commit

Permalink
Changed: Renamed isShared method to isSingleton method in Container
Browse files Browse the repository at this point in the history
  • Loading branch information
merloxx committed Jul 17, 2023
1 parent 1af16ea commit 084d700
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Container implements ContainerInterface
/**
* @var array<string, mixed>
*/
protected array $instances = [];
protected array $singletons = [];

/**
* @var array<string, string[]>
Expand Down Expand Up @@ -57,11 +57,11 @@ public function __construct(AggregateServiceProviderInterface|null $providers =
/**
* {@inheritdoc}
*/
public function bind(string $alias, Closure|string|null $concrete = null, bool $shared = false): static
public function bind(string $alias, Closure|string|null $concrete = null, bool $singleton = false): static
{
$concrete ??= $alias;

$this->bindings[$alias] = compact('concrete', 'shared');
$this->bindings[$alias] = compact('concrete', 'singleton');

return $this;
}
Expand Down Expand Up @@ -106,8 +106,8 @@ public function get(string $id): mixed
*/
protected function resolve(string $alias): mixed
{
if (isset($this->instances[$alias])) {
return $this->instances[$alias];
if (isset($this->singletons[$alias])) {
return $this->singletons[$alias];
}

if ($this->providers->provides($alias)) {
Expand All @@ -124,8 +124,8 @@ protected function resolve(string $alias): mixed
}
}

if ($this->isShared($alias)) {
$this->instances[$alias] = $object;
if ($this->isSingleton($alias)) {
$this->singletons[$alias] = $object;
}

return $object;
Expand All @@ -142,9 +142,9 @@ public function has(string $id): bool
/**
* {@inheritdoc}
*/
public function isShared(string $alias): bool
public function isSingleton(string $alias): bool
{
return isset($this->bindings[$alias]['shared']) && $this->bindings[$alias]['shared'] === true;
return isset($this->bindings[$alias]['singleton']) && $this->bindings[$alias]['singleton'] === true;
}

/**
Expand Down Expand Up @@ -234,8 +234,8 @@ public function tagged(string $tag): iterable
*/
public function extend(string $alias, Closure $closure): static
{
if (isset($this->instances[$alias])) {
$this->instances[$alias] = $closure($this->instances[$alias], $this);
if (isset($this->singletons[$alias])) {
$this->singletons[$alias] = $closure($this->singletons[$alias], $this);
} else {
$this->extends[$alias][] = $closure;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ interface ContainerInterface extends PsrContainerInterface
/**
* @param string $alias
* @param Closure|string|null $concrete
* @param bool $shared
* @param bool $singleton
*
* @return $this
*/
public function bind(string $alias, Closure|string|null $concrete = null, bool $shared = false): static;
public function bind(string $alias, Closure|string|null $concrete = null, bool $singleton = false): static;

/**
* @param string $alias
Expand All @@ -35,7 +35,7 @@ public function bindSingleton(string $alias, Closure|string|null $concrete = nul
*
* @return bool
*/
public function isShared(string $alias): bool;
public function isSingleton(string $alias): bool;

/**
* @param array<mixed>|string|Closure $callable
Expand Down
16 changes: 8 additions & 8 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,17 @@ public function testHas(): void
}

/* -------------------------------------------------
* IS SHARED
* IS SINGLETON
* -------------------------------------------------
*/

public function testIsShared(): void
public function testIsSingleton(): void
{
$this->container->bind(Foo::class);
$this->container->bind(alias: Bar::class, shared: true);
$this->container->bind(alias: Bar::class, singleton: true);

self::assertFalse($this->container->isShared(Foo::class));
self::assertTrue($this->container->isShared(Bar::class));
self::assertFalse($this->container->isSingleton(Foo::class));
self::assertTrue($this->container->isSingleton(Bar::class));
}

/* -------------------------------------------------
Expand Down Expand Up @@ -481,9 +481,9 @@ public function testExtend(): void
self::assertSame('FooBar', $this->container->get('foo'));
}

public function testExtendSharedBinding(): void
public function testExtendSingletonBinding(): void
{
$this->container->bind('foo', fn() => (object)['bar' => 'Bar'], true);
$this->container->bindSingleton('foo', fn() => (object)['bar' => 'Bar']);
$this->container->extend('foo', static function ($object) {
$object->baz = 'Baz';

Expand Down Expand Up @@ -530,7 +530,7 @@ public function testExtendLazy(): void
self::assertTrue(ExtendLazy::$init);
}

public function testExtendSharedBindingsAfterResolve(): void
public function testExtendSingletonBindingsAfterResolve(): void
{
$this->container->bind('foo', function () {
$object = new stdClass();
Expand Down

0 comments on commit 084d700

Please sign in to comment.