15
15
defined ('TB_BASE_COMMANDS_PATH ' ) || define ('TB_BASE_COMMANDS_PATH ' , TB_BASE_PATH . '/Commands ' );
16
16
17
17
use Exception ;
18
+ use Longman \TelegramBot \Commands \AdminCommand ;
18
19
use Longman \TelegramBot \Commands \Command ;
20
+ use Longman \TelegramBot \Commands \SystemCommand ;
21
+ use Longman \TelegramBot \Commands \UserCommand ;
19
22
use Longman \TelegramBot \Entities \ServerResponse ;
20
23
use Longman \TelegramBot \Entities \Update ;
21
24
use Longman \TelegramBot \Exception \TelegramException ;
@@ -68,6 +71,13 @@ class Telegram
68
71
*/
69
72
protected $ commands_paths = [];
70
73
74
+ /**
75
+ * Custom commands objects
76
+ *
77
+ * @var array
78
+ */
79
+ protected $ commands_objects = [];
80
+
71
81
/**
72
82
* Current Update object
73
83
*
@@ -262,7 +272,7 @@ public function getCommandsList()
262
272
263
273
require_once $ file ->getPathname ();
264
274
265
- $ command_obj = $ this ->getCommandObject ($ command );
275
+ $ command_obj = $ this ->getCommandObject ($ command, $ file -> getPathname () );
266
276
if ($ command_obj instanceof Command) {
267
277
$ commands [$ command_name ] = $ command_obj ;
268
278
}
@@ -279,25 +289,68 @@ public function getCommandsList()
279
289
* Get an object instance of the passed command
280
290
*
281
291
* @param string $command
292
+ * @param string $filepath
282
293
*
283
294
* @return Command|null
284
295
*/
285
- public function getCommandObject ($ command )
296
+ public function getCommandObject ($ command, $ filepath = null )
286
297
{
287
298
$ which = ['System ' ];
288
299
$ this ->isAdmin () && $ which [] = 'Admin ' ;
289
300
$ which [] = 'User ' ;
290
301
291
302
foreach ($ which as $ auth ) {
292
- $ command_namespace = __NAMESPACE__ . '\\Commands \\' . $ auth . 'Commands \\' . $ this ->ucfirstUnicode ($ command ) . 'Command ' ;
293
- if (class_exists ($ command_namespace )) {
294
- return new $ command_namespace ($ this , $ this ->update );
303
+ if ($ filepath ) {
304
+ $ command_namespace = $ this ->getFileNamespace ($ filepath );
305
+ } else {
306
+ $ command_namespace = __NAMESPACE__ . '\\Commands \\' . $ auth . 'Commands ' ;
307
+ }
308
+ $ command_class = $ command_namespace . '\\' . $ this ->ucfirstUnicode ($ command ) . 'Command ' ;
309
+
310
+ if (class_exists ($ command_class )) {
311
+ $ command_obj = new $ command_class ($ this , $ this ->update );
312
+
313
+ switch ($ auth ) {
314
+ case 'System ' :
315
+ if ($ command_obj instanceof SystemCommand) {
316
+ return $ command_obj ;
317
+ }
318
+ break ;
319
+
320
+ case 'Admin ' :
321
+ if ($ command_obj instanceof AdminCommand) {
322
+ return $ command_obj ;
323
+ }
324
+ break ;
325
+
326
+ case 'User ' :
327
+ if ($ command_obj instanceof UserCommand) {
328
+ return $ command_obj ;
329
+ }
330
+ break ;
331
+ }
295
332
}
296
333
}
297
334
298
335
return null ;
299
336
}
300
337
338
+ /**
339
+ * Get namespace from php file by src path
340
+ *
341
+ * @param string $src (absolute path to file)
342
+ *
343
+ * @return string ("Longman\TelegramBot\Commands\SystemCommands" for example)
344
+ */
345
+ protected function getFileNamespace ($ src )
346
+ {
347
+ $ content = file_get_contents ($ src );
348
+ if (preg_match ('#^namespace\s+(.+?);$#sm ' , $ content , $ m )) {
349
+ return $ m [1 ];
350
+ }
351
+ return null ;
352
+ }
353
+
301
354
/**
302
355
* Set custom input string for debug purposes
303
356
*
@@ -484,7 +537,7 @@ public function processUpdate(Update $update)
484
537
485
538
//Make sure we have an up-to-date command list
486
539
//This is necessary to "require" all the necessary command files!
487
- $ this ->getCommandsList ();
540
+ $ this ->commands_objects = $ this -> getCommandsList ();
488
541
489
542
//If all else fails, it's a generic message.
490
543
$ command = self ::GENERIC_MESSAGE_COMMAND ;
@@ -533,8 +586,13 @@ public function processUpdate(Update $update)
533
586
*/
534
587
public function executeCommand ($ command )
535
588
{
536
- $ command = mb_strtolower ($ command );
537
- $ command_obj = $ this ->getCommandObject ($ command );
589
+ $ command = mb_strtolower ($ command );
590
+
591
+ if (isset ($ this ->commands_objects [$ command ])) {
592
+ $ command_obj = $ this ->commands_objects [$ command ];
593
+ } else {
594
+ $ command_obj = $ this ->getCommandObject ($ command );
595
+ }
538
596
539
597
if (!$ command_obj || !$ command_obj ->isEnabled ()) {
540
598
//Failsafe in case the Generic command can't be found
@@ -907,7 +965,7 @@ protected function ucwordsUnicode($str, $encoding = 'UTF-8')
907
965
protected function ucfirstUnicode ($ str , $ encoding = 'UTF-8 ' )
908
966
{
909
967
return mb_strtoupper (mb_substr ($ str , 0 , 1 , $ encoding ), $ encoding )
910
- . mb_strtolower (mb_substr ($ str , 1 , mb_strlen ($ str ), $ encoding ), $ encoding );
968
+ . mb_strtolower (mb_substr ($ str , 1 , mb_strlen ($ str ), $ encoding ), $ encoding );
911
969
}
912
970
913
971
/**
0 commit comments