php class CliManager implements iController protected action_class pro

 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
<?php
class CliManager implements iController
{
protected $action_class;
protected static $args = array();
public function __construct($action = null, $argv = array()) {
$this->action_class = $action;
$this->storeArguments($argv);
}
protected function storeArguments($argv)
{
while($arg = array_shift($argv)) {
if(mb_substr($arg, 0, 2) == '--') {
$key = mb_substr($arg, 2);
}
else if(mb_substr($arg, 0, 1) == '-') {
$key = mb_substr($arg, 1);
}
else {
continue;
}
self::$args[$key] = trim(array_shift($argv));
}
}
public static function getArgument($key, $default = null)
{
if(array_key_exists($key, self::$args)) {
return self::$args[$key];
}
return $default;
}
public function exec()
{
$action_class = $this->action_class."CliAction";
if (class_exists($action_class)) {
$action = new $action_class($this);
$action->exec();
} else {
throw new Exception('Class "'.$action_class.'" not found!');
}
}
}
?>