Registration of container parameters
The configuration of container parameters can be represented as a set of key-value pairs, where the key is the string name of the parameter, and the value is one of the supported types.
IMPORTANT
Any parameters previously added to the configuration can be replaced with new values, provided the parameter name matches.
Example
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/ReportMaker.php
namespace App\Services;
use App\Storages\ReportStorage;
final class ReportMaker
{
public function __construct(
public readonly ReportStorage $storage,
public readonly int $lastDays,
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Storages/ReportStorage.php
namespace App\Storages;
final class ReportStorage
{
public function __construct(
public readonly string $dir
) {}
}In the report_parameters.php configuration file, specify scalar values for the container parameters:
'params.report_output_dir'— the directory for saving reports.'params.last_days'— the number of recent days to be taken into account when generating a report.
The report_services_with_params.php configuration file defines the PHP classes to which these scalar values are passed via container parameters.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\ReportMaker;
$container = (new DiContainerBuilder())
->load('/app/config/report_services_with_params.php')
->loadParameters('/app/config/report_parameters.php')
->build();
$service = $container->get(ReportMaker::class);
var_dump($service->storage->dir);
var_dump($service->lastDays);<?php
// file: /app/config/report_parameters.php
return [
'params.report_output_dir' => '/var/data',
'params.last_days' => 10,
];<?php
declare(strict_types=1);
// file: /app/config/report_services_with_params.php
use App\Services\ReportMaker;
use App\Storages\ReportStorage;
use function Kaspi\DiContainer\{diAutowire, diParameter};
return static function (): Generator {
yield diAutowire(ReportStorage::class)
->bindArguments(
dir: diParameter('params.report_output_dir'),
);
yield diAutowire(ReportMaker::class)
->bindArguments(
lastDays: diParameter('params.last_days')
);
// other services
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string (9) "/var/data"
int(10)NOTE
Read the chapter “Container parameters”».
loadParameters()
The loadParameters() method registers container parameters from files:
Method signature:
\Kaspi\DiContainer\DiContainerBuilder\DiContainerBuilder::loadParameters(
string $file,
string ...$_
): staticParameters:
$file– full path to the file.$_– full path to the file.
addParameters()
The addParameters() method registers container parameters from a collection.
Method signature:
\Kaspi\DiContainer\DiContainerBuilder\DiContainerBuilder::addParameters(
iterable $params
): staticParameters:
$params– key-value collection of parameters configuration.
setParameter()
The setParameter() method registers a container parameter and its value.
Method signature:
\Kaspi\DiContainer\DiContainerBuilder\DiContainerBuilder::setParameter(
string $name,
array|int|float|string|bool|null|\UnitEnum $value
): staticParameters:
$name– the name of the parameter.$value– the value of the parameter.