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