Skip to content

Commit

Permalink
Merge branch '10.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Jan 6, 2024
2 parents 839889a + dedc3e4 commit bb48c93
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/mako/syringe/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,22 @@ public function hasInstanceOf(string $class): bool
return isset($this->instances[$this->resolveAlias($class)]);
}

/**
* Returns the class names of the instances that have been registered in the container.
*/
public function getInstanceClassNames(): array
{
return array_keys($this->instances);
}

/**
* Removes a class instance from the container.
*/
public function removeInstance(string $class): void
{
unset($this->instances[$this->resolveAlias($class)]);
}

/**
* Returns TRUE if a class has been registered as a singleton and FALSE if not.
*/
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/syringe/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,4 +816,44 @@ public function testImpossibleToResolveDendenciesThatAreNullable(): void

$this->assertNull($object->store);
}

/**
*
*/
public function testGetInstanceClassNames(): void
{
$container = new Container;

$this->assertEmpty($container->getInstanceClassNames());

$container->registerSingleton([stdClass::class, 'foo'], function ($container) {
return new stdClass;
});

$this->assertEmpty($container->getInstanceClassNames());

$container->get('foo');

$this->assertSame([stdClass::class], $container->getInstanceClassNames());
}

/**
*
*/
public function testRemoveInstance(): void
{
$container = new Container;

$container->registerSingleton([stdClass::class, 'foo'], function ($container) {
return new stdClass;
});

$container->get('foo');

$this->assertSame([stdClass::class], $container->getInstanceClassNames());

$container->removeInstance(stdClass::class);

$this->assertEmpty($container->getInstanceClassNames());
}
}

0 comments on commit bb48c93

Please sign in to comment.