30
30
31
31
class Telegram
32
32
{
33
- /**
34
- * Auth name for user commands
35
- */
36
- const AUTH_USER = 'User ' ;
37
- /**
38
- * Auth name tof system commands
39
- */
40
- const AUTH_SYSTEM = 'System ' ;
41
- /**
42
- * Auth name for admin commands
43
- */
44
- const AUTH_ADMIN = 'Admin ' ;
45
-
46
33
/**
47
34
* Version
48
35
*
@@ -86,22 +73,23 @@ class Telegram
86
73
protected $ commands_paths = [];
87
74
88
75
/**
89
- * Custom commands class names
76
+ * Custom command class names
90
77
* ```
91
78
* [
92
- * 'User' => [
93
- * //commandName => className
94
- * 'start' => 'name\space\to\StartCommand',
95
- * ],
96
- * 'Admin' => [], //etc
79
+ * 'User' => [
80
+ * // command_name => command_class
81
+ * 'start' => 'name\space\to\StartCommand',
82
+ * ],
83
+ * 'Admin' => [], //etc
97
84
* ]
98
85
* ```
86
+ *
99
87
* @var array
100
88
*/
101
- protected $ commandsClasses = [
102
- self ::AUTH_USER => [],
103
- self ::AUTH_ADMIN => [],
104
- self ::AUTH_SYSTEM => [],
89
+ protected $ command_classes = [
90
+ Command ::AUTH_USER => [],
91
+ Command ::AUTH_ADMIN => [],
92
+ Command ::AUTH_SYSTEM => [],
105
93
];
106
94
107
95
/**
@@ -318,22 +306,37 @@ public function getCommandsList(): array
318
306
319
307
/**
320
308
* Get classname of predefined commands
321
- * @see commandsClasses
322
- * @param string $auth Auth of command
323
- * @param string $command Command name
309
+ *
310
+ * @see command_classes
311
+ *
312
+ * @param string $auth Auth of command
313
+ * @param string $command Command name
314
+ * @param string $filepath Path to the command file
324
315
*
325
316
* @return string|null
326
317
*/
327
- public function getCommandClassName (string $ auth , string $ command ): ?string
318
+ public function getCommandClassName (string $ auth , string $ command, string $ filepath = '' ): ?string
328
319
{
329
320
$ command = mb_strtolower ($ command );
330
- $ auth = $ this ->ucFirstUnicode ($ auth );
321
+ $ auth = $ this ->ucFirstUnicode ($ auth );
331
322
332
- if (!empty ($ this ->commandsClasses [$ auth ][$ command ])) {
333
- $ className = $ this ->commandsClasses [$ auth ][$ command ];
334
- if (class_exists ($ className )){
335
- return $ className ;
336
- }
323
+ // First, check for directly assigned command class.
324
+ if ($ command_class = $ this ->command_classes [$ auth ][$ command ] ?? null ) {
325
+ return $ command_class ;
326
+ }
327
+
328
+ // Start with default namespace.
329
+ $ command_namespace = __NAMESPACE__ . '\\Commands \\' . $ auth . 'Commands ' ;
330
+
331
+ // Check if we can get the namespace from the file (if passed).
332
+ if ($ filepath && !($ command_namespace = $ this ->getFileNamespace ($ filepath ))) {
333
+ return null ;
334
+ }
335
+
336
+ $ command_class = $ command_namespace . '\\' . $ this ->ucFirstUnicode ($ command ) . 'Command ' ;
337
+
338
+ if (class_exists ($ command_class )) {
339
+ return $ command_class ;
337
340
}
338
341
339
342
return null ;
@@ -353,43 +356,24 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
353
356
return $ this ->commands_objects [$ command ];
354
357
}
355
358
356
- $ which = [self ::AUTH_SYSTEM ];
357
- $ this ->isAdmin () && $ which [] = self ::AUTH_ADMIN ;
358
- $ which [] = self ::AUTH_USER ;
359
-
360
- foreach ($ which as $ auth )
361
- {
362
- if (!($ command_class = $ this ->getCommandClassName ($ auth , $ command )))
363
- {
364
- if ($ filepath ) {
365
- $ command_namespace = $ this ->getFileNamespace ($ filepath );
366
- } else {
367
- $ command_namespace = __NAMESPACE__ . '\\Commands \\' . $ auth . 'Commands ' ;
368
- }
369
- $ command_class = $ command_namespace . '\\' . $ this ->ucFirstUnicode ($ command ) . 'Command ' ;
370
- }
359
+ $ which = [Command::AUTH_SYSTEM ];
360
+ $ this ->isAdmin () && $ which [] = Command::AUTH_ADMIN ;
361
+ $ which [] = Command::AUTH_USER ;
362
+
363
+ foreach ($ which as $ auth ) {
364
+ $ command_class = $ this ->getCommandClassName ($ auth , $ command , $ filepath );
371
365
372
- if (class_exists ( $ command_class) ) {
366
+ if ($ command_class ) {
373
367
$ command_obj = new $ command_class ($ this , $ this ->update );
374
368
375
- switch ($ auth ) {
376
- case self ::AUTH_SYSTEM :
377
- if ($ command_obj instanceof SystemCommand) {
378
- return $ command_obj ;
379
- }
380
- break ;
381
-
382
- case self ::AUTH_ADMIN :
383
- if ($ command_obj instanceof AdminCommand) {
384
- return $ command_obj ;
385
- }
386
- break ;
387
-
388
- case self ::AUTH_USER :
389
- if ($ command_obj instanceof UserCommand) {
390
- return $ command_obj ;
391
- }
392
- break ;
369
+ if ($ auth === Command::AUTH_SYSTEM && $ command_obj instanceof SystemCommand) {
370
+ return $ command_obj ;
371
+ }
372
+ if ($ auth === Command::AUTH_ADMIN && $ command_obj instanceof AdminCommand) {
373
+ return $ command_obj ;
374
+ }
375
+ if ($ auth === Command::AUTH_USER && $ command_obj instanceof UserCommand) {
376
+ return $ command_obj ;
393
377
}
394
378
}
395
379
}
@@ -605,7 +589,7 @@ public function processUpdate(Update $update): ServerResponse
605
589
$ reason = 'Update denied by update_filter ' ;
606
590
try {
607
591
$ allowed = (bool ) call_user_func_array ($ this ->update_filter , [$ update , $ this , &$ reason ]);
608
- } catch (\ Exception $ e ) {
592
+ } catch (Exception $ e ) {
609
593
$ allowed = false ;
610
594
}
611
595
@@ -793,49 +777,54 @@ public function isDbEnabled(): bool
793
777
}
794
778
795
779
/**
796
- * Add a single custom commands class
780
+ * Add a single custom command class
781
+ *
782
+ * @param string $command_class Full command class name
797
783
*
798
- * @param string $className Set full classname
799
784
* @return Telegram
800
785
*/
801
- public function addCommandsClass (string $ className ): Telegram
786
+ public function addCommandClass (string $ command_class ): Telegram
802
787
{
803
- if (!$ className || !class_exists ($ className ))
804
- {
805
- $ error = 'Command class name: " ' . $ className . '" does not exist. ' ;
788
+ if (!$ command_class || !class_exists ($ command_class )) {
789
+ $ error = sprintf ('Command class "%s" does not exist. ' , $ command_class );
806
790
TelegramLog::error ($ error );
807
791
throw new InvalidArgumentException ($ error );
808
792
}
809
793
810
- if (!is_array ($ this ->commandsClasses ))
811
- {
812
- $ this ->commandsClasses = [];
813
- }
814
-
815
- if (!is_a ($ className , Command::class, true )) {
816
- $ error = 'Command class is not a base command class ' ;
794
+ if (!is_a ($ command_class , Command::class, true )) {
795
+ $ error = sprintf ('Command class "%s" does not extend "%s". ' , $ command_class , Command::class);
817
796
TelegramLog::error ($ error );
818
797
throw new InvalidArgumentException ($ error );
819
798
}
820
799
821
- $ commandObject = new $ className ($ this );
800
+ // Dummy object to get data from.
801
+ $ command_object = new $ command_class ($ this );
822
802
823
- $ command = $ commandObject ->getName ();
824
803
$ auth = null ;
825
- switch (true ) {
826
- case $ commandObject ->isSystemCommand ():
827
- $ auth = self ::AUTH_SYSTEM ;
828
- break ;
829
- case $ commandObject ->isAdminCommand ():
830
- $ auth = self ::AUTH_ADMIN ;
831
- break ;
832
- case $ commandObject ->isUserCommand ():
833
- $ auth = self ::AUTH_USER ;
834
- break ;
835
- }
804
+ $ command_object ->isSystemCommand () && $ auth = Command::AUTH_SYSTEM ;
805
+ $ command_object ->isAdminCommand () && $ auth = Command::AUTH_ADMIN ;
806
+ $ command_object ->isUserCommand () && $ auth = Command::AUTH_USER ;
836
807
837
808
if ($ auth ) {
838
- $ this ->commandsClasses [$ auth ][$ command ] = $ className ;
809
+ $ command = mb_strtolower ($ command_object ->getName ());
810
+
811
+ $ this ->command_classes [$ auth ][$ command ] = $ command_class ;
812
+ }
813
+
814
+ return $ this ;
815
+ }
816
+
817
+ /**
818
+ * Add multiple custom command classes
819
+ *
820
+ * @param array $command_classes List of full command class names
821
+ *
822
+ * @return Telegram
823
+ */
824
+ public function addCommandClasses (array $ command_classes ): Telegram
825
+ {
826
+ foreach ($ command_classes as $ command_class ) {
827
+ $ this ->addCommandClass ($ command_class );
839
828
}
840
829
841
830
return $ this ;
@@ -892,13 +881,13 @@ public function getCommandsPaths(): array
892
881
}
893
882
894
883
/**
895
- * Return the list of commands classes
884
+ * Return the list of command classes
896
885
*
897
886
* @return array
898
887
*/
899
- public function getCommandsClasses (): array
888
+ public function getCommandClasses (): array
900
889
{
901
- return $ this ->commandsClasses ;
890
+ return $ this ->command_classes ;
902
891
}
903
892
904
893
/**
0 commit comments