-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandManager.php
More file actions
131 lines (108 loc) · 3.99 KB
/
CommandManager.php
File metadata and controls
131 lines (108 loc) · 3.99 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace Sirius;
require './vendor/autoload.php';
use Sirius\utils\JsonParser;
class CommandManager
{
public const HELP_ARGUMENT = 'help';
private array $commandList = [];
private array $argumentsList = [];
private array $optionsList = [];
protected ?Container $container = null;
public static function main($command, $arguments)
{
(new static)->initialize($command, $arguments);
}
private function initialize(string $command, array $arguments): void
{
$this->container = Container::getInstance();
$this->addCommands();
$this->addParams($arguments);
$this->runCommand($command);
}
private function addCommands(): void
{
$commands = JsonParser::parseFile(__DIR__.'/commands/commands.json');
if (file_exists(ROOT_DIR. '/config/commands.json')) {
$userCommands = JsonParser::parseFile(ROOT_DIR . '/config/commands.json');
foreach ($userCommands as $name => $command) {
if (isset($commands[$name])) {
continue;
}
$commands[$name] = $command;
}
}
foreach ($commands as $command) {
$this->commandList[$command['name']] = new $command['class'];
}
}
private function addParams(array $arguments): void
{
foreach ($arguments as $arg) {
if (preg_match("#(.+?)=(.+)#", $arg, $parsedArgs)) {
$this->argumentsList[$parsedArgs[1]] = $parsedArgs[2];
} elseif (preg_match("#--(.+)#", $arg, $parsedArgs)) {
$this->optionsList[$parsedArgs[1]] = true;
} else {
echo 'Wrong argument or option : ' . $arg . PHP_EOL;
exit();
}
}
}
private function runCommand(string $command)
{
if (self::HELP_ARGUMENT === $command) {
$this->sendHelp();
}
$foundCommand = null;
foreach ($this->commandList as $currentCommand) {
if ($command === $currentCommand->getName() || $command === $currentCommand->getAlias()) {
$foundCommand = $currentCommand;
break;
}
}
if (null === $foundCommand) {
echo 'Command not found' . PHP_EOL;
exit();
}
foreach ($this->argumentsList as $argument => $value) {
if (!$foundCommand->hasArgument($argument)) {
echo 'This argument does not exist : ' . $argument . PHP_EOL;
exit();
}
}
foreach ($this->optionsList as $option => $value) {
if (!$foundCommand->hasOption($option)) {
echo 'This option does not exist : ' . $option . PHP_EOL;
exit();
}
}
$foundCommand->setParemValues($this->argumentsList, $this->optionsList);
$foundCommand->run($this->argumentsList);
}
private function sendHelp()
{
echo 'The available commands are :' . PHP_EOL . '----------------------------' . PHP_EOL;
foreach ($this->commandList as $command) {
echo $command->getName() . ' :' . PHP_EOL;
echo 'alias : ' . ($command->getAlias()??'') . PHP_EOL;
echo 'description : ' . ($command->getDescription()??'') . PHP_EOL;
if (!empty($arguments = $command->getArguments())) {
echo 'arguments :' . PHP_EOL;
foreach ($arguments as $argument) {
echo '- ' . $argument['name'];
echo (' : ' . $argument['description']?? null) . PHP_EOL ;
}
}
if (!empty($options = $command->getOptions())) {
echo 'options :' . PHP_EOL;
foreach ($options as $option) {
echo '- ' . $option['name'];
echo (' : ' . $option['description']?? null) . PHP_EOL ;
}
}
echo PHP_EOL;
}
exit();
}
}