Skip to content

Declaring definitions via a collection ​

Definitions can be declared using a collection. This collection must be an iterable type. Its elements should be key-value pairs, where the key is a container identifier [1] and the value is the definition declaration.

addDefinitions() ​

The addDefinitions() method declares container definitions while ensuring that each container identifier [1:1] remains unique throughout the entire container configuration.

Method signature:

php
\Kaspi\DiContainer\DiContainerBuilder::addDefinitions(
    iterable $definitions
): static;

Parameters:

  • $definitions – a collection of definitions.

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 addDefinitions() method ​

PHP classes:

php
<?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
<?php
declare(strict_types=1);

// file: /app/src/Storages/ReportStorage.php

namespace App\Storages;

final class ReportStorage
{
    public function __construct(
        public readonly string $dir
    ) {}
}

Declare PHP classes via a collection, with scalar parameters injected: \App\Storages\ReportStorage into the $dir parameter, and \App\Services\ReportMaker 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
<?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 App\Storages\ReportStorage;

use function Kaspi\DiContainer\diAutowire;

// An anonymous function as a collection
$fn = static function (): Generator {
    yield diAutowire(ReportStorage::class)
        ->bindArguments(
            dir: '/var/data'
        );
};

// PHP array as a collection
$services = [
    diAutowire(ReportMaker::class)
        ->bindArguments(
            lastDays: 10
        )
];

$container = (new DiContainerBuilder())
    ->addDefinitions(($fn)())
    ->addDefinitions($services)
    ->build();


$service = $container->get(ReportMaker::class);

var_dump($service->storage->dir);
var_dump($service->lastDays);

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
string (9) "/var/data"
int(10)

addDefinitionsOverride() ​

The addDefinitionsOverride() method registers definitions from a collection, overwriting any previously registered definitions that share the same identifier within the container[1:2].

Method signature:

php
\Kaspi\DiContainer\DiContainerBuilder::addDefinitionsOverride(
    iterable $definitions
): static;

Parameters:

  • $definitions – a collection of definitions.

Example of using the addDefinitionsOverride() method ​

PHP classes:

php
<?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
<?php
declare(strict_types=1);

// file: /app/src/Storages/ReportStorage.php

namespace App\Storages;

final class ReportStorage
{
    public function __construct(
        public readonly string $dir
    ) {}
}

The report_services.php configuration file defines PHP classes into which scalar parameters are injected: a value is passed to the $dir parameter of \App\Storages\ReportStorage, and a value is passed to the $lastDays parameter of \App\Services\ReportMaker. If the APP_ENV environment variable is set to 'dev', then the scalar value '/tmp' must be injected into the $dir parameter of the \App\Storages\ReportStorage class (via a collection of parameters). In effect, this means overwriting a previously established definition with a new value for the same identifier within the container[1:3].

TIP

To declare PHP classes, the helper function diAutowire() is used, which allows you to pass a container identifier into the configuration.

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\ReportMaker;
use App\Storages\ReportStorage;

use function Kaspi\DiContainer\diAutowire;
use function {getenv, putenv};

putenv('APP_ENV=dev');

$builder = (new DiContainerBuilder())
    ->load('/app/config/report_services.php');

if ('dev' === getenv('APP_ENV')) {
    $devServices = [
        diAutowire(ReportStorage::class)
            ->bindArguments(dir: '/tmp')
    ];

    $builder->addDefinitionsOverride($devServices);
}

$container = $builder->build();

$service = $container->get(ReportMaker::class);

var_dump($service->storage->dir);
var_dump($service->lastDays);
php
<?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:

shell
php /app/src/container_build.php

A message like the following will appear:

console
string (4) "/tmp"
int(10)

  1. A container ID is a string name (or a class/interface name) used to register a service or object within a container and to retrieve it via the get() method. ↩︎ ↩︎ ↩︎ ↩︎