@@ -71,6 +71,25 @@ class Telegram
71
71
*/
72
72
protected $ commands_paths = [];
73
73
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
+
74
93
/**
75
94
* Custom commands objects
76
95
*
@@ -283,6 +302,28 @@ public function getCommandsList(): array
283
302
return $ commands ;
284
303
}
285
304
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
+
286
327
/**
287
328
* Get an object instance of the passed command
288
329
*
@@ -301,13 +342,17 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
301
342
$ this ->isAdmin () && $ which [] = 'Admin ' ;
302
343
$ which [] = 'User ' ;
303
344
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 ' ;
309
355
}
310
- $ command_class = $ command_namespace . '\\' . $ this ->ucFirstUnicode ($ command ) . 'Command ' ;
311
356
312
357
if (class_exists ($ command_class )) {
313
358
$ command_obj = new $ command_class ($ this , $ this ->update );
@@ -347,7 +392,7 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
347
392
protected function getFileNamespace (string $ src ): ?string
348
393
{
349
394
$ content = file_get_contents ($ src );
350
- if (preg_match ('#^namespace\s+(.+?);#m ' , $ content , $ m )) {
395
+ if (preg_match ('#^\s+ namespace\s+(.+?);#m ' , $ content , $ m )) {
351
396
return $ m [1 ];
352
397
}
353
398
@@ -716,6 +761,39 @@ public function isDbEnabled(): bool
716
761
return $ this ->mysql_enabled ;
717
762
}
718
763
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
+
719
797
/**
720
798
* Add a single custom commands path
721
799
*
0 commit comments