Skip to content

Commit f79c799

Browse files
authored
Merge pull request #1365 from mlocati/enable-commands-with-underscore
Enable using commands containing underscores
2 parents 5de35f7 + 392f949 commit f79c799

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

src/Telegram.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,17 @@ public function getCommandsList(): array
283283

284284
foreach ($files as $file) {
285285
//Remove "Command.php" from filename
286-
$command = $this->sanitizeCommand(substr($file->getFilename(), 0, -11));
287-
$command_name = mb_strtolower($command);
286+
$command = $this->classNameToCommandName(substr($file->getFilename(), 0, -4));
288287

289-
if (array_key_exists($command_name, $commands)) {
288+
if (array_key_exists($command, $commands)) {
290289
continue;
291290
}
292291

293292
require_once $file->getPathname();
294293

295294
$command_obj = $this->getCommandObject($command, $file->getPathname());
296295
if ($command_obj instanceof Command) {
297-
$commands[$command_name] = $command_obj;
296+
$commands[$command] = $command_obj;
298297
}
299298
}
300299
} catch (Exception $e) {
@@ -334,7 +333,7 @@ public function getCommandClassName(string $auth, string $command, string $filep
334333
return null;
335334
}
336335

337-
$command_class = $command_namespace . '\\' . $this->ucFirstUnicode($command) . 'Command';
336+
$command_class = $command_namespace . '\\' . $this->commandNameToClassName($command);
338337

339338
if (class_exists($command_class)) {
340339
return $command_class;
@@ -678,7 +677,7 @@ public function executeCommand(string $command): ServerResponse
678677
}
679678

680679
/**
681-
* Sanitize Command
680+
* @deprecated
682681
*
683682
* @param string $command
684683
*
@@ -1285,4 +1284,35 @@ public function getUpdateFilter(): ?callable
12851284
{
12861285
return $this->update_filter;
12871286
}
1287+
1288+
/**
1289+
* Converts the name of a class into the name of a command.
1290+
*
1291+
* @param string $class For example FooBarCommand
1292+
*
1293+
* @return string for example foo_bar. In case of errors, returns an empty string
1294+
*/
1295+
protected function classNameToCommandName(string $class): string
1296+
{
1297+
// 7 is the length of 'Command'
1298+
if (substr($class, -7) !== 'Command') {
1299+
return '';
1300+
}
1301+
return mb_strtolower(preg_replace('/(.)(?=[\p{Lu}])/u', '$1_', substr($class, 0, -7)));
1302+
}
1303+
1304+
/**
1305+
* Converts a command name into the name of a class.
1306+
*
1307+
* @param string $command For example foo_bar
1308+
*
1309+
* @return string for example FooBarCommand. In case of errors, returns an empty string
1310+
*/
1311+
protected function commandNameToClassName(string $command): string
1312+
{
1313+
if ($command === '') {
1314+
return '';
1315+
}
1316+
return str_replace(' ', '', $this->ucWordsUnicode(str_replace('_', ' ', $command))) . 'Command';
1317+
}
12881318
}

tests/Unit/Commands/CustomTestCommands/DummyAdminCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
use Longman\TelegramBot\Request;
1717

1818
/**
19-
* Test "/dummyadmin" command
19+
* Test "/dummy_admin" command
2020
*/
2121
class DummyAdminCommand extends AdminCommand
2222
{
2323
/**
2424
* @var string
2525
*/
26-
protected $name = 'dummyadmin';
26+
protected $name = 'dummy_admin';
2727

2828
/**
2929
* @var string
@@ -33,7 +33,7 @@ class DummyAdminCommand extends AdminCommand
3333
/**
3434
* @var string
3535
*/
36-
protected $usage = '/dummyadmin';
36+
protected $usage = '/dummy_admin';
3737

3838
/**
3939
* Command execute method

tests/Unit/Commands/CustomTestCommands/DummySystemCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
use Longman\TelegramBot\Request;
1717

1818
/**
19-
* Test "/dummysystem" command
19+
* Test "/dummy_system" command
2020
*/
2121
class DummySystemCommand extends SystemCommand
2222
{
2323
/**
2424
* @var string
2525
*/
26-
protected $name = 'dummysystem';
26+
protected $name = 'dummy_system';
2727

2828
/**
2929
* @var string
@@ -33,7 +33,7 @@ class DummySystemCommand extends SystemCommand
3333
/**
3434
* @var string
3535
*/
36-
protected $usage = '/dummysystem';
36+
protected $usage = '/dummy_system';
3737

3838
/**
3939
* Command execute method

tests/Unit/Commands/CustomTestCommands/DummyUserCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
use Longman\TelegramBot\Request;
1717

1818
/**
19-
* Test "/dummyuser" command
19+
* Test "/dummy_user" command
2020
*/
2121
class DummyUserCommand extends UserCommand
2222
{
2323
/**
2424
* @var string
2525
*/
26-
protected $name = 'dummyuser';
26+
protected $name = 'dummy_user';
2727

2828
/**
2929
* @var string
@@ -33,7 +33,7 @@ class DummyUserCommand extends UserCommand
3333
/**
3434
* @var string
3535
*/
36-
protected $usage = '/dummyuser';
36+
protected $usage = '/dummy_user';
3737

3838
/**
3939
* Command execute method

tests/Unit/TelegramTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ public function testAddCustomCommandsClass(): void
170170
]);
171171

172172
$command_classes = $tg->getCommandClasses();
173-
self::assertCount(1, $command_classes['System']);
174-
self::assertCount(1, $command_classes['Admin']);
175-
self::assertCount(1, $command_classes['User']);
173+
self::assertSame(['dummy_system' => 'Dummy\SystemCommands\DummySystemCommand'], $command_classes['System']);
174+
self::assertSame(['dummy_admin' => 'Dummy\AdminCommands\DummyAdminCommand'], $command_classes['Admin']);
175+
self::assertSame(['dummy_user' => 'Dummy\UserCommands\DummyUserCommand'], $command_classes['User']);
176176
}
177177

178178
public function testSettingDownloadUploadPaths(): void

0 commit comments

Comments
 (0)