Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to add commands stored in an iterable #1100

Merged
merged 1 commit into from
Jan 30, 2024

Conversation

romm
Copy link
Contributor

@romm romm commented Nov 5, 2023

Adding several commands with a single method call can be really useful, especially when working with a framework that can detect all commands instances and inject them in the command bus automatically.

This commit allows commands registration using an iterable, enhancing the possibilities to — for instance — lazy-load the commands only when the SDK API is actually called.

Below is an example of how the Api class is declared in a recent Symfony application:

return static function (ContainerConfigurator $container): void {
    $container
        ->services()

        // Tagging all commands, so they can be retrieved below
        ->instanceof(\Telegram\Bot\Commands\CommandInterface::class)
        ->tag('telegram.command')

        // Registering the `Api` class
        ->set(Api::class)
        ->args([
            '$token' => env('TELEGRAM_BOT_TOKEN')->resolve(),
        ])
        // Adding all commands that were previously tagged
        ->call('addCommands', [tagged_iterator('telegram.command')]);
};

The example above would not work without changing the type of the parameter, because the tagged_iterator is not an array.

Adding several commands with a single method call can be really useful,
especially when working with a framework that can detect all commands
instances and inject them in the command bus automatically.

This commit allows commands registration using an iterable, enhancing
the possibilities to — for instance — lazy-load the commands only when
the SDK API is actually called.

Below is an example of how the `Api` class is declared in a recent
Symfony application:

```php
return static function (ContainerConfigurator $container): void {
    $container
        ->services()

        // Tagging all commands, so they can be retrieved below
        ->instanceof(\Telegram\Bot\Commands\CommandInterface::class)
        ->tag('telegram.command')

        // Registering the `Api` class
        ->set(Api::class)
        ->args([
            '$token' => env('TELEGRAM_BOT_TOKEN')->resolve(),
        ])
        // Adding all commands that were previously tagged
        ->call('addCommands', [tagged_iterator('telegram.command')]);
};
```

The example above would not work without changing the type of the
parameter, because the `tagged_iterator` is not an array.
@irazasyed irazasyed merged commit 54cf193 into irazasyed:3.x Jan 30, 2024
@irazasyed
Copy link
Owner

Interesting change, thanks for the PR.

@romm
Copy link
Contributor Author

romm commented Feb 16, 2024

Hey there, thanks for the merge!

I've been waiting for a new release and I noticed the previous one is quite old. Would it be ok to publish a new one? No urge btw, I know that OSS can be very time-consuming. 😉

Thanks for your work on the library!

@irazasyed
Copy link
Owner

@romm Thank you for your patience. Before tagging a release, I need to test it with other changes to avoid potential issues for projects that rely on this SDK. Your understanding and patience during this time is appreciated.

@romm
Copy link
Contributor Author

romm commented Feb 16, 2024

Understood! Wishing you the best on the upcoming tests then. 🤞

@irazasyed
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants