Declaring dependency injection via a factory
Injection into a parameter of a method or a callable type[1] can be declared via a PHP factory class.
Parameters for the constructor and methods of a PHP factory class can be injected automatically based on the container configuration. For specific scenarios, you can declare arguments for the factory method.
Injection into parameters can be declared:
- via the
diFactory()helper function in configuration files. - via the
\Kaspi\DiContainer\Attributes\DiFactoryattribute.
Important note: the factory method[2] must return a result that is compatible with the parameter type[3] of the method or function.
Example of declaring dependency injection via a factory
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
final class Foo
{
public function __construct(
public readonly Qux $qux
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Qux.php
namespace App\Services;
final class Qux
{
public function __construct(public readonly string $value) {}
}<?php
declare(strict_types=1);
// file: /app/src/Helpers/Configurator.php
namespace App\Helpers;
use App\Services\Bar;
final class Configurator
{
public function __construct(public readonly Bar $bar) {}
public function doConfigQux(): Qux
{
$calculatedValue = $this->bar->calculateSomething();
return '' === $calculatedValue
? new Qux('Lorem ipsum')
: new Qux($calculatedValue);
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
public function __construct(
public readonly Baz $baz
) {}
public function calculateSomething(): string
{
if (0 < $this->baz->calculateSomething()) {
return 'Fuuz';
}
return 'Ok';
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Services;
use function rand;
final class Baz
{
// …
public function calculateSomething(): int
{
// here are some calculations
return rand(-100, 100); // test data
}
}For the PHP class \App\Services\Foo, it is necessary to declare a dependency injection into the $qux parameter via the factory \App\Helpers\Configurator::quxDoConfig().
Declaration in configuration files
In the services.php configuration file, we will register the PHP class \App\Services\Foo in the container and configure dependency injection for the constructor parameter $qux using the diFactory() helper function.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->load('/app/config/services.php')
->build();
var_dump($container->get(Foo::class)->qux->value);<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Service\Foo;
use App\Helpers\Configurator;
use function Kaspi\DiContainer\{diAutowire, diFactory};
return static function (): Generator {
yield diAutowire(Foo::class)
->bindArguments(
qux: diFactory([Configurator::class, 'doConfigQux'])
);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string(4) "Fuuz"Declaration via an attribute
Declare the injection of a dependency into the $qux constructor parameter of the PHP class \App\Services\Foo using the \Kaspi\DiContainer\Attributes\DiFactory attribute.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\DiFactory;
use App\Helpers\Configurator;
final class Foo
{
public function __construct(
#[DiFactory([Configurator::class, 'doConfigQux'])]
public readonly Qux $qux
) {}
}<?php
declare(strict_types=1);
// file: /app/src/container_build.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();
var_dump($container->get(Foo::class)->qux->value);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string(4) "Fuuz"The
callabletype represents values or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎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." ↩︎
Type hints – the specification of an expected data type for method parameters, such as the name of a specific class or interface. ↩︎