Final Class Yiisoft\Factory\Factory
| Inheritance | Yiisoft\Factory\Factory |
|---|
Factory allows creating objects passing arguments runtime.
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\Factory | |
| create() | Creates a new object using the given configuration. | Yiisoft\Factory\Factory |
| withDefinitions() | Yiisoft\Factory\Factory |
Method Details
| public mixed __construct ( \Psr\Container\ContainerInterface|null $container = null, array<string, mixed> $definitions = [], boolean $validate = true ) | ||
| $container | \Psr\Container\ContainerInterface|null |
Container to use for resolving dependencies. |
| $definitions | array<string, mixed> |
Definitions to create objects with. |
| $validate | boolean |
If definitions should be validated when set. |
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException | |
|---|---|---|
public function __construct(
?ContainerInterface $container = null,
array $definitions = [],
private bool $validate = true
) {
$this->validateDefinitions($definitions);
$this->internalContainer = new FactoryInternalContainer($container, $definitions);
}
Creates a new object using the given configuration.
You may view this method as an enhanced version of the new operator.
The method supports creating an object based on a class name, a configuration array or
an anonymous function.
Below are some usage examples:
// create an object using a class name
$object = $factory->create(\Yiisoft\Db\Connection::class);
// create an object using a configuration array
$object = $factory->create([
'class' => \Yiisoft\Db\Connection\Connection::class,
'__construct()' => [
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
],
'setUsername()' => ['root'],
'setPassword()' => [''],
'setCharset()' => ['utf8'],
]);
Using Container, this method can also identify dependent objects, instantiate them and inject them into the newly created object.
| public mixed|object create ( mixed $config ) | ||
| $config | mixed |
The object configuration. This can be specified in one of the following forms:
|
| return | mixed|object |
The created object. |
|---|---|---|
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException |
If the configuration is invalid. |
| throws | \Yiisoft\Definitions\Exception\CircularReferenceException | |
| throws | Yiisoft\Factory\NotFoundException | |
| throws | \Yiisoft\Definitions\Exception\NotInstantiableException | |
public function create(mixed $config): mixed
{
if ($this->validate) {
DefinitionValidator::validate($config);
}
if (is_string($config)) {
if ($this->internalContainer->hasDefinition($config)) {
$definition = $this->internalContainer->getDefinition($config);
} elseif (class_exists($config)) {
$definition = ArrayDefinition::fromPreparedData($config);
} else {
throw new NotFoundException($config);
}
} else {
$definition = $this->createDefinition($config);
}
return $this->internalContainer->create($definition);
}
| public self withDefinitions ( array<string, mixed> $definitions ) | ||
| $definitions | array<string, mixed> |
Definitions to create objects with. |
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException | |
|---|---|---|
public function withDefinitions(array $definitions): self
{
$this->validateDefinitions($definitions);
$new = clone $this;
$new->internalContainer = $this->internalContainer->withDefinitions($definitions);
return $new;
}
Signup or Login in order to comment.