Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

bizley/yii2-deep-instantiate

Yii 2 Deep Instantiate

Latest Stable Version Total Downloads License Mutation testing badge

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
    ]
]);

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published