Declaring a tag in configuration files
The bindTag()[1] method, defined by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionTagArgumentInterface interface, declares a tag and its metadata.
The diTaggedAs() helper function injects a collection of items from the container that are marked with a tag of the same name.
IMPORTANT
PHP classes that have not been explicitly registered in the container will be ignored during tag-based lookups; therefore, you must ensure that these classes are added to the container using one of the following methods:
- importing PHP classes — via the
DiContainerBuilder::import()method. - using configuration files — via the
DiContainerBuilder::load()method.
Example of use
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/RuleCollection.php
namespace App\Services;
final class RuleCollection
{
public function __construct(public readonly iterable $rules) {}
}<?php
declare(strict_types=1);
// file: /app/src/Rules/RuleA.php
namespace App\Rules;
final class RuleA
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Rules/RuleB.php
namespace App\Rules;
final class RuleB
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Rules/RuleC.php
namespace App\Rules;
final class RuleC
{
// …
}Declare the 'tags.lite-rules' tag for each class using the bindTag()[1:1] method in the rule_services.php configuration file.
Register the injection of a collection of container elements marked with the 'tags.lite-rules' tag into the $rules parameter of the \App\Services\RuleCollection class constructor, using the diTaggedAs() helper function within the services.php configuration file.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\RuleCollection;
$container = (new DiContainerBuilder())
->load('/app/config/rule_services.php')
->load('/app/config/services.php')
->build();
$ruleCollection = $container->get(RuleCollection::class);
foreach ($ruleCollection->rules as $rule) {
var_dump($rule::class);
}<?php
declare(strict_types=1);
// file: /app/config/rule_services.php
use App\Rules\{RuleA, RuleB, RuleC};
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(RuleA::class)
->bindTag('tags.lite-rules');
yield diAutowire(RuleB::class)
->bindTag('tags.other-rulers');
yield diAutowire(RuleC::class)
->bindTag('tags.lite-rules');
};<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\RuleCollection;
use function Kaspi\DiContainer\{diAutowire, diTaggedAs};
return static function (): Generator {
yield diAutowire(RuleCollection::class)
->bindArguments(
rules: diTaggedAs('tags.lite-rules')
);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string (15) "App\Rules\RuleA"
string (15) "App\Rules\RuleC"The \App\Rules\RuleB class is not included in the collection because it is marked with the tag 'tags.other-rulers'.
NOTE
The order of elements in the collection is not defined in this configuration and can be any. Please review the documentation section "Collection Item Priority".
Dependency injection into a parameter of type array
When injecting a collection of elements from a container—identified by a specific tag — into a parameter of type array, you must set the $isLazy argument to false within the diTaggedAs() helper function.
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Rules.php
namespace App\Services;
final class Rules
{
public function __construct(
public readonly array $rules
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Rules/RuleA.php
namespace App\Rules;
final class RuleA
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Rules/RuleB.php
namespace App\Rules;
final class RuleB
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Rules/RuleC.php
namespace App\Rules;
final class RuleC
{
// …
}The $rules parameter of the \App\Services\Rules class constructor is of type array. You need to inject a collection of container elements marked with the 'tags.lite-rules' tag using the diTaggedAs() helper function within the services.php configuration file.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Rules;
$container = (new DiContainerBuilder())
->load('/app/config/services.php')
->load('/app/config/rule_services.php')
->build();
$rules = $container->get(Rules::class);
var_dump(is_array($rules->rules));
// bool(true)
foreach ($rules->rules as $rule) {
var_dump($rule::class);
}<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\Rules;
use function Kaspi\DiContainer\{diAutowire, diTaggedAs};
return static function (): Generator {
yield diAutowire(Rules::class)
->bindArguments(
rules: diTaggedAs(
'tags.lite-rules',
isLazy: false,
)
);
};<?php
declare(strict_types=1);
// file: /app/config/rule_services.php
use App\Rules\{RuleA, RuleB, RuleC};
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(RuleA::class)
->bindTag('tags.lite-rules');
yield diAutowire(RuleB::class)
->bindTag('tags.other-rulers');
yield diAutowire(RuleC::class)
->bindTag('tags.lite-rules');
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string (15) "App\Rules\RuleA"
string (15) "App\Rules\RuleC"The \App\Rules\RuleB class is not included in the collection because it is marked with the tag 'tags.other-rulers'.
NOTE
The order of elements in the collection is not defined in this configuration and can be any. Please review the documentation section "Collection Item Priority".
Implementing a collection for variable-length parameters
A variable-length parameter also supports the inclusion of collections of elements from a container marked with a tag.
<?php
declare(strict_types=1);
// file: /app/config/tagged_services.php
use App\Services\Bar;
use function Kaspi\DiContainer\{diAutowire, diTaggedAs};
return static function (): Generator {
yield diAutowire(Bar::class)
->bindArguments(
rules_liteRules: diTaggedAs('tags.lite-rules'),
rules_otherRules: diTaggedAs('tags.other-rulers'),
);
};<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
class Bar
{
public function __construct(iterable ...$rules) {}
}TIP
When using named arguments and variable-length parameter lists, the rules described in the PHP documentation apply.
To specify tags, use the
bindTag()method, which is available in helper functions:diAutowire(),diCallable(),diProxyClosure(),diValue(). ↩︎ ↩︎