Declaring definitions in configuration files
Configuration files help to separate and group container definitions. By using different configuration files, you can selectively include the ones you need based on specific conditions and pass them into the container build process.
load()
The load() method registers definitions in the container based on configuration files, while simultaneously ensuring that the identifiers within the container remain unique [1].
Method signature:
\Kaspi\DiContainer\DiContainerBuilder::load(
string $file,
string ...$_
): static;Parameters:
$file– full path to the configuration file.$_– full path to the configuration file.
WARNING
Calling the DiContainerBuilder::build() method will throw an exception that implements the \Kaspi\DiContainer\Interfaces\Exceptions\ContainerBuilderExceptionInterface interface if any non-unique identifiers are found among the definitions provided for building the container.
Example of using the load() method
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Storages/ReportStorage.php
namespace App\Storages;
final class ReportStorage
{
public function __construct(
public readonly string $dir
) {}
}<?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,
) {}
}In the report_services.php configuration file, PHP classes are defined using scalar dependency injection: \App\Storages\ReportStorage is injected into the $dir parameter, and \App\Services\ReportMaker is injected into the $lastDays parameter.
TIP
To declare PHP classes, the helper function diAutowire() is used, which allows you to pass a container identifier into the configuration.
<?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.php')
->build();
$service = $container->get(ReportMaker::class);
var_dump($service->storage->dir);
var_dump($service->lastDays);<?php
declare(strict_types=1);
// file: /app/config/report_services.php
use App\Services\ReportMaker;
use App\Storages\ReportStorage;
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(ReportStorage::class)
->bindArguments(
dir: '/var/data'
);
yield diAutowire(ReportMaker::class)
->bindArguments(
lastDays: 10
);
// other services
};Run the following command in the terminal:
php /app/src/container_build.phpA message similar to the following will appear:
string (9) "/var/data"
int(10)loadOverride()
In some cases, it may be necessary to override previously established definitions associated with the same container identifier [1:1].
The loadOverride() method declares container definitions from configuration files, overwriting any previously defined entries that share the same identifier.
Method signature:
\Kaspi\DiContainer\DiContainerBuilder::loadOverride(
string $file,
string ...$_
): static;Parameters:
$file– full path to the configuration file.$_– full path to the configuration file.
Example of using the loadOverride() method
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_services.php configuration file, PHP classes are defined with scalar dependency injection: \App\Storages\ReportStorage receives a value for the $dir parameter, and \App\Services\ReportMaker receives a value for the $lastDays parameter. If the APP_ENV environment variable is set to 'dev', then scalar dependency injection for these classes must be redefined with different values—effectively overwriting the previously declared definitions for the same container identifiers[1:2].
TIP
To declare PHP classes, the helper function diAutowire() is used, which allows you to pass a container identifier into the configuration.
<?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;
use function {getenv, putenv};
putenv('APP_ENV=dev');
$builder = (new DiContainerBuilder())
->load('/app/config/report_services.php');
if ('dev' === getenv('APP_ENV')) {
$builder->loadOverride('/app/config/dev/services.php');
}
$container = $builder->build();
$service = $container->get(ReportMaker::class);
var_dump($service->storage->dir);
var_dump($service->lastDays);<?php
declare(strict_types=1);
// file: /app/config/report_services.php
use App\Services\ReportMaker;
use App\Storages\ReportStorage;
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(ReportStorage::class)
->bindArguments(
dir: '/var/data'
);
yield diAutowire(ReportMaker::class)
->bindArguments(
lastDays: 10
);
// other services
};<?php
declare(strict_types=1);
// file: /app/config/dev/services.php
use App\Storages\ReportStorage;
use App\Services\ReportMaker;
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(ReportMaker::class)
->bindArguments(
lastDays: 1
);
yield diAutowire(ReportStorage::class)
->bindArguments(
dir: '/tmp'
);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string (4) "/tmp"
// int(1)