Skip to content

Declaring a tag via an attribute ​

The \Kaspi\DiContainer\Attributes\Tag attribute declares a tag and metadata for a PHP class.

The \Kaspi\DiContainer\Attributes\TaggedAs attribute injects a collection of elements from the container tagged with 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:

Example of configuration via an attribute ​

PHP classes:

php
<?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
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleA.php

namespace App\Rules;

final class RuleA
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleB.php

namespace App\Rules;

final class RuleB
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleC.php

namespace App\Rules;

final class RuleC
{
    // …
}

Configure the injection of a collection of container elements—marked with the tag 'tags.lite-rules' into the $rules parameter of the \App\Services\RuleCollection class constructor, using the \Kaspi\DiContainer\Attributes\TaggedAs attribute.

Assign the tag 'tags.lite-rules' to each class using the \Kaspi\DiContainer\Attributes\Tag attribute.

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use Kaspi\DiContainer\Attributes\TaggedAs;

final class RuleCollection
{
    public function __construct(
        #[TaggedAs('tags.lite-rules')]
        public readonly iterable $rules
    ) {}
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleA.php

namespace App\Rules;

use Kaspi\DiContainer\Attributes\Tag;

#[Tag('tags.lite-rules')]
final class RuleA
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleB.php

namespace App\Rules;


use Kaspi\DiContainer\Attributes\Tag;

#[Tag('tags.other-rulers')]
final class RuleB
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleC.php

namespace App\Rules;

use Kaspi\DiContainer\Attributes\Tag;

#[Tag('tags.lite-rules')]
final class RuleC
{
    // …
}
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\RuleCollection;

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->build();

$ruleCollection = $container->get(RuleCollection::class);

foreach ($ruleCollection->rules as $rule) {
    var_dump($rule::class);
}

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
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 into a parameter of type array, you must set the $isLazy parameter in the \Kaspi\DiContainer\Attributes\TaggedAs attribute to false.

PHP classes:

php
<?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
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleA.php

namespace App\Rules;

final class RuleA
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleB.php

namespace App\Rules;

final class RuleB
{
    // …
}
php
<?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. It is necessary to inject a collection of container elements marked with the tag 'tags.lite-rules' using the \Kaspi\DiContainer\Attributes\TaggedAs attribute.

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use Kaspi\DiContainer\Attributes\TaggedAs;

final class Rules
{
    public function __construct(
        #[TaggedAs(
            'tags.lite-rules',
            isLazy: false,
        )]
        public readonly array $rules
    ) {}
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleA.php

namespace App\Rules;

use Kaspi\DiContainer\Attributes\Tag;

#[Tag('tags.lite-rules')]
final class RuleA
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleB.php

namespace App\Rules;


use Kaspi\DiContainer\Attributes\Tag;

#[Tag('tags.other-rulers')]
final class RuleB
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleC.php

namespace App\Rules;

use Kaspi\DiContainer\Attributes\Tag;

#[Tag('tags.lite-rules')]
final class RuleC
{
    // …
}
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\Rules;

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->build();

$rules = $container->get(Rules::class);

var_dump(is_array($rules->rules));

foreach ($rules->rules as $rule) {
    var_dump($rule::class);
}

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
bool(true)
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
<?php
declare(strict_types=1);

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

namespace App\Services;

use Kaspi\DiContainer\Attributes\TaggedAs;

final class Qux
{
    public function __construct(
        #[TaggedAs('tags.lite-rules')]
        #[TaggedAs('tags.other-rulers')]
        iterable ...$rules
    ) {}

}

TIP

When using named arguments and variable-length parameter lists, the rules described in the PHP documentation apply.