Files
kiri-core/Kafka/Annotation/Kafka.php
T

50 lines
822 B
PHP
Raw Normal View History

2021-04-11 16:22:21 +08:00
<?php
2021-08-20 14:34:10 +08:00
namespace Kafka\Annotation;
2021-04-11 16:22:21 +08:00
2021-08-20 14:34:10 +08:00
use Annotation\Attribute;
2021-07-12 17:17:36 +08:00
use Exception;
2021-04-11 17:18:54 +08:00
use Kafka\ConsumerInterface;
2021-08-04 14:38:20 +08:00
use Kafka\KafkaProvider;
2021-08-11 01:04:57 +08:00
use Kiri\Kiri;
2021-04-11 17:18:54 +08:00
2021-04-11 16:22:21 +08:00
/**
* Class Kafka
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class Kafka extends Attribute
{
2021-08-20 14:36:50 +08:00
/**
* Kafka constructor.
* @param string $topic
*/
public function __construct(public string $topic)
{
}
2021-04-11 16:22:21 +08:00
2021-07-12 17:17:36 +08:00
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
2021-08-20 14:36:50 +08:00
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;
}
2021-04-11 16:22:21 +08:00
}