-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbot-cli
More file actions
91 lines (74 loc) · 2.3 KB
/
mbot-cli
File metadata and controls
91 lines (74 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env php
<?php
use GetOpt\ArgumentException;
use GetOpt\GetOpt;
use GetOpt\Option;
foreach ([
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php'
] as $file) {
if (file_exists($file)) {
define('COMPOSER_INSTALL', $file);
break;
}
}
unset($file);
if (!defined('COMPOSER_INSTALL')) {
fwrite(
STDERR,
'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL .
' composer install' . PHP_EOL . PHP_EOL .
'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL
);
die(1);
}
require COMPOSER_INSTALL;
define('NAME', 'Mobile One example bot');
define('VERSION', '1.0');
$getOpt = new GetOpt();
$getOpt->addOptions([
Option::create(null, 'version', GetOpt::NO_ARGUMENT)
->setDescription('Show version information and quit'),
Option::create('?', 'help', GetOpt::NO_ARGUMENT)
->setDescription('Show this help and quit'),
Option::create('b', 'bot', GetOpt::REQUIRED_ARGUMENT)
->setDescription(''),
]);
$getOpt->addCommands([
\GetOpt\Command::create('websocket', '\leocata\m1Bot\Commands\WebSocketCommand::run')
->setShortDescription('Start listen websocket'),
\GetOpt\Command::create('gearman', '\leocata\m1Bot\Commands\GearmanCommand::run')
->setShortDescription('Start gearman worker'),
]);
try {
$getOpt->process();
} catch (ArgumentException $exception) {
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
echo PHP_EOL . $getOpt->getHelpText();
exit;
}
$command = $getOpt->getCommand();
if (empty($getOpt->getOption('bot'))) {
echo PHP_EOL . 'no have bot';
exit;
}
$botName = $getOpt->getOption('bot');
$file = __DIR__ . '/../../../' . $botName . '.php';
if (!file_exists($file)) {
echo PHP_EOL . 'Don`t have bot file';
exit;
}
require $file;
$userBot = new $botName();
if (!$command || $getOpt->getOption('help')) {
echo $getOpt->getHelpText();
exit;
} else {
list ($class, $method) = explode('::', $command->handler());
call_user_func([$class, $method], $userBot, $getOpt->getOperands());
}
if ($getOpt->getOption('version')) {
echo sprintf('%s: %s' . PHP_EOL, NAME, VERSION);
exit;
}