Skip to content

Атрибут AutowireExclude ​

Атрибут \Kaspi\DiContainer\Attributes\AutowireExclude исключает из внедряемых зависимостей контейнера PHP-класс или интерфейс. FQCN [1] будет недоступен для метода контейнера get() и has().

Конфигурация атрибута:

  • \Attribute::TARGET_CLASS

Сигнатура атрибута:

php
\Kaspi\DiContainer\Attributes\AutowireExclude::__construct()

У атрибута нет параметров.

Предупреждение

Атрибут \Kaspi\DiContainer\Attributes\AutowireExclude исключает из конфигурирования любые другие атрибуты.

Пример исключения PHP-класса ​

PHP-классы:

php
<?php
declare(strict_types=1);

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

namespace App\Services;

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

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

namespace App\Services;

final class Bar
{
    // …
}

Исключить из контейнера PHP-класс \App\Services\Foo через атрибут \Kaspi\DiContainer\Attributes\AutowireExclude.

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use Kaspi\DiContainer\Attributes\AutowireExclude;

#[AutowireExclude]
final class Foo
{
    // …
}
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, Bar};

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

var_dump($container->has(Foo::class));
var_dump($container->has(Bar::class));

Выполните в терминале команду:

shell
php /app/src/container_build.php

Появится сообщение вида:

console
(bool) false
(bool) true

  1. Fully Qualified Class Name – полное имя класса или интерфейса, включая пространство имён. ↩︎