Files
kiri-core/Kafka/TaskProvider.php
T

58 lines
888 B
PHP
Raw Normal View History

2021-04-11 17:18:54 +08:00
<?php
namespace Kafka;
use Snowflake\Abstracts\BaseObject;
/**
2021-08-04 14:37:12 +08:00
* Class TaskProvider
2021-04-11 17:18:54 +08:00
* @package Kafka
*/
2021-08-04 14:37:12 +08:00
class TaskProvider extends BaseObject
2021-04-11 17:18:54 +08:00
{
private array $_topics = [];
/**
* @param $topic
* @param $handler
*/
public function addConsumer($topic, $handler)
{
if (isset($this->_topics[$topic])) {
return;
}
$this->_topics[$topic] = $handler;
}
2021-08-04 14:33:39 +08:00
/**
* @param string $topic
* @return mixed
*/
public function getConsumer(string $topic): mixed
2021-04-11 19:02:42 +08:00
{
return $this->_topics[$topic] ?? null;
}
2021-04-11 17:18:54 +08:00
/**
* @param $topic
2021-08-04 14:37:12 +08:00
* @param Struct $struct
2021-04-11 17:18:54 +08:00
*/
public function process($topic, Struct $struct)
{
$handler = $this->_topics[$topic] ?? null;
if (empty($handler)) {
return;
}
call_user_func($handler, $struct);
}
}