DiRuntime attribute
The \Kaspi\DiContainer\Attributes\DiRuntime attribute declares a PHP class within the container whose actual state will be initialized outside the container, with its value being set only at runtime.
In the context of a dependency container, such a PHP class is called a “Runtime Definition”.
Attribute configuration:
\Attribute::TARGET_CLASS\Attribute::IS_REPEATABLE
Attribute signature:
\Kaspi\DiContainer\Attributes\DiRuntime::__construct(
string $containerIdentifier = '',
?string $message = null,
array|\Kaspi\DiContainer\Attributes\Tag|null $tags = null,
callable|false|string $resetter = false,
)Parameters:
$containerIdentifierOrClass– the container identifier[1] for a "runtime definition".$message– the message to display if an error occurs.$tags– a tag declaration with metadata.$resetter– a value used to reset an object's state.
Container identifier for Runtime Definition
When declaring a PHP class using the \Kaspi\DiContainer\Attributes\DiRuntime attribute, an empty string for the $containerIdentifier parameter is interpreted by the container as an identifier corresponding to the FQCN[2] of the PHP class being declared.
To declare the PHP class \App\Services\FooService via this attribute as a "Runtime Definition," the container identifier must be the string 'App\Services\FooServices'.
<?php
declare(strict_types=1);
// file: /app/src/Services/FooService.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\DiRuntime;
#[DiRuntime]
final class FooService
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\FooService;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$container->has(FooService::class);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
(bool) trueRuntime Definition declaration with multiple identifiers
The \Kaspi\DiContainer\Attributes\DiRuntime attribute can be used to register the same PHP class within the container under different identifiers[1:1].
To register the PHP class \App\Services\FooService as a "Runtime Definition" via this attribute, the container identifier must be specified as the string 'App\Services\FooService'.
To register the PHP class \App\Services\FooService as a "Runtime Definition" via this attribute, the container identifier must be specified as the string 'services.foo_service'.
<?php
declare(strict_types=1);
// file: /app/src/Services/FooService.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\DiRuntime;
#[DiRuntime]
#[DiRuntime('services.foo_service')]
final class FooService
{
// …
}Tags
The $tags parameter of the \Kaspi\DiContainer\Attributes\DiRuntime attribute sets a tag and metadata for the PHP class being declared, using the \Kaspi\DiContainer\Attributes\Tag attribute.
A value of null for the $tags parameter instructs the container to set the tag based on the Tag attribute specified within the PHP class being declared.
A value of [] (an empty array) for the $tags parameter instructs the container not to assign any tag to the PHP class being declared, even if a Tag attribute is present on that class.
<?php
declare(strict_types=1);
namespace App\Services;
use Kaspi\DiContainer\Attributes\DiRuntime;
use Kaspi\DiContainer\Attributes\Tag;
#[DiRuntime(
tags: [
new Tag('tags.fuze'),
new Tag('tags.runtime_service'),
]
)]
final class FooService
{
// …
}