Skip to content

Definition configuration files ​

You can use configuration files and helper functions for declaring container definitions to define container elements.

The \Kaspi\DiContainer\DiContainerBuilder class provides the load() and loadOverride() methods, which are designed for declaring container definitions and their corresponding identifiers[1] based on configuration files.

The file must use the return keyword and return an iterable or a callable type.

In PHP, iterable is a special pseudo-type that encompasses any arrays and objects of classes that implement the \Traversable interface.

In configuration files, it is recommended to use anonymous functions that act as generators (functions utilizing the yield keyword); this allows the declaration of definitions to be deferred until the moment they are actually configured.

The container configuration tracks the uniqueness of container identifiers[1:1]. When the DiContainerBuilder::build() method is called to construct the container, an exception—implementing the \Kaspi\DiContainer\Interfaces\Exceptions\ContainerBuilderExceptionInterface interface—will be thrown if any non-unique container definition identifiers are detected within the configuration files.

Anonymous function and generator ​

A configuration file that returns an iterable type can be represented, for example, by an anonymous function. The result of executing such a function is a generator (\Generator).

php
<?php
declare(strict_types=1);

// file: /app/config/as_generator_simple.php

use App\Services\Foo;

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield diAutowire(Foo::class)
        ->bindArguments('baz val');
};

TIP

For the PHP class App\Services\Foo, the container identifier [1:2] will be generated automatically via the diAutowire() helper function.

Anonymous function with a configurator and a generator ​

A configuration file can be represented as a callable; in this case, the container passes a container definition configurator as the first argument.

An anonymous function can be used as this callable.

php
<?php
declare(strict_types=1);

// file: /app/config/as_generator_with_configurator.php

use App\Services\Foo;
use Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface;

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
return static function (DefinitionsConfiguratorInterface $configurator): Generator {
    $configurator->removeDefinition(Baz::class);
    $configurator->setParameter('params.baz', 'Lorem ipsum');

    yield diAutowire(Foo::class)
        ->bindArguments('baz val');
};

Callable type with a configurator ​

The configuration file can be represented as a callable type that returns void when invoked.

php
<?php
declare(strict_types=1);

// file: /app/config/callable_with_configurator.php

use App\Services\Foo;
use Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface;

use function Kaspi\DiContainer\diAutowire;

return static function (DefinitionsConfiguratorInterface $configurator): Generator {
return static function (DefinitionsConfiguratorInterface $configurator): void {
    $configurator->removeDefinition(Baz::class);
    $configurator->setParameter('params.baz', 'Lorem ipsum');
    $configurator->setDefinition(
        Foo::class,
        diAutowire(Foo::class)
            ->bindArguments('baz val')
    );
};

PHP array ​

The configuration file can be represented as a PHP array.

php
<?php

// file: /app/config/as_array.php

use App\Services\Foo;

use function Kaspi\DiContainer\diAutowire;

return [
    diAutowire(Foo::class)
        ->bindArguments('baz val')
];

  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. ↩︎ ↩︎ ↩︎