Files
kiri-core/Annotation/Kafka.php
T
as2252258@163.com 8137b24865 modify
2021-05-03 03:48:52 +08:00

48 lines
815 B
PHP

<?php
namespace Annotation;
use Kafka\ConsumerInterface;
use Kafka\TaskContainer;
use Snowflake\Snowflake;
/**
* Class Kafka
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class Kafka extends Attribute
{
/**
* Kafka constructor.
* @param string $topic
*/
public function __construct(public string $topic)
{
}
/**
* @param array $handler
* @return mixed
*/
public function execute(mixed $class, mixed $method = null): mixed
{
if (!($class instanceof ConsumerInterface)) {
return false;
}
/** @var TaskContainer $container */
$container = Snowflake::app()->get('kafka-container');
$container->addConsumer($this->topic, [$class, 'onHandler']);
return true;
}
}