Helper function diFactory()
The helper function di Factory() injects a container dependency using the factory pattern[1].
Function signature:
use \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionArgumentsInterface;
\Kaspi\DiContainer\diFactory(
string|array $definition,
?bool $isSingleton = null
): DiDefinitionArgumentsInterfaceParameters:
$definition– a representation of a PHP class and a factory method.$isSingleton– The container will return the same object instance (singleton pattern). If set tonull, the value will be determined based on the container configuration.
The diFactory() function returns an object that provides the following methods:
bindArguments()– to pass arguments to the factory method.
TIP
Reads the documentation section “Using factory methods for dependency injection”.
bindArguments()
Specifying how a factory method should inject dependencies into specific method parameters.
The bindArguments() method for defining parameters is described by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionArgumentsInterface interface.
Method signature:
DiDefinitionArgumentsInterface::bindArguments(
mixed ...$argument
)Parameters:
$argument– arguments.
When specifying how a dependency should be injected into a particular parameter using bindArguments(), you can use named arguments.
If the method does not explicitly specify how a dependency should be injected into a given parameter, the container will handle the injection automatically based on its configuration.
TIP
- The
$argumentparameter inbindArguments()can accept helper functions such asdiGet(),diValue(),diAutowire(), and others. - For injecting scalar-type dependencies, it is recommended to use container parameters.
WARNING
The bindArguments() method overwrites any previously added arguments.
Container identifier for a factory method
To make the result of a factory method available via the container's get() and has() methods, you must specify the corresponding container identifier[2] in the configuration file and declare the factory.
Example of declaring a container identifier for a factory
PHP classes:
<?php
declare(strict_types=1);
//file: /app/src/Services/Foo.php
namespace App\Services;
final class Foo
{
public function __construct(private readonly Bar $bar) {}
public function createValue(string $prefixValue): string
{
return $prefixValue.' '.$this->bar->doSomething();
}
}<?php
declare(strict_types=1);
//file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
// …
public function doSomething(): string
{
return 'dolor sit amet';
}
}The container method get('services.foo_factory') should return the result of executing the factory \App\Services\Foo::createValue().
For the factory the container will automatically inject the dependency into the $bar constructor parameter, based on the configuration.
For the \App\Services\Foo::createValue() method, configure the injection of a scalar type into the $prefixValue parameter using the bindArguments() method.
<?php
declare(strict_types=1);
// file: /app/src/container.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->load('/app/config/factories.php')
->build();
$result = $container->get('services.foo_factory');
var_dump($result);<?php
declare(strict_types=1);
// file: /app/config/factories.php
use App\Services\Foo;
use function Kaspi\DiContainer\diFactory;
return static function(): Generator {
yield 'services.foo_factory' => diFactory([Foo::class, 'createValue'])
->bindArguments(
prefixValue: 'Lorem ipsum'
);
};Run the following command in the terminal:
php /app/src/container.phpA message like the following will appear:
string(26) "Lorem ipsum dolor sit amet"In some scenarios, it becomes necessary—for the purpose of dependency injection—to employ the "factory" design pattern, delegating the creation of a required object to a dedicated PHP class known as a "factory." ↩︎
A container ID is a string name (or a class/interface name) used to register a service or object within a container and to retrieve it via the
get()method. ↩︎