Arguments for a factory method
When declaring dependency injection or defining a PHP class via a factory class[1], the parameters of the factory class's constructor and methods can be injected automatically based on the container configuration.
For specific scenarios, you can define how dependencies should be injected into particular parameters of a factory class method.
When declaring injection within configuration files, arguments are passed to the bindArguments() method of the diFactory() helper function.
When declaring injection via the \Kaspi\DiContainer\Attributes\DiFactory attribute, arguments are specified using the $arguments parameter.
Example of passing arguments to a PHP class factory method
PHP classes:
<?php
declare(strict_types=1);
//file: /app/src/Services/Foo.php
namespace App\Services;
final class Foo
{
public function __construct(private readonly string $value) {}
}<?php
declare(strict_types=1);
//file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
public function __construct(private readonly Baz $baz) {}
public function createValue(string $prefixValue): string
{
return $prefixValue.' '.$this->baz->doSomething();
}
}<?php
declare(strict_types=1);
//file: /app/src/Services/Baz.php
namespace App\Services;
final class Baz
{
// …
public function doSomething(): string
{
return 'dolor sit amet';
}
}It is necessary to declare the injection into the constructor parameter $value of the PHP class \App\Services\Foo of the value that is calculated by the \App\Services\Bar::createValue() method.
In this example, \App\Services\Bar::createValue() is a “factory” that is delegated the process of creating the required value.
In the constructor parameter $baz of the PHP class \App\Services\Bar the container will inject the dependency on its own, based on the configuration.
For the $prefixValue parameter of the \App\Services\Bar::createValue() factory method, declare a scalar type injection via a container parameter named 'params.prefix_value'.
Declaration in configuration files
In the configuration file services.php we declare the PHP class \App\Services\Foo and inject the dependency into the constructor parameter $value through the helper function diFactory().
For the $prefixValue parameter of the \App\Services\Bar::createValue() factory method, we will declare the implementation of a scalar value through the diParameter() helper function.
<?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/services.php')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->value);<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\{Foo, Bar};
use function Kaspi\DiContainer\{diAutowire, diFactory, diParameter};
return static function(): Generator {
yield diAutowire(Foo::class)
->bindArguments(
value: diFactory(['Bar::class', 'createValue'])
->bindArguments(
prefixValue: diParameter('params.prefix_value')
)
);
};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"Declaration via an attribute
We will declare dependency injection for the constructor parameter $value using the DiFactory attribute within the PHP class \App\Services\Foo.
For the $prefixValue parameter of the factory method \App\Services\Bar::createValue(), we will declare the injection of a scalar value using the PHP class \Kaspi\DiContainer\DiDefinition\DiDefinitionParameter.
You can use classes from other container definitions within the arguments.
Kaspi\DiContainer\DiDefinition\DiDefinitionAutowire– PHP class.Kaspi\DiContainer\DiDefinition\DiDefinitionCallable– injects a dependency by invoking acallabletype.Kaspi\DiContainer\DiDefinition\DiDefinitionFactory– factory for dependency injection.Kaspi\DiContainer\DiDefinition\DiDefinitionGet– dependency injection via a reference to a container identifier.Kaspi\DiContainer\DiDefinition\DiDefinitionParameter– dependency injection from container parameters.Kaspi\DiContainer\DiDefinition\DiDefinitionParameterRutime– dependency injection from runtime container parameters.Kaspi\DiContainer\DiDefinition\DiDefinitionProxyClosure– dependency injection via "lazy loading".Kaspi\DiContainer\DiDefinition\DiDefinitionRuntime– runtime object injection.Kaspi\DiContainer\DiDefinition\DiDefinitionTaggedAs– dependency injection - collection by tag.
PHP classes:
<?php
declare(strict_types=1);
//file: /app/src/Services/Foo.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\DiFactory;
use Kaspi\DiContainer\DiDefinition\DiDefinitionParameter as DiParameter;
final class Foo
{
public function __construct(
#[DiFactory(
['Bar::class', 'createValue'],
arguments: [
'prefixValue' => new DiParameter('params.prefix_value')
],
)]
private readonly string $value
) {}
}<?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())
->import(namespace: 'App\\', src: '/app/src')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->value);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." ↩︎