Files
kiri-core/Annotation/Kafka.php
T

48 lines
865 B
PHP
Raw Normal View History

2021-04-11 16:22:21 +08:00
<?php
namespace Annotation;
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
{
}
/**
* @param array $handler
* @return mixed
*/
public function execute(array $handler): mixed
{
2021-04-11 17:18:54 +08:00
if (!($handler[0] instanceof ConsumerInterface)) {
return false;
}
2021-04-11 18:26:11 +08:00
/** @var TaskContainer $container */
$container = Snowflake::app()->get('kafka-container');
2021-04-11 17:18:54 +08:00
$container->addConsumer($this->topic, [$handler[0], 'onHandler']);
2021-04-11 16:22:21 +08:00
return parent::execute($handler); // TODO: Change the autogenerated stub
}
}