bizley / deep-instantiate
Deep Instantiator for Yii 2.
Fund package maintenance!
bizley
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.0
- yiisoft/yii2: ^2.0.39.3
Requires (Dev)
README
This package provides Yii 2 Dependency Injector Container capable of automatically resolving nested constructor interface-typed dependencies.
Requirements
- Yii 2.0.39.3+
- PHP 7.0+
Installation
composer require bizley/deep-instantiate:^1.0
Usage
Directly - just call new \Bizley\DeepInstantiate\Container()
.
Globally - set \Yii::$container = new \Bizley\DeepInstantiate\Container();
in your entry script.
Enhanced Instantiating
class Alpha implements AlphaInterface { private $beta; public function __construct(BetaInterface $beta) { $this->beta = $beta; } } class Beta implements BetaInterface { } class Gamma { private $alpha; public function __construct(AlphaInterface $alpha) { $this->alpha = $alpha; } }
With the original Container:
$container->set(BetaInterface::class, Beta::class); $alpha = $container->get(Alpha::class);
With Deep Instantiate Container you just need:
$alpha = $container->get(Alpha::class, [Beta::class]);
For nested dependencies with the original Container:
$container->set(AlphaInterface::class, Alpha::class); $container->set(BetaInterface::class, Beta::class); $gamma = $container->get(Gamma::class);
For nested dependencies with Deep Instantiate Container:
$gamma = $container->get(Gamma::class, [ [ 'class' => Alpha::class, '__construct()' => Beta::class ] ]);