TYPO3 CMS  TYPO3_6-2
CommandLineController.php
Go to the documentation of this file.
1 <?php
3 
27 
28  // Command line arguments, exploded into key => value-array pairs
32  public $cli_args = array();
33 
37  public $cli_options = array(
38  array('-s', 'Silent operation, will only output errors and important messages.'),
39  array('--silent', 'Same as -s'),
40  array('-ss', 'Super silent, will not even output errors or important messages.')
41  );
42 
46  public $cli_help = array(
47  'name' => 'CLI base class (overwrite this...)',
48  'synopsis' => '###OPTIONS###',
49  'description' => 'Class with basic functionality for CLI scripts (overwrite this...)',
50  'examples' => 'Give examples...',
51  'options' => '',
52  'license' => 'GNU GPL - free software!',
53  'author' => '[Author name]'
54  );
55 
59  public $stdin = NULL;
60 
68  public function __construct() {
69  // Loads the cli_args array with command line arguments
70  $this->cli_setArguments($_SERVER['argv']);
71  }
72 
82  public function cli_getArgArray($option, $argv) {
83  while (count($argv) && (string)$argv[0] !== (string)$option) {
84  array_shift($argv);
85  }
86  if ((string)$argv[0] === (string)$option) {
87  array_shift($argv);
88  return count($argv) ? $argv : array('');
89  }
90  }
91 
99  public function cli_isArg($option) {
100  return isset($this->cli_args[$option]);
101  }
102 
111  public function cli_argValue($option, $idx = 0) {
112  return is_array($this->cli_args[$option]) ? $this->cli_args[$option][$idx] : '';
113  }
114 
124  public function cli_getArgIndex(array $argv = array()) {
125  $cli_options = array();
126  $index = '_DEFAULT';
127  foreach ($argv as $token) {
128  // Options starting with a number is invalid - they could be negative values!
129  if ($token[0] === '-' && !\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($token[1])) {
130  list($index, $opt) = explode('=', $token, 2);
131  if (isset($cli_options[$index])) {
132  echo 'ERROR: Option ' . $index . ' was used twice!' . LF;
133  die;
134  }
135  $cli_options[$index] = array();
136  if (isset($opt)) {
137  $cli_options[$index][] = $opt;
138  }
139  } else {
140  $cli_options[$index][] = $token;
141  }
142  }
143  return $cli_options;
144  }
145 
152  public function cli_validateArgs() {
153  $cli_args_copy = $this->cli_args;
154  unset($cli_args_copy['_DEFAULT']);
155  $allOptions = array();
156  foreach ($this->cli_options as $cfg) {
157  $allOptions[] = $cfg[0];
158  $argSplit = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(' ', $cfg[0], TRUE);
159  if (isset($cli_args_copy[$argSplit[0]])) {
160  foreach ($argSplit as $i => $v) {
161  $ii = $i;
162  if ($i > 0) {
163  if (!isset($cli_args_copy[$argSplit[0]][($i - 1)]) && $v[0] != '[') {
164  // Using "[]" around a parameter makes it optional
165  echo 'ERROR: Option "' . $argSplit[0] . '" requires a value ("' . $v . '") on position ' . $i . LF;
166  die;
167  }
168  }
169  }
170  $ii++;
171  if (isset($cli_args_copy[$argSplit[0]][$ii - 1])) {
172  echo 'ERROR: Option "' . $argSplit[0] . '" does not support a value on position ' . $ii . LF;
173  die;
174  }
175  unset($cli_args_copy[$argSplit[0]]);
176  }
177  }
178  if (count($cli_args_copy)) {
179  echo wordwrap('ERROR: Option ' . implode(',', array_keys($cli_args_copy)) . ' was unknown to this script!' . LF . '(Options are: ' . implode(', ', $allOptions) . ')' . LF);
180  die;
181  }
182  }
183 
190  public function cli_setArguments(array $argv = array()) {
191  $this->cli_args = $this->cli_getArgIndex($argv);
192  }
193 
200  public function cli_keyboardInput() {
201  // Have to open the stdin stream only ONCE! otherwise I cannot read multiple lines from it... :
202  if (!$this->stdin) {
203  $this->stdin = fopen('php://stdin', 'r');
204  }
205  while (FALSE == ($line = fgets($this->stdin, 1000))) {
206 
207  }
208  return trim($line);
209  }
210 
218  public function cli_keyboardInput_yes($msg = '') {
219  // ONLY makes sense to echo it out since we are awaiting keyboard input - that cannot be silenced
220  echo $msg . ' (Yes/No + return): ';
221  return \TYPO3\CMS\Core\Utility\GeneralUtility::inList('y,yes', strtolower($this->cli_keyboardInput()));
222  }
223 
232  public function cli_echo($string = '', $force = FALSE) {
233  if (isset($this->cli_args['-ss'])) {
234 
235  } elseif (isset($this->cli_args['-s']) || isset($this->cli_args['--silent'])) {
236  if ($force) {
237  echo $string;
238  return TRUE;
239  }
240  } else {
241  echo $string;
242  return TRUE;
243  }
244  return FALSE;
245  }
246 
253  public function cli_help() {
254  foreach ($this->cli_help as $key => $value) {
255  $this->cli_echo(strtoupper($key) . ':
256 ');
257  switch ($key) {
258  case 'synopsis':
259  $optStr = '';
260  foreach ($this->cli_options as $v) {
261  $optStr .= ' [' . $v[0] . ']';
262  }
263  $this->cli_echo($this->cli_indent(str_replace('###OPTIONS###', trim($optStr), $value), 4) . '
264 
265 ');
266  break;
267  case 'options':
268  $this->cli_echo($this->cli_indent($value, 4) . LF);
269  $maxLen = 0;
270  foreach ($this->cli_options as $v) {
271  if (strlen($v[0]) > $maxLen) {
272  $maxLen = strlen($v[0]);
273  }
274  }
275  foreach ($this->cli_options as $v) {
276  $this->cli_echo($v[0] . substr($this->cli_indent(rtrim(($v[1] . LF . $v[2])), ($maxLen + 4)), strlen($v[0])) . LF);
277  }
278  $this->cli_echo(LF);
279  break;
280  default:
281  $this->cli_echo($this->cli_indent($value, 4) . '
282 
283 ');
284  }
285  }
286  }
287 
296  public function cli_indent($str, $indent) {
297  $lines = explode(LF, wordwrap($str, 75 - $indent));
298  $indentStr = str_pad('', $indent, ' ');
299  foreach ($lines as $k => $v) {
300  $lines[$k] = $indentStr . $lines[$k];
301  }
302  return implode(LF, $lines);
303  }
304 
305 }
die
Definition: index.php:6
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)