1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
use Teebot\Exception\Fatal; |
4
|
|
|
use Teebot\Exception\Output; |
5
|
|
|
|
6
|
|
|
class BotMother |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected $cliOptions = [ |
9
|
|
|
'short' => 'r:b:c:t:d:h:', |
10
|
|
|
'long' => ['run', 'bot', 'command', 'token', 'dir', 'help'] |
11
|
|
|
]; |
12
|
|
|
|
13
|
|
|
protected $commandMethodMapper = [ |
14
|
|
|
'create-bot' => ['commandBotCreate', ['b:bot', 't:token', 'd:dir']], |
15
|
|
|
'help' => ['help', []], |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$opts = getopt($this->cliOptions['short'], $this->cliOptions['long']); |
21
|
|
|
|
22
|
|
|
$help = $opts['h'] ?? $opts['help'] ?? null; |
23
|
|
|
|
24
|
|
|
if ($help || empty($opts)) { |
25
|
|
|
$this->help(); |
26
|
|
|
|
27
|
|
|
exit; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$command = $opts['r'] ?? $opts['run'] ?? null; |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
if (!$command) { |
34
|
|
|
throw new Fatal('Command not specified!'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$method = $this->getMethod($command); |
38
|
|
|
|
39
|
|
|
if (!$method) { |
40
|
|
|
throw new Fatal('Unknown command!'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$args = $this->getMethodArgs($method); |
|
|
|
|
44
|
|
|
|
45
|
|
|
call_user_func_array([$this, $method], ['1', '2', '3']); |
46
|
|
|
} catch (Fatal $e) { |
47
|
|
|
Output::log($e); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function help() |
52
|
|
|
{ |
53
|
|
|
echo " |
54
|
|
|
Bot mother CLI utility will help you easily and quickly create Bot or Command for existing bot. Please use |
55
|
|
|
the following arguments: |
56
|
|
|
|
57
|
|
|
-r, --run [command_name] : Command name - required parameter. Could have the following values: create-bot, create-command |
58
|
|
|
-b, --bot [bot_name] : The name of the bot that you want to create |
59
|
|
|
-c, --command [name] : The name of the command that you want to create |
60
|
|
|
-t, --token [value] : Token for the bot creation |
61
|
|
|
-d, --dir [dir] : Bot's root directory (absolute path)\n\n"; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function getMethod($command) |
65
|
|
|
{ |
66
|
|
|
$method = $this->commandMethodMapper[$command] ?? null; |
67
|
|
|
|
68
|
|
|
if (!$method || !method_exists($this, $method)) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $method; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getMethodArgs($methodName) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function commandBotCreate($botName, $token, $dir) |
80
|
|
|
{ |
81
|
|
|
echo "\nCreating a new bot.\n"; |
82
|
|
|
|
83
|
|
|
echo $botName . " - " . $token . " - " . $dir; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
define('ROOT_DIR', realpath(__DIR__)); |
88
|
|
|
require_once ROOT_DIR . '/vendor/autoload.php'; |
89
|
|
|
|
90
|
|
|
$BotMother = new BotMother(); |
91
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.