Fuel Dependency
, (*1)
Fuel Dependency package based on League\Container., (*2)
Usage
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., (*3)
This documentation covers the basic usage of the Container, also the added features. For full documentation check the original documentation., (*4)
Container
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., (*5)
``` php
$container = new Fuel\Dependency\Container;, (*6)
### Definitions
A definition is either a class string name or a closure which returns an instance or a class name.
#### String definition
``` php
// Register
$container->add('string', 'stdClass');
// Resolve
$instance = $container->get('string');
Closure definition
``` php
// Register
$container->add('closure.object', function() {
return new stdClass;
});, (*7)
// Resolve
$instance = $container->get('closure.object');, (*8)
### Service Providers
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.
``` php
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 extensions
Fuel adds two main functionalities to the Container:, (*9)
- Creating multiton instances
- Creating new instances regardless it is singleton or not
Multiton
``` php
// Register
$container->add('closure::object1', function() {
return new stdClass;
});
$container->add('closure::object2', function() {
return new stdClass;
});, (*10)
// Resolve
object1 = $container->multiton('closure', 'object1');
objects = $container->multiton('closure');, (*11)
#### Forge
``` php
// Register
$container->singleton('closure.object', function() {
return new stdClass;
});
// Resolve
// Always returns a newly resolved definition
$instance = $container->forge('closure.object');
Contributing
Thank you for considering contribution to FuelPHP framework. Please see CONTRIBUTING for details., (*12)
License
The MIT License (MIT). Please see License File for more information., (*13)