Files
kiri-core/Annotation/Kafka.php
T

50 lines
855 B
PHP
Raw Normal View History

2021-04-11 16:22:21 +08:00
<?php
namespace Annotation;
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-04-11 18:26:11 +08:00
use Snowflake\Snowflake;
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
{
/**
* Kafka constructor.
* @param string $topic
*/
2021-04-11 17:18:54 +08:00
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
*/
public function execute(mixed $class, mixed $method = null): bool
2021-04-11 16:22:21 +08:00
{
2021-05-03 03:48:52 +08:00
if (!($class instanceof ConsumerInterface)) {
2021-04-11 17:18:54 +08:00
return false;
}
2021-08-04 14:38:20 +08:00
/** @var KafkaProvider $container */
2021-08-05 17:02:12 +08:00
$container = Snowflake::getDi()->get(KafkaProvider::class);
2021-08-05 15:55:27 +08:00
$container->addConsumer($this->topic, $class);
2021-04-11 17:18:54 +08:00
2021-05-03 03:48:52 +08:00
return true;
2021-04-11 16:22:21 +08:00
}
}