-
Notifications
You must be signed in to change notification settings - Fork 0
/
novice
73 lines (58 loc) · 1.72 KB
/
novice
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
#!/usr/bin/env php
<?php
use App\Lib\Helpers\Config;
use App\Lib\Helpers\TextFormatter;
require __DIR__ . '/vendor/autoload.php';
class Novice
{
protected $commandPaths = [
];
function __construct($args)
{
$this->args = $args;
$this->commandPaths = Config::get('console.commandPaths');
}
function exec()
{
if (count($this->args) <= 1) {
$this->reportError("usage: php " . $this->args[0] . " <command> [<args>]");
}
$args = array_slice($this->args, 2);
$commandClass = $this->getCommandClass($this->args[1]);
if (empty($commandClass)) {
$this->reportError("Command not found" . $commandClass);
}
$command = $this->createCommandClass($commandClass, $args);
try {
$return = $command->run();
exit($return);
} catch (Exception $exception) {
$this->reportError($exception->getMessage());
}
}
private function getCommandClass($commandName)
{
foreach ($this->commandPaths as $commandPath) {
$commandFullName = $this->generateClassName($commandPath, $commandName);
if (class_exists($commandFullName)) {
return $commandFullName;
}
}
return false;
}
private function createCommandClass($commandClass, $args)
{
return new $commandClass($args);
}
private function reportError($message)
{
echo $message . PHP_EOL;
exit(1);
}
private function generateClassName($commandPath, $commandName)
{
return $commandPath . TextFormatter::snakeToCamelCase($commandName) . 'Command';
}
}
$novice = new Novice($argv);
$novice->exec();