Issues (1474)

framework/Shell/TShellAction.php (2 issues)

Severity
1
<?php
2
3
/**
4
 * TShellAction class file
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
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
 * @since 3.0.5
21
 */
22
abstract class TShellAction extends \Prado\TComponent
23
{
24
	protected $action;
25
	protected $defaultMethod = 0;
26
	protected $methods;
27
	protected $parameters;
28
	protected $optional;
29
	protected $description;
30
31
	protected $_outWriter;
32
33
	/**
34
	 * @return TShellApplication current application instance
35
	 */
36
	public function getApplication()
37
	{
38
		return Prado::getApplication();
39
	}
40
41
	/**
42
	 * @return TShellWriter the writer for the class
43
	 */
44
	public function getWriter(): TShellWriter
45
	{
46
		return $this->_outWriter;
47
	}
48
49
	/**
50
	 * @param TShellWriter $writer the writer for the class
51
	 */
52
	public function setWriter(TShellWriter $writer)
53
	{
54
		$this->_outWriter = $writer;
55
	}
56
57
	/**
58
	 * @return string the command action for the class
59
	 */
60
	public function getAction(): string
61
	{
62
		return $this->action;
63
	}
64
65
	/**
66
	 * @param string $action the command action for the class
67
	 */
68
	public function setAction(string $action)
69
	{
70
		$this->action = $action;
71
	}
72
73
	/**
74
	 * Properties for the action set by parameter
75
	 * @param string $actionID the action being executed
76
	 * @return array properties for the $actionID
77
	 */
78
	public function options($actionID): array
0 ignored issues
show
The parameter $actionID is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
	public function options(/** @scrutinizer ignore-unused */ $actionID): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
	{
80
		return [];
81
	}
82
83
	/**
84
	 * Aliases for the properties to be set by parameter
85
	 * @return array<string, string> alias => property for the $actionID
86
	 */
87
	public function optionAliases(): array
88
	{
89
		return [];
90
	}
91
92
	/**
93
	 * Creates a directory and sets its mode
94
	 * @param string $dir directory name
95
	 * @param int $mask directory mode mask suitable for chmod()
96
	 */
97
	protected function createDirectory($dir, $mask)
98
	{
99
		if (!is_dir($dir)) {
100
			mkdir($dir);
101
			$this->_outWriter->writeLine("creating $dir");
102
		}
103
		if (is_dir($dir)) {
104
			chmod($dir, $mask);
105
		}
106
	}
107
108
	/**
109
	 * Creates a file and fills it with content
110
	 * @param string $filename file name
111
	 * @param int $content file contents
112
	 */
113
	protected function createFile($filename, $content)
114
	{
115
		if (!is_file($filename)) {
116
			file_put_contents($filename, $content);
117
			$this->_outWriter->writeLine("creating $filename");
118
		}
119
	}
120
121
	/**
122
	 * Checks if specified parameters are suitable for the specified action
123
	 * @param array $args parameters
124
	 * @return bool
125
	 */
126
	public function isValidAction($args)
127
	{
128
		if (preg_match("/^{$this->action}(?:\\/([-\w\d]*))?$/", $args[0] ?? '', $match)) {
129
			if (isset($match[1]) && $match[1]) {
130
				$i = array_flip($this->methods)[$match[1]] ?? null;
131
				if ($i === null) {
132
					return null;
133
				}
134
			} else {
135
				$i = $this->defaultMethod;
136
				$match[1] = $this->methods[$i];
137
			}
138
139
			$params = ($this->parameters[$i] === null) ? [] : $this->parameters[$i];
140
			$params = is_array($params) ? $params : [$this->parameters[$i]];
141
			if (count($args) - 1 < count($params)) {
142
				return null;
143
			}
144
			return $match[1];
145
		}
146
		return null;
147
	}
148
149
	/**
150
	 * renders help for the command
151
	 * @param string $cmd
152
	 */
153
	public function renderHelpCommand($cmd)
0 ignored issues
show
The parameter $cmd is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

153
	public function renderHelpCommand(/** @scrutinizer ignore-unused */ $cmd)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
	{
155
		$this->_outWriter->write("\nusage: ");
156
		$this->_outWriter->writeLine("php prado-cli.php {$this->action}/<action>", [TShellWriter::BLUE, TShellWriter::BOLD]);
157
		$this->_outWriter->writeLine("\nexample: php prado-cli.php {$this->action}/{$this->methods[0]}\n");
158
		$this->_outWriter->writeLine("The following actions are available:");
159
		$this->_outWriter->writeLine();
160
		foreach ($this->methods as $i => $method) {
161
			$params = [];
162
			if ($this->parameters[$i]) {
163
				$parameters = is_array($this->parameters[$i]) ? $this->parameters[$i] : [$this->parameters[$i]];
164
				foreach ($parameters as $v) {
165
					$params[] = '<' . $v . '>';
166
				}
167
			}
168
			$parameters = implode(' ', $params);
169
			$options = [];
170
			if ($this->optional[$i]) {
171
				$optional = is_array($this->optional[$i]) ? $this->optional[$i] : [$this->optional[$i]];
172
				foreach ($optional as $v) {
173
					$options[] = '[' . $v . ']';
174
				}
175
			}
176
			$optional = (strlen($parameters) ? ' ' : '') . implode(' ', $options);
177
178
			$description = $this->getWriter()->wrapText($this->description[$i + 1], 10);
179
			$parameters = $this->getWriter()->format($parameters, [TShellWriter::BLUE, TShellWriter::BOLD]);
180
			$optional = $this->getWriter()->format($optional, [TShellWriter::BLUE]);
181
			$description = $this->getWriter()->format($description, TShellWriter::DARK_GRAY);
182
183
			$this->_outWriter->write('  ');
184
			$this->_outWriter->writeLine($this->action . '/' . $method . ' ' . $parameters . $optional, [TShellWriter::BLUE, TShellWriter::BOLD]);
185
			$this->_outWriter->writeLine('         ' . $description);
186
			$this->_outWriter->writeLine();
187
		}
188
	}
189
190
	/**
191
	 * Renders General Help for the command
192
	 * @return string
193
	 */
194
	public function renderHelp()
195
	{
196
		$action = $this->getWriter()->format($this->action, [TShellWriter::BLUE, TShellWriter::BOLD]);
197
198
		$str = '';
199
		$length = 31;
200
		$str .= $this->getWriter()->pad("  {$action}", $length);
201
		$description = $this->getWriter()->wrapText($this->description[0], $length);
202
		$str .= $description . PHP_EOL;
203
		foreach ($this->methods as $i => $method) {
204
			$str .= $this->getWriter()->pad("     {$this->action}/$method", $length);
205
			$description = $this->getWriter()->wrapText($this->description[$i + 1], $length);
206
			$str .= $this->getWriter()->format($description, TShellWriter::DARK_GRAY) . PHP_EOL;
207
		}
208
		return $str;
209
	}
210
}
211