Skip to content

Commit

Permalink
CommandMetadataProvider and Configuration optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Feb 29, 2024
1 parent 5822a28 commit a47f67b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 42 deletions.
17 changes: 4 additions & 13 deletions src/CommandMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@

use Luzrain\TelegramBotBundle\Attribute\OnCommand;

final class CommandMetadataProvider
final readonly class CommandMetadataProvider
{
private array $controllerClassMap = [];

public function __construct(
private array $controllersMap,
) {
public function __construct(private array $controllers)
{
}

/**
* @return \Generator<OnCommand>
*/
public function gelMetadataList(): \Generator
{
foreach ($this->controllersMap as ['controller' => $controller]) {
if (isset($this->controllerClassMap[$controller])) {
return;
}
$this->controllerClassMap[$controller] = true;
foreach ($this->controllers as $controller) {
foreach ($this->instantiateAttributes($controller) as $attrubute) {
yield $attrubute;
}
}

$this->controllerClassMap[] = [];
}

/**
Expand Down
22 changes: 7 additions & 15 deletions src/config/compilerpass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@
return new class () implements CompilerPassInterface {
public function process(ContainerBuilder $container): void
{
$controllers = $container->findTaggedServiceIds('telegram_bot.command');
$controllers = $container->findTaggedServiceIds('telegram_bot.controller');

$controllersMap = [];
foreach ($controllers as $attributeEntries) {
foreach ($attributeEntries as $attributeEntry) {
$controllersMap[] = [
'event' => $attributeEntry['event'],
'value' => $attributeEntry['value'],
'controller' => $attributeEntry['controller'],
'priority' => $attributeEntry['priority'],
];
foreach ($controllers as $controller) {
foreach ($controller as $attributeEntry) {
$controllersMap[] = $attributeEntry;
}
}

Expand All @@ -32,14 +27,11 @@ public function process(ContainerBuilder $container): void
// Sort by priority
\usort($controllersMap, static fn(array $a, array $b) => $b['priority'] <=> $a['priority']);

foreach ($controllersMap as $id => $row) {
unset($controllersMap[$id]['priority']);
}

$container
->register('telegram_bot.controllers_locator', ServiceLocator::class)
->addTag('container.service_locator')
->setArguments([$this->referenceMap(\array_keys($controllers))])
->addTag('container.service_locator');
;

$container
->register('telegram_bot.update_handler', UpdateHandler::class)
Expand All @@ -52,7 +44,7 @@ public function process(ContainerBuilder $container): void

$container
->register('telegram_bot.command_metadata_provider', CommandMetadataProvider::class)
->setArguments([$controllersMap])
->setArguments([\array_unique(\array_column($controllersMap, 'controller'))])
;
}

Expand Down
15 changes: 7 additions & 8 deletions src/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
;

$container
->register('telegram_bot.set_webhook_command', WebhookUpdateCommand::class)
->register('telegram_bot.webhook_update_command', WebhookUpdateCommand::class)
->addTag('console.command')
->setArguments([
new Reference(BotApi::class),
Expand All @@ -66,30 +66,29 @@
;

$container
->register('telegram_bot.get_webhook_command', WebhookInfoCommand::class)
->register('telegram_bot.webhook_info_command', WebhookInfoCommand::class)
->addTag('console.command')
->setArguments([new Reference(BotApi::class)])
;

$container
->register('telegram_bot.delete_webhook_command', WebhookDeleteCommand::class)
->register('telegram_bot.webhook_delete_command', WebhookDeleteCommand::class)
->addTag('console.command')
->setArguments([new Reference(BotApi::class)])
;

$container
->register('telegram_bot.polling_command', PolllingStartCommand::class)
->register('telegram_bot.polling_start_command', PolllingStartCommand::class)
->addTag('console.command')
->setArguments([
new Reference('telegram_bot.long_polling_service'),
new Reference('telegram_bot.update_handler'),
new Reference(BotApi::class),
])

;

$container
->register('telegram_bot.menu_button_set_commands', ButtonUpdateCommand::class)
->register('telegram_bot.button_update_commands', ButtonUpdateCommand::class)
->addTag('console.command')
->setArguments([
new Reference(BotApi::class),
Expand All @@ -99,7 +98,7 @@
;

$container
->register('telegram_bot.menu_button_delete_command', ButtonDeleteCommand::class)
->register('telegram_bot.button_delete_command', ButtonDeleteCommand::class)
->addTag('console.command')
->setArguments([new Reference(BotApi::class)])
;
Expand All @@ -108,7 +107,7 @@
$controllerConfigurate = static function (ChildDefinition $definition, object $attribute, \ReflectionMethod $reflector) use ($container): void {
$value = $attribute->command ?? $attribute->callbackData ?? '';

$definition->addTag('telegram_bot.command', [
$definition->addTag('telegram_bot.controller', [
'event' => $attribute->event,
'value' => $container->getParameterBag()->resolveValue($value),
'controller' => $reflector->getDeclaringClass()->getName() . '::' . $reflector->getName(),
Expand Down
12 changes: 6 additions & 6 deletions tests/ServicesAutowiringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public function testServiceAutowiring(): void
$this->assertInstanceOf(ClientApi::class, self::$container->get('telegram_bot.client_api'));
$this->assertInstanceOf(WebHookController::class, self::$container->get('telegram_bot.webhook_controller'));
$this->assertInstanceOf(LongPollingService::class, self::$container->get('telegram_bot.long_polling_service'));
$this->assertInstanceOf(WebhookUpdateCommand::class, self::$container->get('telegram_bot.set_webhook_command'));
$this->assertInstanceOf(WebhookInfoCommand::class, self::$container->get('telegram_bot.get_webhook_command'));
$this->assertInstanceOf(WebhookDeleteCommand::class, self::$container->get('telegram_bot.delete_webhook_command'));
$this->assertInstanceOf(PolllingStartCommand::class, self::$container->get('telegram_bot.polling_command'));
$this->assertInstanceOf(ButtonUpdateCommand::class, self::$container->get('telegram_bot.menu_button_set_commands'));
$this->assertInstanceOf(ButtonDeleteCommand::class, self::$container->get('telegram_bot.menu_button_delete_command'));
$this->assertInstanceOf(WebhookUpdateCommand::class, self::$container->get('telegram_bot.webhook_update_command'));
$this->assertInstanceOf(WebhookInfoCommand::class, self::$container->get('telegram_bot.webhook_info_command'));
$this->assertInstanceOf(WebhookDeleteCommand::class, self::$container->get('telegram_bot.webhook_delete_command'));
$this->assertInstanceOf(PolllingStartCommand::class, self::$container->get('telegram_bot.polling_start_command'));
$this->assertInstanceOf(ButtonUpdateCommand::class, self::$container->get('telegram_bot.button_update_commands'));
$this->assertInstanceOf(ButtonDeleteCommand::class, self::$container->get('telegram_bot.button_delete_command'));
$this->assertInstanceOf(UpdateHandler::class, self::$container->get('telegram_bot.update_handler'));
$this->assertInstanceOf(CommandMetadataProvider::class, self::$container->get('telegram_bot.command_metadata_provider'));
}
Expand Down

0 comments on commit a47f67b

Please sign in to comment.