Skip to content

Commit d78e261

Browse files
authored
Merge pull request #689 from Jonybang/custom_namespace
issue #323 modify getCommandObject and executeCommand for possibility…
2 parents 808c423 + bfd9513 commit d78e261

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1616
- `TelegramLog::$always_log_request_and_response` parameter to force output of the request and response data to the debug log, also for successful requests
1717
### Changed
1818
- [:exclamation:][unreleased-bc-static-method-entityescapemarkdown] Made `Entity::escapeMarkdown` static, to not require an `Entity` object.
19+
- Allow custom namespacing for commands. (@Jonybang)
1920
### Deprecated
2021
### Removed
2122
### Fixed

src/Telegram.php

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
defined('TB_BASE_COMMANDS_PATH') || define('TB_BASE_COMMANDS_PATH', TB_BASE_PATH . '/Commands');
1616

1717
use Exception;
18+
use Longman\TelegramBot\Commands\AdminCommand;
1819
use Longman\TelegramBot\Commands\Command;
20+
use Longman\TelegramBot\Commands\SystemCommand;
21+
use Longman\TelegramBot\Commands\UserCommand;
1922
use Longman\TelegramBot\Entities\ServerResponse;
2023
use Longman\TelegramBot\Entities\Update;
2124
use Longman\TelegramBot\Exception\TelegramException;
@@ -68,6 +71,13 @@ class Telegram
6871
*/
6972
protected $commands_paths = [];
7073

74+
/**
75+
* Custom commands objects
76+
*
77+
* @var array
78+
*/
79+
protected $commands_objects = [];
80+
7181
/**
7282
* Current Update object
7383
*
@@ -262,7 +272,7 @@ public function getCommandsList()
262272

263273
require_once $file->getPathname();
264274

265-
$command_obj = $this->getCommandObject($command);
275+
$command_obj = $this->getCommandObject($command, $file->getPathname());
266276
if ($command_obj instanceof Command) {
267277
$commands[$command_name] = $command_obj;
268278
}
@@ -279,25 +289,68 @@ public function getCommandsList()
279289
* Get an object instance of the passed command
280290
*
281291
* @param string $command
292+
* @param string $filepath
282293
*
283294
* @return Command|null
284295
*/
285-
public function getCommandObject($command)
296+
public function getCommandObject($command, $filepath = null)
286297
{
287298
$which = ['System'];
288299
$this->isAdmin() && $which[] = 'Admin';
289300
$which[] = 'User';
290301

291302
foreach ($which as $auth) {
292-
$command_namespace = __NAMESPACE__ . '\\Commands\\' . $auth . 'Commands\\' . $this->ucfirstUnicode($command) . 'Command';
293-
if (class_exists($command_namespace)) {
294-
return new $command_namespace($this, $this->update);
303+
if ($filepath) {
304+
$command_namespace = $this->getFileNamespace($filepath);
305+
} else {
306+
$command_namespace = __NAMESPACE__ . '\\Commands\\' . $auth . 'Commands';
307+
}
308+
$command_class = $command_namespace . '\\' . $this->ucfirstUnicode($command) . 'Command';
309+
310+
if (class_exists($command_class)) {
311+
$command_obj = new $command_class($this, $this->update);
312+
313+
switch ($auth) {
314+
case 'System':
315+
if ($command_obj instanceof SystemCommand) {
316+
return $command_obj;
317+
}
318+
break;
319+
320+
case 'Admin':
321+
if ($command_obj instanceof AdminCommand) {
322+
return $command_obj;
323+
}
324+
break;
325+
326+
case 'User':
327+
if ($command_obj instanceof UserCommand) {
328+
return $command_obj;
329+
}
330+
break;
331+
}
295332
}
296333
}
297334

298335
return null;
299336
}
300337

338+
/**
339+
* Get namespace from php file by src path
340+
*
341+
* @param string $src (absolute path to file)
342+
*
343+
* @return string ("Longman\TelegramBot\Commands\SystemCommands" for example)
344+
*/
345+
protected function getFileNamespace($src)
346+
{
347+
$content = file_get_contents($src);
348+
if (preg_match('#^namespace\s+(.+?);$#sm', $content, $m)) {
349+
return $m[1];
350+
}
351+
return null;
352+
}
353+
301354
/**
302355
* Set custom input string for debug purposes
303356
*
@@ -484,7 +537,7 @@ public function processUpdate(Update $update)
484537

485538
//Make sure we have an up-to-date command list
486539
//This is necessary to "require" all the necessary command files!
487-
$this->getCommandsList();
540+
$this->commands_objects = $this->getCommandsList();
488541

489542
//If all else fails, it's a generic message.
490543
$command = self::GENERIC_MESSAGE_COMMAND;
@@ -533,8 +586,13 @@ public function processUpdate(Update $update)
533586
*/
534587
public function executeCommand($command)
535588
{
536-
$command = mb_strtolower($command);
537-
$command_obj = $this->getCommandObject($command);
589+
$command = mb_strtolower($command);
590+
591+
if (isset($this->commands_objects[$command])) {
592+
$command_obj = $this->commands_objects[$command];
593+
} else {
594+
$command_obj = $this->getCommandObject($command);
595+
}
538596

539597
if (!$command_obj || !$command_obj->isEnabled()) {
540598
//Failsafe in case the Generic command can't be found
@@ -907,7 +965,7 @@ protected function ucwordsUnicode($str, $encoding = 'UTF-8')
907965
protected function ucfirstUnicode($str, $encoding = 'UTF-8')
908966
{
909967
return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding)
910-
. mb_strtolower(mb_substr($str, 1, mb_strlen($str), $encoding), $encoding);
968+
. mb_strtolower(mb_substr($str, 1, mb_strlen($str), $encoding), $encoding);
911969
}
912970

913971
/**

0 commit comments

Comments
 (0)