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