@@ -71,6 +71,25 @@ class Telegram
7171 */
7272 protected $ commands_paths = [];
7373
74+ /**
75+ * Custom commands class names
76+ * ```
77+ * [
78+ * 'User' => [
79+ * //commandName => className
80+ * 'start' => 'name\space\to\StartCommand',
81+ * ],
82+ * 'Admin' => [], //etc
83+ * ]
84+ * ```
85+ * @var array
86+ */
87+ protected $ commandsClasses = [
88+ 'User ' => [],
89+ 'Admin ' => [],
90+ 'System ' => [],
91+ ];
92+
7493 /**
7594 * Custom commands objects
7695 *
@@ -283,6 +302,28 @@ public function getCommandsList(): array
283302 return $ commands ;
284303 }
285304
305+ /**
306+ * Get classname of predefined commands
307+ * @see commandsClasses
308+ * @param string $auth Auth of command
309+ * @param string $command Command name
310+ *
311+ * @return string|null
312+ */
313+ private function getCommandClassName (string $ auth , string $ command ): ?string
314+ {
315+ $ command = mb_strtolower ($ command );
316+ $ auth = $ this ->ucFirstUnicode ($ auth );
317+ if (!empty ($ this ->commandsClasses [$ auth ][$ command ])) {
318+ $ className = $ this ->commandsClasses [$ auth ][$ command ];
319+ if (class_exists ($ className )){
320+ return $ className ;
321+ }
322+ }
323+
324+ return null ;
325+ }
326+
286327 /**
287328 * Get an object instance of the passed command
288329 *
@@ -301,13 +342,17 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
301342 $ this ->isAdmin () && $ which [] = 'Admin ' ;
302343 $ which [] = 'User ' ;
303344
304- foreach ($ which as $ auth ) {
305- if ($ filepath ) {
306- $ command_namespace = $ this ->getFileNamespace ($ filepath );
307- } else {
308- $ command_namespace = __NAMESPACE__ . '\\Commands \\' . $ auth . 'Commands ' ;
345+ foreach ($ which as $ auth )
346+ {
347+ if (!($ command_class = $ this ->getCommandClassName ($ auth , $ command )))
348+ {
349+ if ($ filepath ) {
350+ $ command_namespace = $ this ->getFileNamespace ($ filepath );
351+ } else {
352+ $ command_namespace = __NAMESPACE__ . '\\Commands \\' . $ auth . 'Commands ' ;
353+ }
354+ $ command_class = $ command_namespace . '\\' . $ this ->ucFirstUnicode ($ command ) . 'Command ' ;
309355 }
310- $ command_class = $ command_namespace . '\\' . $ this ->ucFirstUnicode ($ command ) . 'Command ' ;
311356
312357 if (class_exists ($ command_class )) {
313358 $ command_obj = new $ command_class ($ this , $ this ->update );
@@ -347,7 +392,7 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
347392 protected function getFileNamespace (string $ src ): ?string
348393 {
349394 $ content = file_get_contents ($ src );
350- if (preg_match ('#^namespace\s+(.+?);#m ' , $ content , $ m )) {
395+ if (preg_match ('#^\s+ namespace\s+(.+?);#m ' , $ content , $ m )) {
351396 return $ m [1 ];
352397 }
353398
@@ -716,6 +761,39 @@ public function isDbEnabled(): bool
716761 return $ this ->mysql_enabled ;
717762 }
718763
764+ /**
765+ * Add a single custom commands class
766+ *
767+ * @param string $command Set command name
768+ * @param string $className Set full classname
769+ * @param string $auth Set Auth for command. Default is User
770+ *
771+ * @return Telegram
772+ */
773+ public function addCommandsClass (string $ command , string $ className , $ auth = 'User ' ): Telegram
774+ {
775+ if (!class_exists ($ className ))
776+ {
777+ TelegramLog::error ('Command class name: " ' . $ className . '" does not exist. ' );
778+ }
779+ if (!empty ($ command ))
780+ {
781+ TelegramLog::error ('Command Name " ' . $ command . '" not set. ' );
782+ }
783+
784+ if (!is_array ($ this ->commandsClasses ))
785+ {
786+ $ this ->commandsClasses = [];
787+ }
788+
789+ $ command = mb_strtolower ($ command );
790+ $ auth = $ this ->ucFirstUnicode ($ auth );
791+
792+ $ this ->commandsClasses [$ auth ][$ command ] = $ className ;
793+
794+ return $ this ;
795+ }
796+
719797 /**
720798 * Add a single custom commands path
721799 *
0 commit comments