Fuel Dependency package based on League\Container.
The Dependency package is an extension of League\Container responsible for handling dependencies in FuelPHP framework. Most of the functionalities are the same, but there are some custom ones as well. It means that we don't encourage anyone to use this package on it's own since the additions are mostly FuelPHP specific.
This documentation covers the basic usage of the Container, also the added features. For full documentation check the original documentation.
The container is the primary component of the dependency package and ties all the parts together. You can look at this as the (PHP) object store. The container is where you register resources, service providers and retrieve dependencies.
$container = new Fuel\Dependency\Container;
A definition is either a class string name or a closure which returns an instance or a class name.
// Register
$container->add('string', 'stdClass');
// Resolve
$instance = $container->get('string');
// Register
$container->add('closure.object', function() {
return new stdClass;
});
// Resolve
$instance = $container->get('closure.object');
Service providers are used to expose packages to the Container. A Service Provider can provide the container with resources but also act on a namespace. A namespace is a string prefix which maps identifiers to the providers factory method.
use League\Container\ServiceProvider;
class MyProvider extends ServiceProvider
{
protected $provides = ['some.identifier', 'other.resource'];
public function register()
{
$this->container->add('some.identifier', 'stdClass');
$this->container->singleton('other.resource', function() {
return new Something($this->container->resolve('database.connection'));
));
}
}
Fuel adds two main functionalities to the Container:
- Creating multiton instances
- Creating new instances regardless it is singleton or not
// Register
$container->add('closure::object1', function() {
return new stdClass;
});
$container->add('closure::object2', function() {
return new stdClass;
});
// Resolve
object1 = $container->multiton('closure', 'object1');
objects = $container->multiton('closure');
// Register
$container->singleton('closure.object', function() {
return new stdClass;
});
// Resolve
// Always returns a newly resolved definition
$instance = $container->forge('closure.object');
Thank you for considering contribution to FuelPHP framework. Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.