|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DarkDarin\TelegramBotSdk\Commands; |
| 4 | + |
| 5 | +use DarkDarin\TelegramBotSdk\DTO\Message; |
| 6 | +use DarkDarin\TelegramBotSdk\DTO\MessageEntityTypeEnum; |
| 7 | +use DarkDarin\TelegramBotSdk\Exceptions\TelegramException; |
| 8 | +use DarkDarin\TelegramBotSdk\Telegram; |
| 9 | +use DarkDarin\TelegramBotSdk\TelegramClient; |
| 10 | +use Psr\Container\ContainerExceptionInterface; |
| 11 | +use Psr\Container\ContainerInterface; |
| 12 | +use Psr\Container\NotFoundExceptionInterface; |
| 13 | + |
| 14 | +class CommandHandler implements CommandHandlerInterface |
| 15 | +{ |
| 16 | + private array $commands = []; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + private readonly Telegram $telegram, |
| 20 | + private readonly ContainerInterface $container, |
| 21 | + ) { |
| 22 | + } |
| 23 | + |
| 24 | + public function registerCommand(string $botName, object $command): void |
| 25 | + { |
| 26 | + if (!method_exists($command, 'handle')) { |
| 27 | + throw new \InvalidArgumentException('Command must have method [handle]'); |
| 28 | + } |
| 29 | + |
| 30 | + $commandWrapper = new CommandWrapper($command); |
| 31 | + $this->commands[$botName][$commandWrapper->getName()][] = $commandWrapper; |
| 32 | + foreach ($commandWrapper->getAliases() as $alias) { |
| 33 | + $this->commands[$botName][$alias][] = $commandWrapper; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @throws ContainerExceptionInterface |
| 39 | + * @throws NotFoundExceptionInterface |
| 40 | + * @throws \ReflectionException |
| 41 | + */ |
| 42 | + public function handle(string $botName, Message $message): void |
| 43 | + { |
| 44 | + $command = $this->findCommand($message); |
| 45 | + |
| 46 | + if ($command === null || empty($this->commands[$botName][$command->command])) { |
| 47 | + return; |
| 48 | + } |
| 49 | + foreach ($this->commands[$botName][$command->command] as $commandHandler) { |
| 50 | + $arguments = $this->parseArguments($commandHandler, $command); |
| 51 | + $this->handleCommand($botName, $message, $commandHandler, $arguments); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private function findCommand(Message $message): ?MessageCommand |
| 56 | + { |
| 57 | + if ($message->entities === null) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($message->entities as $entity) { |
| 62 | + if ($entity->type === MessageEntityTypeEnum::BotCommand) { |
| 63 | + $command = substr($message->text, $entity->offset + 1, $entity->length - 1); |
| 64 | + $arguments = substr($message->text, $entity->offset + $entity->length + 1); |
| 65 | + return new MessageCommand($command, $arguments); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + public function parseArguments(CommandWrapper $commandWrapper, MessageCommand $messageCommand): array |
| 73 | + { |
| 74 | + $pattern = $commandWrapper->getPattern(); |
| 75 | + [$regex, $parameters] = $this->makeRegexPattern($pattern); |
| 76 | + preg_match("%{$regex}%ixmu", $messageCommand->arguments, $matches, PREG_UNMATCHED_AS_NULL); |
| 77 | + |
| 78 | + $result = []; |
| 79 | + foreach ($matches as $key => $value) { |
| 80 | + if (in_array($key, $parameters)) { |
| 81 | + $result[$key] = $value; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return array_filter($result); |
| 86 | + } |
| 87 | + |
| 88 | + private function makeRegexPattern(string $pattern): array |
| 89 | + { |
| 90 | + preg_match_all( |
| 91 | + pattern: '#\{\s*(?<name>\w+)\s*(?::\s*(?<pattern>\S+)\s*)?}#ixmu', |
| 92 | + subject: $pattern, |
| 93 | + matches: $matches, |
| 94 | + flags: PREG_SET_ORDER |
| 95 | + ); |
| 96 | + |
| 97 | + $patterns = collect($matches) |
| 98 | + ->mapWithKeys(function ($match): array { |
| 99 | + $pattern = $match['pattern'] ?? '[^ ]++'; |
| 100 | + |
| 101 | + return [ |
| 102 | + $match['name'] => "(?<{$match['name']}>{$pattern})?", |
| 103 | + ]; |
| 104 | + }) |
| 105 | + ->filter(); |
| 106 | + |
| 107 | + return [ |
| 108 | + $patterns->implode('\s*'), |
| 109 | + $patterns->keys()->all(), |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @throws ContainerExceptionInterface |
| 115 | + * @throws NotFoundExceptionInterface |
| 116 | + * @throws \ReflectionException |
| 117 | + */ |
| 118 | + private function handleCommand(string $botName, Message $message, CommandWrapper $commandWrapper, array $arguments): void |
| 119 | + { |
| 120 | + $command = $commandWrapper->getCommand(); |
| 121 | + $parameters = $this->getMethodParameters($botName, $message, $command, $arguments); |
| 122 | + |
| 123 | + $command->handle(...$parameters); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @throws ContainerExceptionInterface |
| 128 | + * @throws NotFoundExceptionInterface |
| 129 | + * @throws \ReflectionException |
| 130 | + */ |
| 131 | + private function getMethodParameters(string $botName, Message $message, object $command, array $arguments): array |
| 132 | + { |
| 133 | + $parameters = []; |
| 134 | + $methodReflection = new \ReflectionMethod($command, 'handle'); |
| 135 | + foreach ($methodReflection->getParameters() as $parameter) { |
| 136 | + if (array_key_exists($parameter->getName(), $arguments)) { |
| 137 | + $parameters[$parameter->getName()] = $arguments[$parameter->getName()]; |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + if ($parameter->getType() instanceof \ReflectionNamedType) { |
| 142 | + $typeName = $parameter->getType()->getName(); |
| 143 | + if ($typeName === TelegramClient::class) { |
| 144 | + $parameters[$parameter->getName()] = $this->telegram->bot($botName); |
| 145 | + continue; |
| 146 | + } elseif($typeName === Message::class) { |
| 147 | + $parameters[$parameter->getName()] = $message; |
| 148 | + continue; |
| 149 | + } elseif ($this->container->has($typeName)) { |
| 150 | + $parameters[$parameter->getName()] = $this->container->get($typeName); |
| 151 | + continue; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (!$parameter->isDefaultValueAvailable()) { |
| 156 | + throw new TelegramException( |
| 157 | + sprintf( |
| 158 | + 'Command [%s] expects parameter [%s], but it not declared', |
| 159 | + get_class($command), |
| 160 | + $parameter->getName() |
| 161 | + ) |
| 162 | + ); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return $parameters; |
| 167 | + } |
| 168 | +} |
0 commit comments