Removing elements from a collection
Collections can exclude elements. An element can be excluded from a collection based on its container identifier[1] or by specifying the calling PHP class that is, the class itself.
Exclusion from a collection by container identifier
To exclude elements from a collection, use the $containerIdExclude parameter in the diTaggedAs() helper function and the \Kaspi\DiContainer\Attributes\TaggedAs attribute.
The $containerIdExclude parameter can contain the container identifiers of the elements to be excluded from the injected collection.
An example of removing an item from a collection
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Service;
final class Foo
{
public function __construct(public readonly iterable $services) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Service;
final class Bar
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Service;
final class Baz
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Qux.php
namespace App\Service;
final class Qux
{
// …
}The PHP classes \App\Service\Bar, \App\Service\Baz, and \App\Service\Qux will be marked with the tag 'tags.internal_service'.
A collection of elements tagged with 'tags.internal_service' must be injected into the $services parameter of the PHP class \App\Service\Foo. However, the collection element with the container identifier 'App\Services\Baz' must be excluded from this collection; the element can be retrieved via the container method .
Declaration in configuration files
In the configuration file services.php in the $containerIdExclude parameter of the diTaggedAs() helper function, specify the 'App\Services\Baz' identifier to exclude from the collection.
In the tagged_services.php configuration file, declare the tag 'tags.internal_service' for the remaining PHP classes using the bindTag() method.
<?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())
->load('/app/config/services.php')
->load('/app/config/tagged_services.php.php')
->build();
$foo = $container->get(Foo::class);
foreach ($foo->services as $service) {
var_dump($service::class);
}<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\{Foo, Bar, Baz, Qux};
use function Kaspi\DiContainer\{diAutowire, diTaggedAs};
return static function (): Generator {
yield diAutowire(Foo::class)
->bindArguments(
services: diTaggedAs(
tag: 'tags.internal_service',
containerIdExclude: [
Baz::class,
],
)
);
yield diAutowire(Bar::class)
->bindTag('tags.internal_service');
yield diAutowire(Baz::class)
->bindTag('tags.internal_service');
yield diAutowire(Qux::class)
->bindTag('tags.internal_service');
};<?php
declare(strict_types=1);
// file: /app/config/tagged_services.php
use App\Services\{Bar, Baz, Qux};
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(Bar::class)
->bindTag('tags.internal_service');
yield diAutowire(Baz::class)
->bindTag('tags.internal_service');
yield diAutowire(Qux::class)
->bindTag('tags.internal_service');
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string(16) "App\Services\Bar"
string(16) "App\Services\Qux"Declaration via attributes
Use the $containerIdExclude parameter of the \Kaspi\DiContainer\Attributes\TaggedAs attribute to specify the identifier 'App\Services\Baz' for exclusion from the collection.
Declare the 'tags.internal_service' tag for other PHP classes using the \Kaspi\DiContainer\Attributes\Tag attribute.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Service;
use Kaspi\DiContainer\Attributes\TaggedAs;
final class Foo
{
public function __construct(
#[TaggedAs(
'tags.internal_service',
containerIdExclude: [
Baz::class
],
)]
public readonly iterable $services
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Service;
use Kaspi\DiContainer\Attributes\Tag;
#[Tag('tags.internal_service')]
final class Bar
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Service;
use Kaspi\DiContainer\Attributes\Tag;
#[Tag('tags.internal_service')]
final class Baz
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Qux.php
namespace App\Service;
use Kaspi\DiContainer\Attributes\Tag;
#[Tag('tags.internal_service')]
final class Qux
{
// …
}<?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())
->import(namespace: 'App\\', src: '/app/src/')
->build();
$foo = $container->get(Foo::class);
foreach ($foo->services as $service) {
var_dump($service::class);
}Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
string(16) "App\Services\Bar"
string(16) "App\Services\Qux"Excluding the calling class
A collection of elements may contain a PHP class that itself injects that same collection. To prevent this behavior, refer to the $selfExclude parameter in the diTaggedAs() helper function and the \Kaspi\DiContainer\Attributes\TaggedAs attribute. By default, this parameter is set to true.
If the calling PHP class should be included in the collection, you must set the $selfExclude parameter to false.
<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\ClassTaggedAs;
use function Kaspi\DiContainer\{diAutowire, diTaggedAs};
return static function (): Generator {
yield diAutowire(ClassTaggedAs::class)
->bindTag('tags.foo')
->bindArguments(
items: diTaggedAs(
'tags.foo',
selfExclude: false,
)
);
};<?php
declare(strict_types=1);
// file: /app/src/Services/ClassTaggedAs.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\{Tag, TaggedAs};
#[Tag('tags.foo')]
final class ClassTaggedAs
{
public function __construct(
#[TaggedAs(
'tags.foo',
selfExclude: false,
)]
public readonly iterable $items
) {}
}A container ID is a string name (or a class/interface name) used to register a service or object within a container and to retrieve it via the
get()method. ↩︎