Realized in fewer than 160 lines of source.
Well documented, perfect for building/learning.
100% PHPUnit test coverage
One of the fastest PHP dynamic autowired containers available
Cobalt was created to push the performance limits on what a PHP based dynamic autowired DI container can achieve. The Container::class implements the PSR-11 ContainerInterface and provides many of the features found in more notable container projects. Additionally, dependency caching capabilities make the Cobalt container a great choice for performance intensive applications. Cobalt and its simplistic code are perfect for learning or for use within projects or frameworks.
The Cobalt service container has the following features:
- Single class container implementing the PSR-11 ContainerInterface.
- ArrayAccess methods for container bindings.
- Constructor injection of type-hinted dependencies.
- Dependency injection through bind method closures.
- Autowired dependency resolution using Reflection.
- Top down inversion of control (IoC).
- Shared mode option (singleton only).
- Bind existing instances into the container.
- A self-binding global container instance.
composer require jshannon63/cobalt
use Jshannon63\Cobalt\Container;
// create a default container
$app = new Container();
// or, create a singleton only services container
$app = new Container('shared');
Binding does not instantiate the class. Instantiation is deferred until requested from the container. The bind method accepts 3 parameters... the abstract name, the concrete implementation name and a true or false for defining as a singleton. Notice in all three versions we use different abstract names. This is to show that the abstract name is free-form and is used as the "key" for array storage of bindings.
bind($abstract, $concrete=null, $singleton=false)
// a simple binding using only the class name
$app->bind(Foo::class);
// or, bind an interface with a desired concrete implementation.
// can be switched out easily on one place in your code.
$app->bind('FooInterface', Foo::class);
// or, bind an interface or other label to a closure to
// directly control dependency injection.
$app->bind('FooInterface', function(){
return new Foo('123-456-7890');
};
// or, use array access to bind a new instance directly.
$app['Foo'] = new Foo();
$instance = resolve($abstract); (resolve checks for existing binding before instantiating)
$foo = $app->resolve(FooInterface::class);
// or
$foo = $app[FooInterface::class];
// or
$foo = $app->get(FooInterface::class);
Note: Trying to resolve will throw an exception if the requested binding does not exist.
The make method will bind()
then resolve()
to return a fully instantiated binding.
$instance = make($abstract, $concrete, $singleton=false)
$foo = make(FooInterface::class, Foo());
alias($alias, $binding)
Allows creating additional $id string keys for accessing existing container bindings.
alias('myfoo', FooInterface::class);
$instance = instance($abstract, $instance)
$instance = $app->instance('Foo', new Foo);
Note: The instance
method is deprecated but retained for backward compatibility. Instead, use bind($id, $instance)
to register an existing intance.
$bool = has($abstract)
$bool = $app->has('Foo');
$array = getBinding($abstract)
$array = $app->getBinding($abstract);
$array = getBindings()
$array = $app->getBindings();