-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.ext.php
More file actions
106 lines (79 loc) · 2.5 KB
/
console.ext.php
File metadata and controls
106 lines (79 loc) · 2.5 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
<?php
class EXT_Console
{
//--------------------------------------------------------------------------
private $commands = array();
//--------------------------------------------------------------------------
public function __construct()
{
require_once 'console_command' . EXT;
require_once 'stdout' . EXT;
$this->console = new stdOut();
}
//--------------------------------------------------------------------------
public function command_class($class, $path = NULL)
{
if ($path === NULL)
{
$path = EXT_PATH . 'console/commands/';
}
require_once $path . $class . EXT;
$this->register_command(new $class($this));
}
//--------------------------------------------------------------------------
public function register_command($cmd)
{
$cmd->init();
$this->commands[$cmd->key()] = $cmd;
}
//--------------------------------------------------------------------------
public function run()
{
$args = array_splice($GLOBALS['argv'] , 1);
$this->console->hr();
if ( ! count($args))
{
$this->display_commands();
}
else
{
@list($key, $cmd) = explode(':', $args[0]);
$args = array_slice($args, 1);
if ( ! isset($this->commands[$key]) || !isset($this->commands[$key]->commands[$cmd]))
{
$this->console->red('Команда не найдена')->br()->hr();
exit;
}
$required = $this->commands[$key]->commands[$cmd]['required'];
if (count($required) > count($args))
{
$this->console->red('Недостаточно аргументов')->br()->hr();
foreach ($this->commands[$key]->commands[$cmd]['arguments'] as $arg => $desc)
{
$optional = empty($this->commands[$key]->commands[$cmd]['required'][$arg]);
$this->console->green($optional ? "[$arg]" : $arg)->put(' - ' . $desc . ($optional ? ' (не обязателно)' : ''));
$this->console->br();
}
$this->console->hr();
exit;
}
$cmd = 'cmd_' . $cmd;
call_user_func_array(array($this->commands[$key], $cmd), $args);
$this->console->br()->hr();
}
$this->console->reset();
}
//--------------------------------------------------------------------------
public function display_commands()
{
foreach ($this->commands as $key => &$command)
{
$this->console->bg_green()->black(" {$key} ")->br();
foreach ($command->commands as $cmd => $opt)
{
$this->console->green($key)->put(':')->magenta($cmd)->put(' - ' . $opt['description'])->br();
}
}
}
//--------------------------------------------------------------------------
}