b2pweb/bdf-instantiator

Bdf Instantiator component

v1.1.1 2024-10-09 15:01 UTC

This package is auto-updated.

Last update: 2024-12-03 15:25:36 UTC


README

A basic object instantiator.

build codecov Packagist Version Total Downloads

Install via composer

$ composer require b2pweb/bdf-instantiator

Usage Instruction

Basic usage when resolving from container definition.

use Bdf\Instantiator\Instantiator;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

/** @var ContainerInterface $container */
$container->add(Logger::class, LoggerInterface::class);

$instantiator = new Instantiator($container);
$instantiator->make(LoggerInterface::class);

The instantiator resolve the dependencies of a method based on container definitions.

use Bdf\Instantiator\Instantiator;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class Foo
{
    public $logger;
    
    /**
     * Foo constructor.
     * 
     * @param LoggerInterface $logger  
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
}

/** @var ContainerInterface $container */
$container->add(Logger::class, LoggerInterface::class);

$instantiator = new Instantiator($container);
$foo = $instantiator->make(Foo::class);

var_dump($foo->logger); // Logger

License

Distributed under the terms of the MIT license.