InjectByCallable attribute
The \Kaspi\DiContainer\Attributes\InjectByCallable attribute injects a dependency by invoking the specified callable type[1].
Attribute configuration:
\Attribute::TARGET_PARAMETER\Attribute::IS_REPEATABLE
Attribute signature:
\Kaspi\DiContainer\Attributes\InjectByCallable::__construct(
callable $callable
)Parameters:
$callable– a callable type.
Parameters in the callable type can be automatically injected by the container, based on the container's configuration.
Example of implementation via a callable type
PHP classes and functions:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
class Foo
{
public function __construct(
public readonly Bar $bar,
public readonly string $calcStr,
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
public function __construct(public readonly string $val) {}
}<?php
declare(strict_types=1);
// file: /app/src/Helpers/Configurator.php
namespace App\Helpers;
use App\Services\Bar;
final class Configurator
{
// …
public static function doConfigBar(string $inputStr): Bar {
$bar = new Bar($inputStr);
// …
return $bar;
}
}<?php
declare(strict_types=1);
// file /app/src/functions.php
namespace App;
use function {bin2hex, random_bytes};
if (!function_exists('App\calculate_str')) {
function calculate_str(): string {
$str = bin2hex(random_bytes(5));
// …
return $str;
}
}For the PHP class \App\Services\Foo, it is necessary to declare dependency injection for the constructor parameters $bar and $calcStr, both of which require complex calculations.
For the $bar parameter of the \App\Services\Foo class, it is necessary to use the \App\Helpers\Configurator::doConfigBar() method, which performs the calculations required to initialize the \App\Services\Bar PHP class. Since a static method of a PHP class is a callable type[1:1], we use the \Kaspi\DiContainer\Attributes\InjectByCallable attribute to inject the dependency into the $bar constructor parameter.
For the second parameter, $calcStr, of the \App\Services\Foo class, you need to inject a value calculated by the \App\calculate_str() function; this injection should be declared using the \Kaspi\DiContainer\Attributes\InjectByCallable attribute.
The \App\Helpers\Configurator::doConfigBar() method requires a scalar dependency for its $inputStr parameter; to declare this dependency injection, use the \Kaspi\DiContainer\Attributes\Parameter attribute.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\InjectByCallable;
use App\Helpers\Configurator;
class Foo
{
public function __construct(
#[InjectByCallable([Configurator::class, 'doConfigBar'])]
public readonly Bar $bar,
#[InjectByCallable('App\calculate_str')]
public readonly string $calcStr,
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Helpers/Configurator.php
namespace App\Helpers;
use Kaspi\DiContainer\Attributes\Parameter;
use App\Services\Bar;
final class Configurator
{
// …
public static function doConfigBar(
#[Parameter('params.lorem')]
string $inputStr
): Bar {
$bar = new Bar($inputStr);
// …
return $bar;
}
}To inject scalar dependencies, the container requires a container parameter named 'params.lorem'; we will define this using the setParameter() method.
<?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, Bar};
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->setParameter('params.lorem', 'Lorem ipsum')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->bar->val);
var_dump($foo->calcStr);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string(11) "Lorem ipsum"
string(10) "121e767853"The
callabletype represents values or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎ ↩︎