webino / instance-container
Class Instance Container implementation.
Requires
- php: ~7.1
- psr/container: 1.*
- webino/event-emitter: 1.*
- webino/exceptions: 1.*
Requires (Dev)
- nette/tester: 1.*
- phpstan/phpstan: 0.*
- squizlabs/php_codesniffer: 3.*
- tracy/tracy: 2.*
This package is auto-updated.
Last update: 2024-10-29 05:46:48 UTC
README
Instance Container implementation.
Recommended usage
Instance container should be used in factories to resolve new class instance dependencies. It is not recommended to inject it into objects directly.
Setup
composer require webino\instance-container
Quick Use
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);
API
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.
Development
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
Addendum
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.