Container parameters
When declaring a PHP class or a callable type[1], you do not need to specify how the container should inject dependencies of simple types; instead, you can provide the values "as-is" using the bindArguments()[2] method.
Simple types include int, float, string, bool, null, \UnitEnum, or an array consisting of these types.
Consider the following example: we need to inject dependencies specifically simple types into PHP classes by providing the dependency values "as-is" via the bindArguments() method.
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
final class Foo
{
public function __construct(
private readonly string $adminEmail
) {}
//…
}<?php
declare(strict_types=1);
// file: /app/src/Services/Configurator.php
namespace App\Services;
final class Configurator
{
public function __construct(private readonly Bar $bar) {}
public static function __invoke(string $adminEmail): mixed
{
// …
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
// …
}Declaration of simple parameter implementations in a configuration file:
<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\{Foo, Configurator};
use function Kaspi\DiContainer\{diAutowire, diFactory};
return static function (): Generator {
yield diAutowire(Foo::class)
->bindArguments(
// as-is value
adminEmail: 'admin@example.com'
);
yield 'services.do_something' => diFactory(Configurator::class)
->bindArguments(
// as-is value
adminEmail: 'admin@example.com'
);
};As you can see in the services.php configuration file, the same value, 'admin@example.com', is used for injection.
This recurring value can be defined as a "container parameter" a reusable value that can be referenced when declaring injections into parameters.
Container parameter configuration can be represented as a collection of key-value pairs, where the key is the string name of the parameter, and the value is one of the supported types.
In the example above, the value used for a class method parameter can be defined as a container parameter:
<?php
declare(strict_types=1);
// file: /app/config/parameters.php
return [
'adminEmail' => 'admin@example.com',
];The helper function diParameter() injects a dependency from container parameters:
<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\{Foo, Configurator};
use function Kaspi\DiContainer\{diAutowire, diFactory, diParameter};
return static function (): Generator {
yield diAutowire(Foo::class)
->bindArguments(
adminEmail: 'admin@example.com'
adminEmail: diParameter('adminEmail')
);
yield 'services.do_something' => diFactory(Configurator::class)
->bindArguments(
adminEmail: 'admin@example.com'
adminEmail: diParameter('adminEmail')
);
};To declare container parameters, use the DiContainerBuilder::loadParameters() method:
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
$container = (new DiContainerBuilder())
->loadParameters('/app/config/parameters.php')
->load('/app/config/services.php')
->import('App\\', src: '/app/src/')
->build();Supported parameter value types for containers
The following simple value types can be set as container parameters:
- scalar PHP types:
int,float,string, andbool - the
nulltype - enumerated types that inherit from
\UnitEnum - arrays consisting of the aforementioned types
- PHP constants (defined via the
\define()function) that hold values of the aforementioned types - values from public constants defined within PHP classes or interfaces, provided those constants are of the aforementioned types.
<?php
declare(strict_types=1);
return [
// string value
'adminEmail' => 'admin@example.com',
// Boolean type
'services.enable_logging' => true,
// numeric type - integer, floating-point number
'logger.port' => 1_025,
// null type
'smtp.context' => null,
// array
'ui.locales' => [
'ru' => 'ru_RU.utf8',
'en' => 'en_US.utf8',
'fi' => 'fi_FI.utf8',
],
// a value set from a PHP constant defined via `\define()`
'value.from_constat' => CONSTATN_NAME,
// a value set via a public constant in a class
'value.from_class_constat' => Foo::MAX_SIZE,
// a value defined via enumerations that inherit from `\UnitEnum`
'value.from_enumerator' => FooEnum::Viewed,
];Section Contents
- Declaration of container parameters.
- References to the names of other container parameters.
- Declaring dependency injection via container parameters.
- Runtime container parameters.
The
callabletype represents values or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎To pass arguments, use the
bindArguments()method, which is available in helper functions,diAutowire(),diCallable(),diFactory(). ↩︎