0 follower

Final Class Yiisoft\Injector\Injector

InheritanceYiisoft\Injector\Injector

Injector is able to analyze callable dependencies based on type hinting and inject them from any PSR-11 compatible container.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Injector\Injector
invoke() Invoke a callback with resolving dependencies based on parameter types. Yiisoft\Injector\Injector
make() Creates an object of a given class with resolving constructor dependencies based on parameter types. Yiisoft\Injector\Injector
withCacheReflections() Enable memoization of class reflections for improved performance when resolving the same objects multiple times. Yiisoft\Injector\Injector

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Psr\Container\ContainerInterface|null $container null )
$container \Psr\Container\ContainerInterface|null

                public function __construct(?ContainerInterface $container = null)
{
    $this->container = $container;
}

            
invoke() public method

Invoke a callback with resolving dependencies based on parameter types.

This methods allows invoking a callback and let type hinted parameter names to be resolved as objects of the Container. It additionally allow calling function passing named arguments.

For example, the following callback may be invoked using the Container to resolve the formatter dependency:

$formatString = function($string, \Yiisoft\I18n\MessageFormatterInterface $formatter) {
   // ...
}

$injector = new Yiisoft\Injector\Injector($container);
$injector->invoke($formatString, ['string' => 'Hello World!']);

This will pass the string 'Hello World!' as the first argument, and a formatter instance created by the DI container as the second argument.

public mixed invoke ( callable $callable, array $arguments = [] )
$callable callable

Callable to be invoked.

$arguments array

The array of the function arguments. This can be either a list of arguments, or an associative array where keys are argument names.

return mixed

The callable return value.

throws Yiisoft\Injector\MissingRequiredArgumentException

if required argument is missing.

throws \Psr\Container\ContainerExceptionInterface

if a dependency cannot be resolved or if a dependency cannot be fulfilled.

throws ReflectionException

                public function invoke(callable $callable, array $arguments = [])
{
    $callable = Closure::fromCallable($callable);
    $reflection = new ReflectionFunction($callable);
    return $callable(...$this->resolveDependencies($reflection, $arguments));
}

            
make() public method

Creates an object of a given class with resolving constructor dependencies based on parameter types.

This methods allows invoking a constructor and let type hinted parameter names to be resolved as objects of the Container. It additionally allow calling constructor passing named arguments.

For example, the following constructor may be invoked using the Container to resolve the formatter dependency:

class StringFormatter
{
    public function __construct($string, \Yiisoft\I18n\MessageFormatterInterface $formatter)
    {
        // ...
    }
}

$injector = new Yiisoft\Injector\Injector($container);
$stringFormatter = $injector->make(StringFormatter::class, ['string' => 'Hello World!']);

This will pass the string 'Hello World!' as the first argument, and a formatter instance created by the DI container as the second argument.

public object make ( string $class, array $arguments = [] )
$class string

Name of the class to be created.

$arguments array

The array of the function arguments. This can be either a list of arguments, or an associative array where keys are argument names.

return object

The object of the given class.

throws \Psr\Container\ContainerExceptionInterface
throws Yiisoft\Injector\InvalidArgumentException|Yiisoft\Injector\MissingRequiredArgumentException
throws ReflectionException

                public function make(string $class, array $arguments = []): object
{
    $classReflection = $this->getClassReflection($class);
    if (!$classReflection->isInstantiable()) {
        throw new \InvalidArgumentException("Class $class is not instantiable.");
    }
    $reflection = $classReflection->getConstructor();
    if ($reflection === null) {
        // Method __construct() does not exist
        return new $class();
    }
    return new $class(...$this->resolveDependencies($reflection, $arguments));
}

            
withCacheReflections() public method

Enable memoization of class reflections for improved performance when resolving the same objects multiple times.

Note: Enabling this feature may increase memory usage.

public self withCacheReflections ( boolean $cacheReflections true )
$cacheReflections boolean

                public function withCacheReflections(bool $cacheReflections = true): self
{
    $new = clone $this;
    $new->cacheReflections = $cacheReflections;
    return $new;
}