Skip to content

Context for configuration files ​

When building and configuring a container, you can define an additional context for configuration files.

This context is defined by the developer as a collection of key-value pairs. You can pass any type — such as an object, an array of values, or any other desired type — into the collection associated with a given key.

The methods DiContainerBuilder::addConfiguratorContexts() and DiContainerBuilder::setConfiguratorContext() are used to set the context for configuration files.

getContext() ​

The getContext() method retrieves a context value by name.

Method signature:

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

Parameters:

Parameters:

  • $name – the name of the context.
  • $fallback – a fallback handler to be used in case the context is not found by its name.

TIP

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

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

Example of using context in configuration files ​

In the configuration file /app/app/config/foo_service.php, a context is required to define a PHP class: a value must be provided for the \App\Services\Foo::$dsn property (for database configuration), and a value — computed via the \App\Core::calculateFoo() PHP method must be assigned to the \App\Services\Foo::$foo property.

Database connection settings are stored in the DATABASE_DSN environment variable.

Building the container:

php
<?php
declare(strict_types=1);

// file: /app/src/build_container.php

require_once __DIR__.'/../vendor/autoload.php';

use Kaspi\DiContainer\DiContainerBuilder;
use Dotenv\Dotenv;

$builder = (new DiContainerBuilder())
    ->import('App\\', '/app/src')
    ->load(
        '/app/config/services/services.php',
        '/app/app/config/foo_service.php',
    );

// I am using a third-party library for working with `.env` files.
$dotenv = Dotenv::createImmutable('/app/');
$dotenv->required('DATABASE_DSN');
$dotenv->load();

$builder->setConfiguratorContext('DATABASE_DSN', $_ENV['DATABASE_DSN']);

/**
 * an object containing the data needed to configure the container
 * @var \App\Core $core
 */ 
$builder->setConfiguratorContext($core::class, $core);

// ...

$container = $builder->build();
php
<?php
declare(strict_types=1);

// file: /app/app/config/foo_service.php

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

use function Kaspi\DiContainer\diAutowire;

return static function (DefinitionsConfiguratorInterface $configurator): Generator {

    $dsn = $configurator->getContext('DATABASE_DSN');
    /**
     * @var \App\Core $core
    */ 
    $core = $configurator->getContext(Core::class);

    yield diAutowire(Foo::class)
        ->bindArguments(
            dsn: $dsn,
            foo: $core->calculateFoo(),
        );
};