|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TApplication class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Brad Anderson <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
* @package Prado\Shell |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Shell; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Prado; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* TShellInterpreter Class |
|
17
|
|
|
* |
|
18
|
|
|
* Command line interface, configures the action classes and dispatches the command actions. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Wei Zhuo <weizhuo[at]gmail[dot]com> |
|
21
|
|
|
* @package Prado\Shell |
|
22
|
|
|
* @since 3.0.5 |
|
23
|
|
|
*/ |
|
24
|
|
|
class TShellInterpreter |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array command action classes |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $_actions = []; |
|
30
|
|
|
|
|
31
|
|
|
protected $_helpPrinted = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $class action class name |
|
35
|
|
|
*/ |
|
36
|
|
|
public function addActionClass($class) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->_actions[$class] = new $class; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
*/ |
|
43
|
|
|
public function clearActionClass() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->_actions = []; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return TShellInterpreter static instance |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function getInstance() |
|
52
|
|
|
{ |
|
53
|
|
|
static $instance; |
|
54
|
|
|
if ($instance === null) { |
|
55
|
|
|
$instance = new self; |
|
56
|
|
|
} |
|
57
|
|
|
return $instance; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function printGreeting() |
|
61
|
|
|
{ |
|
62
|
|
|
if (!$this->_helpPrinted) { |
|
63
|
|
|
echo "Command line tools for Prado " . Prado::getVersion() . ".\n"; |
|
64
|
|
|
$this->_helpPrinted = true; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Dispatch the command line actions. |
|
70
|
|
|
* @param array $args command line arguments |
|
71
|
|
|
*/ |
|
72
|
|
|
public function run($args) |
|
73
|
|
|
{ |
|
74
|
|
|
if (count($args) > 1) { |
|
75
|
|
|
array_shift($args); |
|
76
|
|
|
} |
|
77
|
|
|
$valid = false; |
|
78
|
|
|
$_actions = $this->_actions; |
|
79
|
|
|
foreach ($_actions as $class => $action) { |
|
80
|
|
|
if ($action->isValidAction($args)) { |
|
81
|
|
|
$valid |= $action->performAction($args); |
|
82
|
|
|
break; |
|
83
|
|
|
} else { |
|
84
|
|
|
$valid = false; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
if (!$valid) { |
|
|
|
|
|
|
88
|
|
|
$this->printHelp(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Print command line help, default action. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function printHelp() |
|
96
|
|
|
{ |
|
97
|
|
|
TShellInterpreter::getInstance()->printGreeting(); |
|
98
|
|
|
|
|
99
|
|
|
echo "usage: php prado-cli.php action <parameter> [optional]\n"; |
|
100
|
|
|
echo "example: php prado-cli.php flushcaches /prado_app_directory\n\n"; |
|
101
|
|
|
echo "example: php prado-cli.php app /prado_app_directory help\n\n"; |
|
102
|
|
|
echo "example: php prado-cli.php app /prado_app_directory cron tasks\n\n"; |
|
103
|
|
|
echo "actions:\n"; |
|
104
|
|
|
foreach ($this->_actions as $action) { |
|
105
|
|
|
echo $action->renderHelp(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: