Defining container parameters
Container parameters are declared using the \Kaspi\DiContainer\DiContainerBuilder class, or in configuration files via the definitions configurator.
IMPORTANT
The order in which container parameters are declared matters! If a container parameter has already been declared and another parameter with the same name is subsequently declared, its value will be overwritten the value from the latest declaration will be used.
Container parameter registration is typically defined in configuration files that represent key-value collections mapping parameter names to their respective values as PHP arrays. It is also possible to define parameters using any iterable collection; for instance, one can use a callable that returns an iterable type, allowing the collection to be traversed.
Defining container parameters from files
Kaspi\DiContainer\DiContainerBuilder::loadParameters(
string $file,
string ...$_
): staticParameters:
$file– the full path to the file that defines the parameter configuration.$_– additional configuration files for container parameters.
IMPORTANT
The container parameter configuration file must begin with the return keyword and return either an iterable type or a callable type that, in turn, must also return an iterable type.
Declaring container parameters from a collection
Kaspi\DiContainer\DiContainerBuilder::addParameters(
iterable $params
): staticParameters:
$params– a key-value collection of configuration parameters.
Declaration of a single container parameter
Kaspi\DiContainer\DiContainerBuilder::setParameter(
string $name,
array|int|float|string|bool|null|\UnitEnum $value
): staticParameters:
$name– the name of the parameter.$value– the value of the parameter.
Declaring container parameters in the definition configurator
You can also declare and manage container parameters in configuration files using the definitions configurator.
IMPORTANT
Any parameters previously added to the configuration can be replaced with new values, provided the parameter name matches.
The \Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface interface provides methods for declaring container parameters:
DefinitionsConfiguratorInterface::loadParameters()– declaring container parameters from files.DefinitionsConfiguratorInterface::addParameters()– declaring container parameters from a collection.DefinitionsConfiguratorInterface::setParameter()– declaration of a parameter and its value.DefinitionsConfiguratorInterface::removeParameter()– removing a parameter by name from the container configuration.DefinitionsConfiguratorInterface::hasParameter()– checking for the availability of a parameter by name.
Example of declaring and managing container parameters in a configuration file:
<?php
declare(strict_types=1);
use App\Services\Foo;
use Kaspi\DiContainer\Interfaces\DefinitionsConfiguratorInterface;
use function Kaspi\DiContainer\{diAutowire, diParameter};
return static function (DefinitionsConfiguratorInterface $configurator): Generator {
$configurator->loadParameters('/app/config/more_app_params.php');
if (!$configurator->hasParameter('debug')) {
$configurator->setParameter('baz', null);
} else {
$configurator->removeParameter('foo');
}
yield diAutowire(Foo::class)
->bindArguments(
diParameter('baz')
);
};