A very simple and convenient lib for working with Dependency Injection Container for PHP
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
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);The following methods are available:
public function set(string $name, $value): void
public function setShared(string $name, $value): void
public function get(string $name): objectWrites 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.
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.
If the component is in the buffer returns from the buffer in the remaining cases creates.
$container->get('koind\storage\CalculatorInterface');Run the following command from you terminal:
phpunit