diff --git a/src/Attributes/AbstractTopicLevelAttribute.php b/src/Attributes/AbstractTopicLevelAttribute.php new file mode 100644 index 0000000..b04f486 --- /dev/null +++ b/src/Attributes/AbstractTopicLevelAttribute.php @@ -0,0 +1,11 @@ +router = $router; $this->token = $token; $this->botApi = $bot_api; $this->chatId = $chat_id; $this->topicId = $topic_id; + $this->topicsLevel = $topics_level; $this->level = $level; $this->bubble = $bubble; $this->proxy = $proxy; @@ -76,12 +88,14 @@ public function __construct( /** * @inheritDoc + * @throws \ReflectionException */ protected function write($record): void { + $topicId = $this->getTopicByAttribute(); $token = $record['context']['token'] ?? null; $chatId = $record['context']['chat_id'] ?? null; - $topicId = $record['context']['topic_id'] ?? null; + $topicId = $topicId ?? $record['context']['topic_id'] ?? null; $this->send($record['formatted'], $token, $chatId, $topicId); } @@ -141,6 +155,41 @@ protected function send(string $message, $token = null, $chatId = null, $topicId } } + + protected function getTopicByAttribute(): string|null + { + $route = Route::current(); + if ($route == null) { + return null; + } + + $action = $route->getAction(); + + if (!isset($action['controller'])) { + return null; + } + + try { + + [$controller, $method] = explode('@', $action['controller']); + $reflectionMethod = new ReflectionMethod($controller, $method); + + $attributes = $reflectionMethod->getAttributes(); + + if (empty($attributes)) { + return null; + } + + /** @var TopicLogInterface $notifyException */ + $notifyException = $attributes[0]->newInstance(); + + return $notifyException->getTopicId($this->topicsLevel); + + } catch (\Throwable) { + return null; + } + } + public function setToken(string $token): static { $this->token = $token;