bizley/deep-instantiate

This package is abandoned and no longer maintained. No replacement package was suggested.

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

1.0.0 2021-03-10 18:47 UTC

This package is auto-updated.

Last update: 2022-03-09 08:51:58 UTC


README

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