Files
kiri-core/kiri-kafka/Annotation/Kafka.php
T
2021-08-24 18:24:46 +08:00

50 lines
822 B
PHP

<?php
namespace Kafka\Annotation;
use Annotation\Attribute;
use Exception;
use Kafka\ConsumerInterface;
use Kafka\KafkaProvider;
use Kiri\Kiri;
/**
* Class Kafka
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class Kafka extends Attribute
{
/**
* Kafka constructor.
* @param string $topic
*/
public function __construct(public string $topic)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
if (!in_array(ConsumerInterface::class, class_implements($class))) {
return false;
}
/** @var KafkaProvider $container */
$container = Kiri::getDi()->get(KafkaProvider::class);
$container->addConsumer($this->topic, $class);
return true;
}
}