TYPO3 CMS  TYPO3_8-7
ConsoleOutput.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
29 
34 {
38  protected $input;
39 
43  protected $output;
44 
48  protected $questionHelper;
49 
53  protected $progressBar;
54 
58  protected $table;
59 
63  public function __construct()
64  {
65  $this->output = new SymfonyConsoleOutput();
66  $this->output->getFormatter()->setStyle('b', new OutputFormatterStyle(null, null, ['bold']));
67  $this->output->getFormatter()->setStyle('i', new OutputFormatterStyle('black', 'white'));
68  $this->output->getFormatter()->setStyle('u', new OutputFormatterStyle(null, null, ['underscore']));
69  $this->output->getFormatter()->setStyle('em', new OutputFormatterStyle(null, null, ['reverse']));
70  $this->output->getFormatter()->setStyle('strike', new OutputFormatterStyle(null, null, ['conceal']));
71  }
72 
78  public function getMaximumLineLength()
79  {
80  return 79;
81  }
82 
91  public function output($text, array $arguments = [])
92  {
93  if ($arguments !== []) {
94  $text = vsprintf($text, $arguments);
95  }
96  $this->output->write($text);
97  }
98 
107  public function outputLine($text = '', array $arguments = [])
108  {
109  $this->output($text . PHP_EOL, $arguments);
110  }
111 
121  public function outputFormatted($text = '', array $arguments = [], $leftPadding = 0)
122  {
123  $lines = explode(PHP_EOL, $text);
124  foreach ($lines as $line) {
125  $formattedText = str_repeat(' ', $leftPadding) . wordwrap($line, $this->getMaximumLineLength() - $leftPadding, PHP_EOL . str_repeat(' ', $leftPadding), true);
126  $this->outputLine($formattedText, $arguments);
127  }
128  }
129 
136  public function outputTable($rows, $headers = null)
137  {
138  $table = $this->getTable();
139  if ($headers !== null) {
140  $table->setHeaders($headers);
141  }
142  $table->setRows($rows);
143  $table->render();
144  }
145 
157  public function select($question, $choices, $default = null, $multiSelect = false, $attempts = null)
158  {
159  // boolean option is @deprecated in TYPO3 v8, will be removed in TYPO3 v9
160  if ($attempts === false) {
161  GeneralUtility::deprecationLog('CLI output select() asks for infinite attempts by setting the value to "false", but should be null by default. Use "null" instead in your CommandController.');
162  $attempts = null;
163  }
164  $question = (new ChoiceQuestion($question, $choices, $default))
165  ->setMultiselect($multiSelect)
166  ->setMaxAttempts($attempts)
167  ->setErrorMessage('Value "%s" is invalid');
168 
169  return $this->getQuestionHelper()->ask($this->getInput(), $this->output, $question);
170  }
171 
181  public function ask($question, $default = null, array $autocomplete = null)
182  {
183  $question = (new Question($question, $default))
184  ->setAutocompleterValues($autocomplete);
185 
186  return $this->getQuestionHelper()->ask($this->getInput(), $this->output, $question);
187  }
188 
198  public function askConfirmation($question, $default = true)
199  {
200  $question = new ConfirmationQuestion($question, $default);
201 
202  return $this->getQuestionHelper()->ask($this->getInput(), $this->output, $question);
203  }
204 
213  public function askHiddenResponse($question, $fallback = true)
214  {
215  $question = (new Question($question))
216  ->setHidden(true)
217  ->setHiddenFallback($fallback);
218 
219  return $this->getQuestionHelper()->ask($this->getInput(), $this->output, $question);
220  }
221 
237  public function askAndValidate($question, $validator, $attempts = null, $default = null, array $autocomplete = null)
238  {
239  // boolean option is @deprecated in TYPO3 v8, will be removed in TYPO3 v9
240  if ($attempts === false) {
241  GeneralUtility::deprecationLog('CLI output askAndValidate() sets infinite attempts by setting the value to "false", but should be null by default. Use "null" instead in your CommandController.');
242  $attempts = null;
243  }
244  $question = (new Question($question, $default))
245  ->setValidator($validator)
246  ->setMaxAttempts($attempts)
247  ->setAutocompleterValues($autocomplete);
248 
249  return $this->getQuestionHelper()->ask($this->getInput(), $this->output, $question);
250  }
251 
267  public function askHiddenResponseAndValidate($question, $validator, $attempts = false, $fallback = true)
268  {
269  $question = (new Question($question))
270  ->setValidator($validator)
271  ->setMaxAttempts($attempts)
272  ->setHidden(true)
273  ->setHiddenFallback($fallback);
274 
275  return $this->getQuestionHelper()->ask($this->getInput(), $this->output, $question);
276  }
277 
283  public function progressStart($max = null)
284  {
285  $this->getProgressBar()->start($max);
286  }
287 
294  public function progressAdvance($step = 1)
295  {
296  $this->getProgressBar()->advance($step);
297  }
298 
305  public function progressSet($current)
306  {
307  $this->getProgressBar()->setProgress($current);
308  }
309 
313  public function progressFinish()
314  {
315  $this->getProgressBar()->finish();
316  }
317 
322  protected function getInput()
323  {
324  if ($this->input === null) {
325  if (!isset($_SERVER['argv'])) {
326  throw new \RuntimeException('Cannot initialize ArgvInput object without CLI context.', 1456914444);
327  }
328  $this->input = new ArgvInput();
329  }
330 
331  return $this->input;
332  }
333 
339  protected function getQuestionHelper()
340  {
341  if ($this->questionHelper === null) {
342  $this->questionHelper = new QuestionHelper();
343  $helperSet = new HelperSet([new FormatterHelper()]);
344  $this->questionHelper->setHelperSet($helperSet);
345  }
346  return $this->questionHelper;
347  }
348 
354  protected function getProgressBar()
355  {
356  if ($this->progressBar === null) {
357  $this->progressBar = new ProgressBar($this->output);
358  }
359  return $this->progressBar;
360  }
361 
367  protected function getTable()
368  {
369  if ($this->table === null) {
370  $this->table = new Table($this->output);
371  }
372  return $this->table;
373  }
374 }
askAndValidate($question, $validator, $attempts=null, $default=null, array $autocomplete=null)
outputFormatted($text='', array $arguments=[], $leftPadding=0)
ask($question, $default=null, array $autocomplete=null)
select($question, $choices, $default=null, $multiSelect=false, $attempts=null)
askConfirmation($question, $default=true)
askHiddenResponseAndValidate($question, $validator, $attempts=false, $fallback=true)
outputLine($text='', array $arguments=[])
output($text, array $arguments=[])
askHiddenResponse($question, $fallback=true)