Container Component
October 10, 2016 ยท View on GitHub
Introduction
Kraken container is both powerful dependency injection container and service container.
Features
Container features:
Concepts
Dependency Injection Container
Dependency injection container is an object that knows how to instantiate and configure objects used throughout application and all of their dependencies.
Service Container
Service container is special case of dependency injection container that treats all of its bindings as abstract structures referred to as "services". Service container allows to create both concrete and virtual services. It is usually used together with set of service providers which it can manage, register, boot, sort and other things.
Service Provider
Service provider is object which main purpose is to provide information how given service should be registered and booted into service container.
Service
Service is an abstract concept that usually referers to an object which is created for a specific purpose and is used throughout your application whenever you need the specific functionality it provides.
Examples
Binding Definition or Interface
$container = new Container();
$foo = new Foo();
$container->bind(FooInterface::class, $foo);
Binding Factory Method
$container = new Container();
$container->bind(FooInterface::class, function() {
return new Foo();
});
Binding Default Params
$container = new Container();
$container->wire(FooBar::class, [
'constructor_option_1',
'constructor_option_2'
]);
Creating Aliases
$container = new Container();
$container->alias(FooInterface::class, Foo::class);
Sharing Objects
$container = new Container();
// this will make Foo a singleton
$container->share(Foo::class);
Auto Wiring
$container = new Container();
// this
$bar = new Bar()
$foo = new Foo($bar);
// can be autoresolved by container using
$foo = $container->make(Foo::class);
Removing Definitions
$container = new Container();
$container->bind(Foo::class, $foo = new Foo());
$container->make(Foo::class); // === $foo
$container->remove(Foo::class);
$container->make(Foo::class); // !== $foo
Writing Service Provider
class CustomProvider extends ServiceProvider implements ServiceProviderInterface
{
protected $requires = [
LoopInterface::class
];
protected $providers = [
WampServerInterface::class
];
protected function register(ContainerInterface $container)
{
$loop = $container->make(LoopInterface::class);
$wamp = new WampServer($loop);
$container->bind(WampServerInterface::class, $wamp);
}
protected function unregister(ContainerInterface $container)
{
$container->remove(WampServerInterface::class);
}
protected function boot(ContainerInterface $container)
{
// Since boot method is fired after everything has been registered, you dont need to put services used here in $requires field.
$runtime = $container->make(RuntimeContainerInterface::class);
$wamp = $container->make(WampServerInterface::class);
$runtime->onStart(function() use($wamp) {
$wamp->start();
});
$runtime->onStop(function() use($wamp) {
$wamp->stop();
});
}
}
Using Service Provider
$container = new Container();
$register = new ServiceRegister($container);
$register->registerProvider(new AProvider());
$register->registerProvider(new BProvider());
$register->boot();
Important Classes & Interfaces
This section contains list of most important classes and interfaces shipped with this component. It does not include all classes and interface.
Container
class Container implements ContainerInterface
This class represents concrete implementation of dependency injection container.
ContainerInterface
interface ContainerInterface extends ContainerReaderInterface, ContainerWriterInterface
ContainerAwareInterface
interface ContainerAwareInterface
Interface for container aware objects. Its main purpose is to provide setter and getter methods for setting and obtaining container instance within objects composition.
ServiceProvider
class ServiceProvider implements ServiceProviderInterface
Abstract service provider which should be extended by each provider used together with service container.
ServiceProviderInterface
interface ServiceProviderInterface
ServiceRegister
class ServiceRegister implements ServiceRegisterInterface
This class uses dependency injection container instance and behaves as service container
ServiceRegisterInterface
interface ServiceRegisterInterface