extends | title | group | prev | order |
---|---|---|---|---|
docs |
Theme Service Container |
Architecture |
autoloader |
5 |
The Tonik\Gin\Foundation\Theme
class is a backbone of the theme which performs the services container role. It utilizes a singleton pattern, so every time you initiate it you will receive the same object.
use Tonik\Gin\Foundation\Theme;
$theme = new Theme;
To bind a value inside container simply call bind
or factory
method with a binding name as the first parameter. The second argument should be a closure which returns desired value.
$theme->bind('basic', function() {
return 'value';
});
Inside closure callback, you have access to the two parameters. First is an instance of Tonik\Gin\Foundation\Theme
, second are variables passed on service resolving.
$theme->bind('basic/parameters', function(Theme $theme, $parameters) {
// $theme: class Tonik\Gin\Foundation\Theme
// $parameters: ['key' => 'value']
});
// Resolve service with parameter.
$theme->get('basic/parameters', ['key' => 'value']);
Use bind
method to create singleton service. You will receive the same instance of service every time you resolve it from the container.
use Tonik\Gin\Foundation\Config;
$theme->bind('singleton', function() {
return new Config(['option' => 'value']);
});
The factory
method allows you to create service which returns you a new instance on every resolving.
$theme->factory('service', function () {
return new Config(['option' => 'value']);
});
Run get
method with service name as an argument to resolve previously bounded value out of the container.
$theme->get('service');
You can also resolve services which are requiring some sort of parameter.
$theme->get('basic/parameters', ['key' => 'value']);
You may want to check if service is already bounded inside the container. Use has
method and service name as a parameter.
$theme->has('service');
Call forget
to remove previously bounded service inside the container.
$theme->forget('service');