0 follower

Final Class Yiisoft\Factory\FactoryInternalContainer

InheritanceYiisoft\Factory\FactoryInternalContainer
ImplementsPsr\Container\ContainerInterface

Factory's primary container.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Psr\Container\ContainerInterface|null $container, array<string, mixed> $definitions )
$container \Psr\Container\ContainerInterface|null

Container to use for resolving dependencies.

$definitions array<string, mixed>

Definitions to create objects with.

                public function __construct(
    private ?ContainerInterface $container,
    private array $definitions
) {
}

            
create() public method

public mixed create ( \Yiisoft\Definitions\Contract\DefinitionInterface $definition )
$definition \Yiisoft\Definitions\Contract\DefinitionInterface

                public function create(DefinitionInterface $definition): mixed
{
    if ($definition instanceof ArrayDefinition) {
        $this->creatingIds[$definition->getClass()] = 1;
    }
    try {
        $result = $definition->resolve($this);
        return is_object($result) ? clone $result : $result;
    } finally {
        if ($definition instanceof ArrayDefinition) {
            unset($this->creatingIds[$definition->getClass()]);
        }
    }
}

            
get() public method

public mixed get ( string $id )
$id string

                public function get($id): mixed
{
    if ($this->hasDefinition($id)) {
        return $this->build($id);
    }
    if ($this->container?->has($id)) {
        return $this->container->get($id);
    }
    throw new NotInstantiableClassException($id);
}

            
getDefinition() public method

Get definition by identifier provided.

public \Yiisoft\Definitions\Contract\DefinitionInterface getDefinition ( string $id )
$id string
throws \Yiisoft\Definitions\Exception\InvalidConfigException

                public function getDefinition(string $id): DefinitionInterface
{
    if (!isset($this->definitionInstances[$id])) {
        if (isset($this->definitions[$id])) {
            if (is_object($this->definitions[$id]) && !($this->definitions[$id] instanceof ReferenceInterface)) {
                return Normalizer::normalize($this->definitions[$id], $id);
            }
            if (
                is_string($this->definitions[$id])
                && $this->hasDefinition($this->definitions[$id])
                && $this->definitions[$id] !== $this->definitions[$this->definitions[$id]]
            ) {
                $this->definitionInstances[$id] = $this->getDefinition($this->definitions[$id]);
            } else {
                $this->definitionInstances[$id] =
                    (is_string($this->definitions[$id]) && class_exists($this->definitions[$id]))
                        ? ArrayDefinition::fromPreparedData($this->definitions[$id])
                        : Normalizer::normalize($this->definitions[$id], $id);
            }
        } else {
            throw new LogicException(
                sprintf('No definition found for "%s".', $id)
            );
        }
    }
    return $this->definitionInstances[$id];
}

            
has() public method

public boolean has ( mixed $id )
$id mixed

                public function has($id): bool
{
    return $this->hasDefinition($id) || $this->container?->has($id);
}

            
hasDefinition() public method

Check if there is a definition with a given identifier.

public boolean hasDefinition ( string $id )
$id string

Identifier to look for.

return boolean

If there is a definition with a given identifier.

                public function hasDefinition(string $id): bool
{
    return array_key_exists($id, $this->definitions);
}

            
withDefinitions() public method

public self withDefinitions ( array<string, mixed> $definitions )
$definitions array<string, mixed>

Definitions to create objects with.

                public function withDefinitions(array $definitions): self
{
    $new = clone $this;
    $new->definitions = $definitions;
    $new->definitionInstances = [];
    $new->creatingIds = [];
    return $new;
}