Kraken container is both powerful dependency injection container and service container.
Container features:
Dependency injection container is an object that knows how to instantiate and configure objects used throughout application and all of their dependencies.
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 is object which main purpose is to provide information how given service should be registered and booted into service container.
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.
$container = new Container();
$foo = new Foo();
$container->bind(FooInterface::class, $foo);
$container = new Container();
$container->bind(FooInterface::class, function() {
return new Foo();
});
$container = new Container();
$container->wire(FooBar::class, [
'constructor_option_1',
'constructor_option_2'
]);
$container = new Container();
$container->alias(FooInterface::class, Foo::class);
$container = new Container();
// this will make Foo a singleton
$container->share(Foo::class);
$container = new Container();
// this
$bar = new Bar()
$foo = new Foo($bar);
// can be autoresolved by container using
$foo = $container->make(Foo::class);
$container = new Container();
$container->bind(Foo::class, $foo = new Foo());
$container->make(Foo::class); // === $foo
$container->remove(Foo::class);
$container->make(Foo::class); // !== $foo
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();
});
}
}
$container = new Container();
$register = new ServiceRegister($container);
$register->registerProvider(new AProvider());
$register->registerProvider(new BProvider());
$register->boot();
This section contains list of most important classes and interfaces shipped with this component. It does not include all classes and interface.
class Container implements ContainerInterface
This class represents concrete implementation of dependency injection container.
interface ContainerInterface extends ContainerReaderInterface, ContainerWriterInterface
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.
class ServiceProvider implements ServiceProviderInterface
Abstract service provider which should be extended by each provider used together with service container.
interface ServiceProviderInterface
class ServiceRegister implements ServiceRegisterInterface
This class uses dependency injection container instance and behaves as service container
interface ServiceRegisterInterface