Skip to content

Declaring and retrieving container definitions ​

When building a container via \Kaspi\DiContainer\DiContainerBuilder, all declared definitions will be available within the definition configurator, including those imported via the DiContainerBuilder::import() method.

getDefinitions() ​

The getDefinitions() method returns a collection of all definitions declared at the time the container is built, where the collection key is the container identifier[1].

The method can only return definitions that were declared prior to the call to getDefinitions().

Method signature:

php
\Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface::getDefinitions(): iterable

Example of use:

php
<?php
declare(strict_types=1);

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

use Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface;

return static function (DefinitionsConfiguratorInterface $configurator): void {

    foreach ($configurator->getDefinitions() as $id => $definition) {
        // do something.
    }

};

setDefinition() ​

The setDefinition() method declares a definition and its container identifier[1:1]. If the identifiers match, this method will replace any previously declared container definition.

Method signature:

php
\Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface::setDefinition(
    string $id,
    mixed $definition
): void

Parameters:

  • $id – the container identifier.
  • $definition – the container definition.

Usage example:

php
<?php
declare(strict_types=1);

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

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

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

return static function (DefinitionsConfiguratorInterface $configurator): void {
    if ('STAGES' === getenv('APP_ENV')) {
        $configurator->setDefinition(
            Mail::class,
            diAutowire(Mail::class)
                ->bindArguments(
                    host: diParameter('mail.host.local'),
                    port: diParameter('mail.port.local'),
                );
        );
    }
};

getDefinition() ​

The method retrieves the declared container definition using the container identifier[1:2].

Method signature:

php
\Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface::getDefinition(
    string $id,
    ?callable $fallback = null
): mixed

Parameters:

  • $id – the identifier of the definition container.
  • $fallback – a fallback handler for cases where the definition is not found.

TIP

The $fallback parameter uses the type callable(string $id): mixed.

If the $fallback parameter is set to null and the definition is not found, an exception implementing the \Kaspi\DiContainer\Interfaces\Exceptions\NotFoundDefinitionInterface interface will be thrown.

Example of use:

php
<?php
declare(strict_types=1);

// file: /app/config/foo_bar_services.php

use Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface;
use Kaspi\DiContainer\Exception\NotFoundDefinition;

use function Kaspi\DiContainer\diAutowire;

return static function (DefinitionsConfiguratorInterface $configurator): void {
    // without specification $fallback
    try {
        $configurator->getDefinition('foo_bar');
    } catch (NotFoundDefinition) {
        $configurator->setDefinition(
            'foo_bar',
            diAutowire(FooBar::class)
                ->bindArguments('foo', 'bar')
        );
    }

    // Use $fallback
    $qux = $configurator->getDefinition('qux', static fn () => null);

    if (null === $qux) {
        $configurator->setDefinition(
            'qux',
            diAutowire(Qux::class)
                ->setup('doConfig')
        );
    }
};

findTaggedDefinitions() ​

The findTaggedDefinitions() method returns a collection of all declared definitions that carry a specific tag, where the collection key is the container identifier[1:3].

Method signature:

php
\Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface::findTaggedDefinitions(
    string $tag
): iterable

Parameters:

  • $tag – tag name, or the FQCN[2] of an interface.

TIP

In the $tag parameter, you can specify the interface's FQCN.

Example of use:

php
<?php
declare(strict_types=1);

// file: /app/config/services_tagged.php

use Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface;
use function Kaspi\DiContainer\diAutowire;
use App\Interfaces\FooInterface;
use App\Services\Client;

return static function (DefinitionsConfiguratorInterface $configurator): void {
    $definitions = $configurator->findTaggedDefinitions(FooInterface::class);
    
    foreach ($definitions as $id => $definition) {
        $definition->setup('setClient', diAutowire(Client::class));
    }
};

removeDefinition() ​

The removeDefinition() method excludes an identifier[1:4] from the container and removes the definition declaration, if one was previously made.

Method signature:

php
\Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface::removeDefinition(
    string $id,
): void

Parameters:

  • $id – container identifier.

Any excluded container IDs will not be injectable into parameters.

When receiving through the container method has(), false will be returned, and when trying to get an excluded identifier through the container method get(), an exception implementing the interface \Psr\Container\NotFoundExceptionInterface will be thrown.

Usage example:

php
<?php
declare(strict_types=1);

// file: /app/config/services_removed.php

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

use function getenv;

return static function (DefinitionsConfiguratorInterface $configurator): void {
    if ('STAGES' === getenv('APP_ENV') {
        $configurator->removeDefinition(Qux::class);
    }
};

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

  2. Fully Qualified Class Name – the full name of a class or interface, including its namespace. ↩︎