Instance Container implementation.
Instance container should be used in factories to resolve new class instance dependencies. It is not recommended to inject it into objects directly.
composer require webino\instance-container
Getting same class instance:
use Webino\InstanceContainer;
class TestInstance
{
}
$instances = new InstanceContainer;
$testInstance = $instances->get(TestInstance::class);
Creating new class instance:
use Webino\InstanceContainer;
class TestInstance
{
public function __construct(stdClass $dependency)
{
}
}
$instances = new InstanceContainer;
$testInstance = $instances->make(TestInstance::class, new stdClass);
Getting class instance with a factory method:
use Webino\InstanceContainer;
use Webino\CreateInstanceEventInterface;
use Webino\InstanceFactoryMethodInterface;
class TestInstance implements InstanceFactoryMethodInterface
{
public static function create(CreateInstanceEventInterface $event)
{
$container = $event->getContainer();
return new static($container->get(stdClass::class));
}
public function __construct(stdClass $dependency)
{
}
}
$instances = new InstanceContainer;
$testInstance = $instances->get(TestInstance::class);
Setting class instance:
use Webino\InstanceContainer;
class TestInstance
{
}
$instances = new InstanceContainer;
$instances->set(TestInstance::class, new TestInstance);
Unsetting class instance:
use Webino\InstanceContainer;
class TestInstance
{
}
$instances = new InstanceContainer;
$instances->set(TestInstance::class, null);
Binding factory class:
use Webino\InstanceContainer;
use Webino\CreateInstanceEventInterface;
class TestInstanceFactory implements InstanceFactoryInterface
{
public function createInstance(CreateInstanceEventInterface $event)
{
$container = $event->getContainer();
return new TestInstance($container->get(stdClass::class));
}
}
class TestInstance
{
public function __construct(stdClass $dependency)
{
}
}
$instances = new InstanceContainer;
$instances->bind(TestInstance::class, TestInstanceFactory::class);
$testInstance = $instances->get(TestInstance::class);
Binding callback factory:
use Webino\InstanceContainer;
use Webino\CreateInstanceEventInterface;
class TestInstance
{
public function __construct(stdClass $dependency)
{
}
}
$instances = new InstanceContainer;
$instances->bind(TestInstance::class, function (CreateInstanceEventInterface $event) {
$container = $event->getContainer();
return new TestInstance($container->get(stdClass::class));
});
$testInstance = $instances->get(TestInstance::class);
Getting interface default class instance:
use Webino\InstanceContainer;
interface TestInstanceInterface
{
}
class TestInstance implements TestInstanceInterface
{
public function __construct(stdClass $dependency)
{
}
}
$instances = new InstanceContainer;
$testInstance = $instances->get(TestInstanceInterface::class);
Setting alias interface to a class:
use Webino\InstanceContainer;
interface TestAliasInterface
{
}
class TestInstance implements TestAliasInterface
{
public function __construct(stdClass $dependency)
{
}
}
$instances = new InstanceContainer;
$instances->alias(TestInstance::class, TestAliasInterface::class);
$testInstance = $instances->get(TestAliasInterface::class);
Instance creation event handling:
use Webino\InstanceContainer;
use Webino\CreateInstanceEvent;
class TestInstance
{
public $foo = false;
}
$instances = new InstanceContainer;
$instances->on(CreateInstanceEvent::class, function (CreateInstanceEvent $event) {
$instance = $event->getInstance();
$instance->foo = true;
});
$testInstance = $instances->get(TestAliasInterface::class);
InstanceContainer
-
bool has(string $id)
Returns true if the container can return an instance for the given identifier, false otherwise. -
mixed get(string $id)
Returns entry instance of the container by its identifier. -
void set(string $id, mixed $instance)
Set entry instance. -
mixed make(string $id, array<int, mixed> ...$parameter)
Creates new instance. -
void bind(string $id, mixed $binding)
Bind provider to an entry instance. -
void alias(string $id, string $alias)
Set alias to an entry instance.
CreateInstanceEvent
-
InstanceContainerInterface getContainer()
Returns instance container. -
string getClass()
Returns instance class. -
array getParameters()
Returns instance creation parameters.
InstanceFactory
- mixed createInstance(CreateInstanceEventInterface $event)
Creates new instance.
InstanceFactoryMethod
- mixed static create(CreateInstanceEventInterface $event)
Creates new instance.
Static analysis:
composer analyse
Coding style check:
composer check
Coding style fix:
composer fix
Testing:
composer test
Git pre-commit setup:
ln -s ../../pre-commit .git/hooks/pre-commit
Please, if you are interested in this library report any issues and don't hesitate to contribute. We will appreciate any contributions on development of this library.