Skip to content

PSR4 command classes include #1207

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

Merged
merged 3 commits into from
Apr 9, 2021
Merged

Conversation

sedovserge
Copy link
Contributor

@sedovserge sedovserge commented Apr 3, 2021

? !
Type bug + feature
BC Break yes
Fixed issues 1205

Summary

Add support to include commands classes via PSR4, and old method (pathes) works too.
Fix regex for search 'namespace'
Add tests.

@noplanman
Copy link
Member

Thanks @Alligator77, will check this out in the coming days!

@noplanman
Copy link
Member

Hi @Alligator77, thanks for the PR!

A few things:

The command name and auth should be inferred from the command class, as they are defined there already. You'll need to check that the passed class correctly extends Longman\TelegramBot\Commands\Command and from there you can create a new dummy object to get the extra info:

$class_name = 'What\\Ever\\MyCommand'; // as an example

if (!is_a($class_name, Command::class)) {
    // return, as not the correct type...
}

$command_object = new $class_name($this); // "$this" here is the instance of Telegram

// Get the name from the command object itself.
$name = $command_object->getName();

You can figure out the $auth by checking the Command::is...Command() methods, like $command_object->isSystemCommand().

$auth = null;
switch (true) {
    case $command_object->isSystemCommand():
        $auth = 'System';
        break;
    case $command_object->isAdminCommand():
        $auth = 'Admin';
        break;
    case $command_object->isUserCommand():
        $auth = 'User';
        break;
}

if ($auth) {
    $this->commandsClasses[$auth][$command] = $className;
}

This will cut down on superfluous data required to be entered by the user, as it's there already.

Hope you know what I mean 😃

Optimize addCommandsClass method
Add constants for auth names
Update tests
@sedovserge
Copy link
Contributor Author

Fair enough. I was not aware of the implementation of these methods.

@noplanman noplanman merged commit 6b401cb into php-telegram-bot:develop Apr 9, 2021
@noplanman
Copy link
Member

Thanks @Alligator77 👍

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