-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
60 lines (48 loc) · 1.7 KB
/
Copy pathcli.php
File metadata and controls
60 lines (48 loc) · 1.7 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
<?php
const BASE_DIR = __DIR__; // /var/www/html
require_once BASE_DIR . '/vendor/autoload.php';
use App\Commands\Contract\Command;
use Dotenv\Dotenv;
use splitbrain\phpcli\CLI as BaseCli;
use splitbrain\phpcli\Options;
Dotenv::createUnsafeImmutable(BASE_DIR)->load();
class CLI extends BaseCli
{
const COMMANDS_FILEPATH = BASE_DIR . '/configs/commands.php';
protected array $commands = [], $setup = [];
public function __construct($autocatch = true)
{
$config = require_once self::COMMANDS_FILEPATH;
$this->commands = $config['commands'];
$this->setup = $config['setup'];
parent::__construct($autocatch);
}
protected function setup(Options $options)
{
foreach ($this->setup as $key => $setupData) {
$options->registerCommand($setupData['command'], $setupData['description'] ?? '');
if (!empty($setupData['arguments'])) {
foreach ($setupData['arguments'] as $argument) {
$options->registerArgument(
$argument['name'],
$argument['description'] ?? '',
$argument['required'] ?? false,
$setupData['command']
);
}
}
}
}
protected function main(Options $options)
{
if (array_key_exists($options->getCmd(), $this->commands)) {
$cmd = new $this->commands[$options->getCmd()]($this, $options->getArgs());
if ($cmd instanceof Command) {
$cmd->handle();
}
} else {
echo $options->help();
}
}
}
(new CLI())->run();