koind / dic
A very simple and convenient lib for working with Dependency Injection Container for PHP.
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^7.2
Requires (Dev)
- phpunit/phpunit: ^7
This package is not auto-updated.
Last update: 2024-09-24 03:38:57 UTC
README
A very simple and convenient lib for working with Dependency Injection Container for PHP
Installation
Run the following command from you terminal:
composer require "koind/dic: ^1.0"
or add this to require section in your composer.json file:
"koind/dic": "^1.0"
then run composer update
Usage
As an example, we will use the library multi-basket
, to start we install it:
composer require "koind/multi-basket: ^1.0"
Now the implementation itself:
<?php use Koind\Container; use koind\storage\SessionStorage; require __DIR__ . '/vendor/autoload.php'; // bootstrap $container = new Container(); $container->set('koind\storage\StorageInterface', function (Container $container) { return new SessionStorage('cart'); }); $container->set('koind\cost\CalculatorInterface', 'koind\cost\SimpleCost'); $container->setShared('cart', 'koind\Cart'); // implementation logic $cart = $container->get('cart'); $cart->add(5, 6, 100);
Available Methods
The following methods are available:
public function set(string $name, $value): void public function setShared(string $name, $value): void public function get(string $name): object
Example usage
Method set()
Writes an anonymous function that returns an instance of the object.
$container->set('koind\storage\StorageInterface', function (Container $container) { return new SessionStorage('cart'); }); // Get component SessionStorage by name: $container->get('koind\storage\StorageInterface');
You can also specify the interface and class that implements this interface:
$container->set('koind\cost\CalculatorInterface', 'koind\cost\SimpleCost'); // Get component SimpleCost by name: $container->get('koind\storage\CalculatorInterface');
Note: when you add a component through the set()
method, each time this component is called by the get()
method, a new instance of this component will be created.
Method setShared()
Writes an anonymous function that returns an instance of the object. The component is created once and placed in the buffer at each request to receive the component, it is taken from the buffer.
$container->setShared('koind\storage\StorageInterface', function (Container $container) { return new SessionStorage('cart'); }); // Get component SessionStorage by name: $container->get('koind\storage\StorageInterface');
You can also specify the interface and class that implements this interface:
$container->setShared('koind\cost\CalculatorInterface', 'koind\cost\SimpleCost'); // Get component SimpleCost by name: $container->get('koind\storage\CalculatorInterface');
Note. When you add a component through the setShared()
method, each time this component is called by the get()
method, only once is created, placed in the buffer, and returned. Subsequent requests for get the component are returned from the buffer.
Method get()
If the component is in the buffer returns from the buffer in the remaining cases creates.
$container->get('koind\storage\CalculatorInterface');
Tests
Run the following command from you terminal:
phpunit