Skip to content

Allow to set Custom namespace for commands, also #919

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class Telegram
*/
protected $input;

/**
* Custom commands namespace
*
* @var string
*/
protected $commands_namespace = '';

/**
* Custom commands paths
*
Expand Down Expand Up @@ -177,6 +184,7 @@ public function __construct($api_key, $bot_username = '')
}

//Add default system commands path
$this->commands_namespace = __NAMESPACE__ . '\\Commands\\';
$this->addCommandsPath(TB_BASE_COMMANDS_PATH . '/SystemCommands');

Request::initialize($this);
Expand Down Expand Up @@ -219,6 +227,19 @@ public function enableExternalMySql($external_pdo_connection, $table_prefix = nu
return $this;
}

/**
* Set Custom Namespace
*
* @param string $namespace
* @return \Longman\TelegramBot\Telegram
*/
public function setCommandsNamespace(string $namespace)
{
$this->commands_namespace = $namespace;

return $this;
}

/**
* Get commands list
*
Expand Down Expand Up @@ -277,7 +298,7 @@ public function getCommandObject($command)
$which[] = 'User';

foreach ($which as $auth) {
$command_namespace = __NAMESPACE__ . '\\Commands\\' . $auth . 'Commands\\' . $this->ucfirstUnicode($command) . 'Command';
$command_namespace = $this->commands_namespace . $auth . 'Commands\\' . $this->ucfirstUnicode($command) . 'Command';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd vote for having this as $this->commands_namespace . '\\' . $auth . 'Commands\\', to not require the final backslashes when setting the namespace.

if (class_exists($command_namespace)) {
return new $command_namespace($this, $this->update);
}
Expand Down Expand Up @@ -649,6 +670,19 @@ public function isDbEnabled()
}
}

/**
* Set commands path to given array
*
* @param array $paths Custom commands paths to set
* @return \Longman\TelegramBot\Telegram
*/
public function setCommandsPath(array $paths)
{
$this->commands_paths = $paths;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imami Best to set as an empty array and then use the existing addCommandsPaths method to add paths. Just to ensure the reuse of code.


return $this;
}

/**
* Add a single custom commands path
*
Expand Down Expand Up @@ -924,6 +958,7 @@ public function enableBotan($token, array $options = [])
*
* @param array $options
*
* @throws TelegramException
* @return \Longman\TelegramBot\Telegram
*/
public function enableLimiter(array $options = [])
Expand Down