Skip to content

Runtime container parameters ​

In certain container usage scenarios, it may be necessary to declare a container parameter without knowing its value in advance that is, only the name of the parameter is known at the time of configuration.

The actual value of such a parameter is calculated outside the container and is assigned to it only at runtime, using the pre-defined parameter name.

These types of container parameters are referred to as runtime container parameters.

During container compilation, declared dependency injections must be aware of the existence of runtime container parameters, the values of which will be set in the container only when the container itself is executed.

Injecting a runtime container parameter ​

Both the helper function and the attribute accept a single argument, $name, which represents the name of the container parameter.

TIP

If an empty string is passed to the $name parameter, the container will attempt to automatically determine the container parameter name based on the name of the method (function) parameter or the named argument.

To set the actual value of a runtime container parameter, you must use the set() method of an object that implements the \Kaspi\DiContainer\Interfaces\SourceParametersMutableInterface interface; this object can be retrieved via the container's parameters() method.

php
use Kaspi\DiContainer\DiContainerBuilder;

$container = (new DiContainerBuilder())
    ->build();

// …

// calculate the value of the parameter
$calculatedValue = doCalculateValue();
// we set the actual value of the container parameter
$container->parameters()
    ->set(
        'params.runtime_foo',
        $calculatedValue
    );
// …

Example of inject a runtime container parameter ​

PHP classes:

php
<?php
declare(strict_types=1);

// file: /app/src/Services/Foo.php

namespace App\Services;

use App\Enums\SetupEnum;

final class Foo
{
    public function __construct(
        public readonly Bar $bar,
        public readonly SetupEnum $setupType,
        public readonly string $mailTo,
        public readonly bool $debug,
    ) {}
}
php
<?php
declare(strict_types=1);

// file: /app/src/Services/Bar.php

namespace App\Services;

final class Bar
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Enums/SetupEnum.php

namespace App\Enums;

enum SetupEnum
{
    case LOCAL;
    case STAGES;
    case PROD;
}

Container parameter configuration files:

php
<?php
declare(strict_types=1);

// file: /app/config/params.php

use App\Enums\SetupEnum;

return [
    'emails.admin' => 'admin@example.com',
    'emails.manager' => 'manager@example.com',
    'app.debug' => false,
];
php
<?php
declare(strict_types=1);

// file: /app/config/params_notify.php

return [
    'emails.notify' => '{emails.admin}, {emails.manager}',
];

For the $mailTo and $debug constructor parameters of the PHP class \App\Services\Foo, configure injection using container parameters. Since the actual value for the $setupType parameter can only be determined outside the container, you must use a runtime container parameter to configure its injection.

Declaration in configuration files ​

In the services.php configuration file, register the PHP class \App\Services\Foo. Configure dependency injection for the constructor parameter $setupType using the helper function diParameterRuntime(), and for the constructor parameters $mailTo and $debug—whose values are known in advance—use the helper function diParameter().

php
<?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;
use App\Enums\SetupEnum;


$container = (new DiContainerBuilder())
    ->loadParameters('/app/config/params_main.php')
    ->loadParameters('/app/config/params_notify.php')
    ->load('/app/config/services.php')
    ->build();


// …

$mockSetupTypeValue = static fn () => SetupEnum::LOCAL;

$calculatedSetupTypeValue = ($mockSetupTypeValue)();

$container->parameters()
    ->set(
        'params.setup_type',
        $calculatedSetupTypeValue,
    );

// …

$foo = $container->get(Foo::class);

var_dump($foo->bar);
var_dump($foo->setupType);
var_dump($foo->mailTo);
var_dump($foo->debug);
php
<?php
declare(strict_types=1);

// file: /app/config/services.php

use App\Services\Foo;

use function Kaspi\DiContainer\{diAutowire, diParameterRuntime, diParameter};

return static function (): Generator {
    yield diAutowire(Foo::class)
        ->bindArguments(
            setupType: diParameterRuntime('params.setup_type'),
            mailTo: diParameter('emails.notify'),
            debug: diParameter('app.debug'),
        );
};

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
object(App\Services\Bar)#2 (0) {
}
enum(App\Enums\SetupEnum::LOCAL)
string(37) "admin@example.com, manager@example.com"
bool(false)

Declaration via an attribute ​

Specify the injection for the constructor parameter $setupType using the \Kaspi\DiContainer\Attributes\ParameterRuntime attribute within the PHP class \App\Services\Foo, and use the \Kaspi\DiContainer\Attributes\Parameter attribute for the remaining constructor parameters.

php
<?php
declare(strict_types=1);

// file: /app/src/Services/Foo.php

namespace App\Services;

use App\Enums\SetupEnum;
use Kaspi\DiContainer\Attributes\Parameter;
use Kaspi\DiContainer\Attributes\ParameterRuntime;

final class Foo
{
    public function __construct(
        public readonly Bar $bar,
        #[ParameterRuntime('params.setup_type')]
        public readonly SetupEnum $setupType,
        #[Parameter('emails.notify')]
        public readonly string $mailTo,
        #[Parameter('app.debug')]
        public readonly bool $debug,
    ) {}
}
php
<?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;
use App\Enums\SetupEnum;


$container = (new DiContainerBuilder())
    ->loadParameters('/app/config/params_main.php')
    ->loadParameters('/app/config/params_notify.php')
    ->import(namespace: 'App\\', src: '/app/src')
    ->build();


// …

$mockSetupTypeValue = static fn () => SetupEnum::LOCAL;

$calculatedSetupTypeValue = ($mockSetupTypeValue)();

$container->parameters()
    ->set(
        'params.setup_type',
        $calculatedSetupTypeValue,
    );

// …

$foo = $container->get(Foo::class);

var_dump($foo->bar);
var_dump($foo->setupType);
var_dump($foo->mailTo);
var_dump($foo->debug);

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
object(App\Services\Bar)#2 (0) {
}
enum(App\Enums\SetupEnum::LOCAL)
string(37) "admin@example.com, manager@example.com"
bool(false)