gdianov / opium
Simple dependency injection container
v1.0.0
2020-07-05 12:18 UTC
Requires
- psr/container: ^1.0
- symfony/yaml: ^5.1
Requires (Dev)
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2025-05-05 23:59:44 UTC
README
How to usage?
Install:
composer require gdianov/opium
-
Create your yaml config like config.yaml:
t: class: gdianov\opium\tests\classes\T constructor: - 1 props: - bar: barValue c: class: gdianov\opium\tests\classes\C constructor: - '@t' p: class: gdianov\opium\tests\classes\P constructor: - opium props: - c: '@c'
-
Create Opium instance like this:
$configFile = __DIR__ . '/config.yaml'; $loader = new YamlLoader($configFile); $config = $loader->configure(); $opium = Opium::getInstance(new Container(), $config);
-
Use it.
Create objects by yaml configuration:
//$t is instance of: gdianov\opium\tests\classes\T $t = $opium->make('t'); //$c is instance of: gdianov\opium\tests\classes\C with //injected object $t by constructor $c = $opium->make('c'); //$p is instance of: gdianov\opium\tests\classes\P with //injected object $c by property and string by constructor $p = $opium->make('p'); //You can injected dependency by property and constructor.
Also we can create new object dynamically:
$t = $opium->makeDynamic([ 'class' => T::class, 'props' => [ [ 'bar' => $barValue ] ]);
Create dynamically and related with yaml config dependency:
$c = $opium->makeDynamic([ 'class' => C::class, 'constructor' => ['@t'] ]); //New C instance with T dependency
You can get new instance with another params:
$t = $opium->getWithParams('t', [ 'props' => [ ['bar' => 'Another Value'], ] ]); //Instance T with new property bar value
You can combine objects as you like without restricting yourself to anything. Try it.