Final Class Yiisoft\Factory\StrictFactory
| Inheritance | Yiisoft\Factory\StrictFactory |
|---|
Strict factory allows creating objects for specified definitions only.
A factory will try to use a PSR-11 compliant container to get dependencies, but will fall back to manual instantiation if the container cannot provide a required dependency.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Factory\StrictFactory | |
| create() | Creates an object using the definition associated with the provided identifier. | Yiisoft\Factory\StrictFactory |
| has() | Checks if the factory has a definition for the specified identifier. | Yiisoft\Factory\StrictFactory |
Method Details
| public mixed __construct ( array<string, mixed> $definitions, \Psr\Container\ContainerInterface|null $container = null, boolean $validate = true ) | ||
| $definitions | array<string, mixed> |
Definitions to create objects with. |
| $container | \Psr\Container\ContainerInterface|null |
Container to use for resolving dependencies. |
| $validate | boolean |
If definitions should be validated when set. |
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException |
When validation is enabled and definitions are invalid. |
|---|---|---|
public function __construct(
array $definitions,
?ContainerInterface $container = null,
bool $validate = true,
) {
if ($validate) {
foreach ($definitions as $id => $definition) {
DefinitionValidator::validate($definition, $id);
}
}
$this->internalContainer = new FactoryInternalContainer($container, $definitions);
}
Creates an object using the definition associated with the provided identifier.
| public mixed create ( string $id ) | ||
| $id | string |
The identifier of the object to create. |
| return | mixed |
The created object. |
|---|---|---|
| throws | Yiisoft\Factory\NotFoundException |
If no definition is found for the given identifier. |
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException |
If definition configuration is not valid. |
public function create(string $id): mixed
{
if (!$this->internalContainer->hasDefinition($id)) {
throw new NotFoundException($id);
}
return $this->internalContainer->create(
$this->internalContainer->getDefinition($id)
);
}
Checks if the factory has a definition for the specified identifier.
| public boolean has ( string $id ) | ||
| $id | string |
The identifier of the definition to check. |
| return | boolean |
Whether the factory has a definition for the specified identifier. |
|---|---|---|
public function has(string $id): bool
{
return $this->internalContainer->hasDefinition($id);
}
Signup or Login in order to comment.