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
+
33
46
/**
34
47
* Version
35
48
*
@@ -86,9 +99,9 @@ class Telegram
86
99
* @var array
87
100
*/
88
101
protected $ commandsClasses = [
89
- ' User ' => [],
90
- ' Admin ' => [],
91
- ' System ' => [],
102
+ self :: AUTH_USER => [],
103
+ self :: AUTH_ADMIN => [],
104
+ self :: AUTH_SYSTEM => [],
92
105
];
93
106
94
107
/**
@@ -340,9 +353,9 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
340
353
return $ this ->commands_objects [$ command ];
341
354
}
342
355
343
- $ which = [' System ' ];
344
- $ this ->isAdmin () && $ which [] = ' Admin ' ;
345
- $ which [] = ' User ' ;
356
+ $ which = [self :: AUTH_SYSTEM ];
357
+ $ this ->isAdmin () && $ which [] = self :: AUTH_ADMIN ;
358
+ $ which [] = self :: AUTH_USER ;
346
359
347
360
foreach ($ which as $ auth )
348
361
{
@@ -360,19 +373,19 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
360
373
$ command_obj = new $ command_class ($ this , $ this ->update );
361
374
362
375
switch ($ auth ) {
363
- case ' System ' :
376
+ case self :: AUTH_SYSTEM :
364
377
if ($ command_obj instanceof SystemCommand) {
365
378
return $ command_obj ;
366
379
}
367
380
break ;
368
381
369
- case ' Admin ' :
382
+ case self :: AUTH_ADMIN :
370
383
if ($ command_obj instanceof AdminCommand) {
371
384
return $ command_obj ;
372
385
}
373
386
break ;
374
387
375
- case ' User ' :
388
+ case self :: AUTH_USER :
376
389
if ($ command_obj instanceof UserCommand) {
377
390
return $ command_obj ;
378
391
}
@@ -766,36 +779,48 @@ public function isDbEnabled(): bool
766
779
/**
767
780
* Add a single custom commands class
768
781
*
769
- * @param string $command Set command name
770
782
* @param string $className Set full classname
771
- * @param string $auth Set Auth for command. Default is User
772
- *
773
783
* @return Telegram
774
784
*/
775
- public function addCommandsClass (string $ command , string $ className, $ auth = ' User ' ): Telegram
785
+ public function addCommandsClass (string $ className ): Telegram
776
786
{
777
- if (!class_exists ($ className ))
787
+ if (!$ className || ! class_exists ($ className ))
778
788
{
779
789
$ error = 'Command class name: " ' . $ className . '" does not exist. ' ;
780
790
TelegramLog::error ($ error );
781
791
throw new InvalidArgumentException ($ error );
782
792
}
783
- if (empty ($ command ))
784
- {
785
- $ error = 'Command Name " ' . $ command . '" not set. ' ;
786
- TelegramLog::error ($ error );
787
- throw new InvalidArgumentException ($ error );
788
- }
789
793
790
794
if (!is_array ($ this ->commandsClasses ))
791
795
{
792
796
$ this ->commandsClasses = [];
793
797
}
794
798
795
- $ command = mb_strtolower ($ command );
796
- $ auth = $ this ->ucFirstUnicode ($ auth );
799
+ if (!is_a ($ className , Command::class, true )) {
800
+ $ error = 'Command class is not a base command class ' ;
801
+ TelegramLog::error ($ error );
802
+ throw new InvalidArgumentException ($ error );
803
+ }
797
804
798
- $ this ->commandsClasses [$ auth ][$ command ] = $ className ;
805
+ $ commandObject = new $ className ($ this );
806
+
807
+ $ command = $ commandObject ->getName ();
808
+ $ auth = null ;
809
+ switch (true ) {
810
+ case $ commandObject ->isSystemCommand ():
811
+ $ auth = self ::AUTH_SYSTEM ;
812
+ break ;
813
+ case $ commandObject ->isAdminCommand ():
814
+ $ auth = self ::AUTH_ADMIN ;
815
+ break ;
816
+ case $ commandObject ->isUserCommand ():
817
+ $ auth = self ::AUTH_USER ;
818
+ break ;
819
+ }
820
+
821
+ if ($ auth ) {
822
+ $ this ->commandsClasses [$ auth ][$ command ] = $ className ;
823
+ }
799
824
800
825
return $ this ;
801
826
}
0 commit comments