Skip to content

References to the names of other container parameters ​

To reference another previously added container parameter, you must specify the parameter name between the { and } characters.

The value '{emails.admin}' will be interpreted as a request to retrieve the value of the container parameter named 'emails.admin'.

TIP

When combining links within a string parameter, it is permissible to include both numeric and string parameter values.

php
<?php

  return [
      'api.host' => 'api.example.com',
      'api.host_port' => 8080,
      'api.url' => '{api.host}:{api.host_port}',
  ];

Значение параметра 'api.url' будет приведено к строке 'api.example.com:8080'.

An example of using links in container parameters ​

PHP classes:

php
<?php
declare(strict_types=1);

// file: /app/src/Services/Foo.php

namespace App\Services;

final class Foo
{
    public function __construct(
        public readonly string $imagesPath,
        public readonly string $emails,
        public readonly array $notifyTo,
    ) {}    
}

Inject values ​​into the constructor of the PHP class \App\Services\Foo via container parameters.

Container parameter configuration files:

php
<?php

// file: /app/config/app_params.php

return [
    'storage' => '/var/storage',
    'emails.admin' => 'admin@example.com',
    'emails.manager' => 'manager@example.com',
];
php
<?php

// file: /app/config/dev_params.php

return [
    'storage.images' => '{storage}/images',
    'emails.as_string' => '{emails.admin}, {emails.manager}',
    'emails.as_array' => [
        '{emails.admin}',
        '{emails.manager}',
    ],
];

Declarations of implementation in the configuration file ​

In the services.php configuration file, declare an injection into the constructor of the PHP class \App\Services\Foo using the diParameter() helper function.

The app_params.php configuration file contains the primary parameter values for the container, while the dev_params.php file uses references to other container parameters to derive the required values for specific parameters.

php
<?php
declare(strict_types=1);

// file: /app/src/container_build.php

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

use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;

$container = (new DiContainerBuilder())
    ->loadParameters(
        '/app/config/app_params.php',
        '/app/config/dev_params.php',
    )
    ->load('/app/config/services.php')
    ->build();

$foo = $container->get(Foo::class);

var_dump($foo->imagesPath);
var_dump($foo->emails);
var_dump($foo->notifyTo);
php
<?php
declare(strict_types=1);

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

use App\Services\Foo;

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

return static function (): Generator {
    yield diAutowire(Foo::class)
        ->bindArguments(
            imagesPath: diParameter('storage.images'),
            emails: diParameter('emails.as_string'),
            notifyTo: diParameter('emails.as_array'),
        );
};
php
<?php

// file: /app/config/app_params.php

return [
    'storage' => '/var/storage',
    'emails.admin' => 'admin@example.com',
    'emails.manager' => 'manager@example.com',
];
php
<?php

// file: /app/config/dev_params.php

return [
    'storage.images' => '{storage}/images',
    'emails.as_string' => '{emails.admin}, {emails.manager}',
    'emails.as_array' => [
        '{emails.admin}',
        '{emails.manager}',
    ],
];

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
string(19) "/var/storage/images"
string(38) "admin@example.com, manager@example.com"
array(2) {
  [0]=>
  string(17) "admin@example.com"
  [1]=>
  string(19) "manager@example.com"
}

Declaring implementation via attributes ​

Inject container parameters into the constructor of the PHP class \App\Services\Foo using the \Kaspi\DiContainer\Attributes\Parameter attribute.

php
<?php
declare(strict_types=1);

// file: /app/src/Services/Foo.php

namespace App\Services;

use Kaspi\DiContainer\Attributes\Parameter;

final class Foo
{
    public function __construct(
        #[Parameter('storage.images')]
        public readonly string $imagesPath,
        #[Parameter('emails.as_string')]
        public readonly string $emails,
        #[Parameter('emails.as_array')]
        public readonly array $notifyTo,
    ) {}    
}
php
<?php
declare(strict_types=1);

// file: /app/src/container_build.php

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

use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;

$container = (new DiContainerBuilder())
    ->loadParameters(
        '/app/config/app_params.php',
        '/app/config/dev_params.php',
    )
    ->import(namespace: 'App\\', src: '/app/src')
    ->build();

$foo = $container->get(Foo::class);

var_dump($foo->imagesPath);
var_dump($foo->emails);
var_dump($foo->notifyTo);
php
<?php

// file: /app/config/app_params.php

return [
    'storage' => '/var/storage',
    'emails.admin' => 'admin@example.com',
    'emails.manager' => 'manager@example.com',
];
php
<?php

// file: /app/config/dev_params.php

return [
    'storage.images' => '{storage}/images',
    'emails.as_string' => '{emails.admin}, {emails.manager}',
    'emails.as_array' => [
        '{emails.admin}',
        '{emails.manager}',
    ],
];

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
string(19) "/var/storage/images"
string(38) "admin@example.com, manager@example.com"
array(2) {
  [0]=>
  string(17) "admin@example.com"
  [1]=>
  string(19) "manager@example.com"
}

Character escaping { ​

To escape the { character within a string, you must double it {{ to prevent it from being interpreted as a reference to a parameter name.

Consider this example: a container parameter 'params.foo' needs to be represented by the string 'foo{bar}baz'. Since the { character is reserved for syntax—specifically for referencing other container parameters it must be escaped; therefore, the value should be written as 'foo{{bar}baz'.

php
<?php

// file: /app/config/escaped_parameters.php

return [
     'params.foo' => 'foo{{bar}baz',
];