From f91f9ff01a3394446946a171791c3f589854f396 Mon Sep 17 00:00:00 2001 From: jonybang Date: Wed, 1 Nov 2017 23:43:37 +0200 Subject: [PATCH] issue #323 modify getCommandObject and executeCommand for possibility to use custom namespaces in commands --- src/Telegram.php | 62 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/src/Telegram.php b/src/Telegram.php index 26ca07533..ed15b8f02 100644 --- a/src/Telegram.php +++ b/src/Telegram.php @@ -14,7 +14,10 @@ define('BASE_COMMANDS_PATH', BASE_PATH . '/Commands'); use Exception; +use Longman\TelegramBot\Commands\AdminCommand; use Longman\TelegramBot\Commands\Command; +use Longman\TelegramBot\Commands\SystemCommand; +use Longman\TelegramBot\Commands\UserCommand; use Longman\TelegramBot\Entities\ServerResponse; use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Exception\TelegramException; @@ -67,6 +70,13 @@ class Telegram */ protected $commands_paths = []; + /** + * Custom commands objects + * + * @var array + */ + protected $commands_objects = []; + /** * Current Update object * @@ -235,7 +245,7 @@ public function getCommandsList() require_once $file->getPathname(); - $command_obj = $this->getCommandObject($command); + $command_obj = $this->getCommandObject($command, $file->getPathname()); if ($command_obj instanceof Command) { $commands[$command_name] = $command_obj; } @@ -255,22 +265,59 @@ public function getCommandsList() * * @return \Longman\TelegramBot\Commands\Command|null */ - public function getCommandObject($command) + public function getCommandObject($command, $filepath = null) { $which = ['System']; $this->isAdmin() && $which[] = 'Admin'; $which[] = 'User'; foreach ($which as $auth) { - $command_namespace = __NAMESPACE__ . '\\Commands\\' . $auth . 'Commands\\' . $this->ucfirstUnicode($command) . 'Command'; + if($filepath){ + $command_namespace = $this->getFileNamespace($filepath) . '\\' . $this->ucfirstUnicode($command) . 'Command'; + } else { + $command_namespace = __NAMESPACE__ . '\\Commands\\' . $auth . 'Commands\\' . $this->ucfirstUnicode($command) . 'Command'; + } if (class_exists($command_namespace)) { - return new $command_namespace($this, $this->update); + $command_obj = new $command_namespace($this, $this->update); + + switch ($auth){ + case 'System': + if($command_obj instanceof SystemCommand) + return $command_obj; + break; + + case 'Admin': + if($command_obj instanceof AdminCommand) + return $command_obj; + break; + + case 'User': + if($command_obj instanceof UserCommand) + return $command_obj; + break; + } + } } return null; } + /** + * Get namespace from php file by src path + * + * @param string $src (absolute path to file) + * + * @return string ("Longman\TelegramBot\Commands\SystemCommands" for example) + */ + function getFileNamespace($src) { + $content = file_get_contents($src); + if (preg_match('#^namespace\s+(.+?);$#sm', $content, $m)) { + return $m[1]; + } + return null; + } + /** * Set custom input string for debug purposes * @@ -455,7 +502,7 @@ public function processUpdate(Update $update) //Make sure we have an up-to-date command list //This is necessary to "require" all the necessary command files! - $this->getCommandsList(); + $this->commands_objects = $this->getCommandsList(); DB::insertRequest($this->update); @@ -473,7 +520,10 @@ public function processUpdate(Update $update) public function executeCommand($command) { $command = strtolower($command); - $command_obj = $this->getCommandObject($command); + $command_obj = isset($this->commands_objects[$command]) ? $this->commands_objects[$command] : null; + + if(!$command_obj) + $command_obj = $this->getCommandObject($command); if (!$command_obj || !$command_obj->isEnabled()) { //Failsafe in case the Generic command can't be found