Files
kiri-core/Annotation/Kafka.php
T

51 lines
866 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;
use Kafka\TaskContainer;
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-04-11 18:26:11 +08:00
/** @var TaskContainer $container */
$container = Snowflake::app()->get('kafka-container');
2021-05-03 03:48:52 +08:00
$container->addConsumer($this->topic, [$class, 'onHandler']);
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
}
}