|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TShellAction 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
|
|
|
* Base class for command line actions. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Brad Anderson <belisoful[at]icloud[dot]com> shell refactor |
|
19
|
|
|
* @author Wei Zhuo <weizhuo[at]gmail[dot]com> |
|
20
|
|
|
* @package Prado\Shell |
|
21
|
|
|
* @since 3.0.5 |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class TShellAction extends \Prado\TComponent |
|
24
|
|
|
{ |
|
25
|
|
|
protected $action; |
|
26
|
|
|
protected $defaultMethod = 0; |
|
27
|
|
|
protected $methods; |
|
28
|
|
|
protected $parameters; |
|
29
|
|
|
protected $optional; |
|
30
|
|
|
protected $description; |
|
31
|
|
|
|
|
32
|
|
|
protected $_outWriter; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return TShellWriter the writer for the class |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getWriter(): TShellWriter |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->_outWriter; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param TShellWriter $writer the writer for the class |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setWriter(TShellWriter $writer) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->_outWriter = $writer; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return string the command action for the class |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getAction(): string |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->action; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @@param string $writer the command action for the class |
|
60
|
|
|
* @param string $action |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setAction(string $action) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->action = $action; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Properties for the action set by parameter |
|
69
|
|
|
* @param string $actionID the action being executed |
|
70
|
|
|
* @return array properties for the $actionID |
|
71
|
|
|
*/ |
|
72
|
|
|
public function options($actionID): array |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
return []; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Aliases for the properties to be set by parameter |
|
79
|
|
|
* @param string $actionID the action being executed |
|
80
|
|
|
* @return array<alias, property> properties for the $actionID |
|
81
|
|
|
*/ |
|
82
|
|
|
public function optionAliases(): array |
|
83
|
|
|
{ |
|
84
|
|
|
return []; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Creates a directory and sets its mode |
|
89
|
|
|
* @param string $dir directory name |
|
90
|
|
|
* @param int $mask directory mode mask suitable for chmod() |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function createDirectory($dir, $mask) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!is_dir($dir)) { |
|
95
|
|
|
mkdir($dir); |
|
96
|
|
|
$this->_outWriter->writeLine("creating $dir"); |
|
97
|
|
|
} |
|
98
|
|
|
if (is_dir($dir)) { |
|
99
|
|
|
chmod($dir, $mask); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Creates a file and fills it with content |
|
105
|
|
|
* @param string $filename file name |
|
106
|
|
|
* @param int $content file contents |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function createFile($filename, $content) |
|
109
|
|
|
{ |
|
110
|
|
|
if (!is_file($filename)) { |
|
111
|
|
|
file_put_contents($filename, $content); |
|
112
|
|
|
$this->_outWriter->writeLine("creating $filename"); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Checks if specified parameters are suitable for the specified action |
|
118
|
|
|
* @param array $args parameters |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
|
|
public function isValidAction($args) |
|
122
|
|
|
{ |
|
123
|
|
|
if (preg_match("/^{$this->action}(?:\\/([-\w\d]*))?$/", $args[0] ?? '', $match)) { |
|
124
|
|
|
if (isset($match[1]) && $match[1]) { |
|
125
|
|
|
$i = array_flip($this->methods)[$match[1]] ?? null; |
|
126
|
|
|
if ($i === null) { |
|
127
|
|
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
} else { |
|
130
|
|
|
$i = $this->defaultMethod; |
|
131
|
|
|
$match[1] = $this->methods[$i]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$params = ($this->parameters[$i] === null) ? [] : $this->parameters[$i]; |
|
135
|
|
|
$params = is_array($params) ? $params : [$this->parameters[$i]]; |
|
136
|
|
|
if (count($args) - 1 < count($params)) { |
|
137
|
|
|
return null; |
|
138
|
|
|
} |
|
139
|
|
|
return $match[1]; |
|
140
|
|
|
} |
|
141
|
|
|
return null; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* renders help for the command |
|
146
|
|
|
* @param string $cmd |
|
147
|
|
|
*/ |
|
148
|
|
|
public function renderHelpCommand($cmd) |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
$this->_outWriter->write("\nusage: "); |
|
151
|
|
|
$this->_outWriter->writeLine("php prado-cli.php {$this->action}/<action>", [TShellWriter::BLUE, TShellWriter::BOLD]); |
|
152
|
|
|
$this->_outWriter->writeLine("\nexample: php prado-cli.php {$this->action}/{$this->methods[0]}\n"); |
|
153
|
|
|
$this->_outWriter->writeLine("The following actions are available:"); |
|
154
|
|
|
$this->_outWriter->writeLine(); |
|
155
|
|
|
foreach ($this->methods as $i => $method) { |
|
156
|
|
|
$params = []; |
|
157
|
|
|
if ($this->parameters[$i]) { |
|
158
|
|
|
$parameters = is_array($this->parameters[$i]) ? $this->parameters[$i] : [$this->parameters[$i]]; |
|
159
|
|
|
foreach ($parameters as $v) { |
|
160
|
|
|
$params[] = '<' . $v . '>'; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
$parameters = implode(' ', $params); |
|
164
|
|
|
$options = []; |
|
165
|
|
|
if ($this->optional[$i]) { |
|
166
|
|
|
$optional = is_array($this->optional[$i]) ? $this->optional[$i] : [$this->optional[$i]]; |
|
167
|
|
|
foreach ($optional as $v) { |
|
168
|
|
|
$options[] = '[' . $v . ']'; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
$optional = (strlen($parameters) ? ' ' : '') . implode(' ', $options); |
|
172
|
|
|
|
|
173
|
|
|
$description = $this->getWriter()->wrapText($this->description[$i + 1], 10); |
|
174
|
|
|
$parameters = $this->getWriter()->format($parameters, [TShellWriter::BLUE, TShellWriter::BOLD]); |
|
175
|
|
|
$optional = $this->getWriter()->format($optional, [TShellWriter::BLUE]); |
|
176
|
|
|
$description = $this->getWriter()->format($description, TShellWriter::DARK_GRAY); |
|
177
|
|
|
|
|
178
|
|
|
$this->_outWriter->write(' '); |
|
179
|
|
|
$this->_outWriter->writeLine($this->action . '/' . $method . ' ' . $parameters . $optional, [TShellWriter::BLUE, TShellWriter::BOLD]); |
|
180
|
|
|
$this->_outWriter->writeLine(' ' . $description); |
|
181
|
|
|
$this->_outWriter->writeLine(); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Renders General Help for the command |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
public function renderHelp() |
|
190
|
|
|
{ |
|
191
|
|
|
$action = $this->getWriter()->format($this->action, [TShellWriter::BLUE, TShellWriter::BOLD]); |
|
192
|
|
|
|
|
193
|
|
|
$str = ''; |
|
194
|
|
|
$length = 31; |
|
195
|
|
|
$str .= $this->getWriter()->pad(" - {$action}", $length); |
|
196
|
|
|
$description = $this->getWriter()->wrapText($this->description[0], $length); |
|
197
|
|
|
$str .= $description . PHP_EOL; |
|
198
|
|
|
foreach ($this->methods as $i => $method) { |
|
199
|
|
|
$str .= $this->getWriter()->pad(" {$this->action}/$method", $length); |
|
200
|
|
|
$description = $this->getWriter()->wrapText($this->description[$i + 1], $length); |
|
201
|
|
|
$str .= $this->getWriter()->format($description, TShellWriter::DARK_GRAY) . PHP_EOL; |
|
202
|
|
|
} |
|
203
|
|
|
return $str; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.