‪TYPO3CMS  9.5
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 
17 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
18 use Symfony\Component\Console\Helper\FormatterHelper;
19 use Symfony\Component\Console\Helper\HelperSet;
20 use Symfony\Component\Console\Helper\ProgressBar;
21 use Symfony\Component\Console\Helper\QuestionHelper;
22 use Symfony\Component\Console\Helper\Table;
23 use Symfony\Component\Console\Input\ArgvInput;
24 use Symfony\Component\Console\Output\ConsoleOutput as SymfonyConsoleOutput;
25 use Symfony\Component\Console\Question\ChoiceQuestion;
26 use Symfony\Component\Console\Question\ConfirmationQuestion;
27 use Symfony\Component\Console\Question\Question;
28 
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  $question = (new ChoiceQuestion($question, $choices, $default))
160  ->setMultiselect($multiSelect)
161  ->setMaxAttempts($attempts)
162  ->setErrorMessage('Value "%s" is invalid');
163 
164  return $this->‪getQuestionHelper()->ask($this->‪getInput(), $this->‪output, $question);
165  }
166 
176  public function ‪ask($question, $default = null, array $autocomplete = null)
177  {
178  $question = (new Question($question, $default))
179  ->setAutocompleterValues($autocomplete);
180 
181  return $this->‪getQuestionHelper()->ask($this->‪getInput(), $this->‪output, $question);
182  }
183 
193  public function ‪askConfirmation($question, $default = true)
194  {
195  $question = new ConfirmationQuestion($question, $default);
196 
197  return $this->‪getQuestionHelper()->ask($this->‪getInput(), $this->‪output, $question);
198  }
199 
208  public function ‪askHiddenResponse($question, $fallback = true)
209  {
210  $question = (new Question($question))
211  ->setHidden(true)
212  ->setHiddenFallback($fallback);
213 
214  return $this->‪getQuestionHelper()->ask($this->‪getInput(), $this->‪output, $question);
215  }
216 
232  public function ‪askAndValidate($question, ‪$validator, $attempts = null, $default = null, array $autocomplete = null)
233  {
234  $question = (new Question($question, $default))
235  ->setValidator(‪$validator)
236  ->setMaxAttempts($attempts)
237  ->setAutocompleterValues($autocomplete);
238 
239  return $this->‪getQuestionHelper()->ask($this->‪getInput(), $this->‪output, $question);
240  }
241 
257  public function ‪askHiddenResponseAndValidate($question, ‪$validator, $attempts = false, $fallback = true)
258  {
259  $question = (new Question($question))
260  ->setValidator(‪$validator)
261  ->setMaxAttempts($attempts)
262  ->setHidden(true)
263  ->setHiddenFallback($fallback);
264 
265  return $this->‪getQuestionHelper()->ask($this->‪getInput(), $this->‪output, $question);
266  }
267 
273  public function ‪progressStart($max = null)
274  {
275  $this->‪getProgressBar()->start($max);
276  }
277 
284  public function ‪progressAdvance($step = 1)
285  {
286  $this->‪getProgressBar()->advance($step);
287  }
288 
295  public function ‪progressSet($current)
296  {
297  $this->‪getProgressBar()->setProgress($current);
298  }
299 
303  public function ‪progressFinish()
304  {
305  $this->‪getProgressBar()->finish();
306  }
307 
312  protected function ‪getInput()
313  {
314  if ($this->input === null) {
315  if (!isset($_SERVER['argv'])) {
316  throw new \RuntimeException('Cannot initialize ArgvInput object without CLI context.', 1456914444);
317  }
318  $this->input = new ArgvInput();
319  }
320 
321  return ‪$this->input;
322  }
323 
329  protected function ‪getQuestionHelper()
330  {
331  if ($this->questionHelper === null) {
332  $this->questionHelper = new QuestionHelper();
333  $helperSet = new HelperSet([new FormatterHelper()]);
334  $this->questionHelper->setHelperSet($helperSet);
335  }
337  }
338 
344  protected function ‪getProgressBar()
345  {
346  if ($this->progressBar === null) {
347  $this->progressBar = new ProgressBar($this->‪output);
348  }
349  return ‪$this->progressBar;
350  }
351 
357  protected function ‪getTable()
358  {
359  if ($this->table === null) {
360  $this->table = new Table($this->‪output);
361  }
362  return ‪$this->table;
363  }
364 }
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\askHiddenResponse
‪string askHiddenResponse($question, $fallback=true)
Definition: ConsoleOutput.php:203
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\outputFormatted
‪outputFormatted($text='', array $arguments=[], $leftPadding=0)
Definition: ConsoleOutput.php:116
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\askHiddenResponseAndValidate
‪string askHiddenResponseAndValidate($question, $validator, $attempts=false, $fallback=true)
Definition: ConsoleOutput.php:252
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\__construct
‪__construct()
Definition: ConsoleOutput.php:58
‪TYPO3\CMS\Extbase\Mvc\Cli
Definition: Command.php:2
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\select
‪int string array select($question, $choices, $default=null, $multiSelect=false, $attempts=null)
Definition: ConsoleOutput.php:152
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\$table
‪Table $table
Definition: ConsoleOutput.php:53
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\askConfirmation
‪bool askConfirmation($question, $default=true)
Definition: ConsoleOutput.php:188
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\outputLine
‪outputLine($text='', array $arguments=[])
Definition: ConsoleOutput.php:102
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput
Definition: ConsoleOutput.php:34
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\progressStart
‪progressStart($max=null)
Definition: ConsoleOutput.php:268
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\getProgressBar
‪ProgressBar getProgressBar()
Definition: ConsoleOutput.php:339
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\$output
‪SymfonyConsoleOutput $output
Definition: ConsoleOutput.php:41
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\getTable
‪Table getTable()
Definition: ConsoleOutput.php:352
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\progressAdvance
‪progressAdvance($step=1)
Definition: ConsoleOutput.php:279
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\$questionHelper
‪QuestionHelper $questionHelper
Definition: ConsoleOutput.php:45
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\getQuestionHelper
‪QuestionHelper getQuestionHelper()
Definition: ConsoleOutput.php:324
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\$progressBar
‪ProgressBar $progressBar
Definition: ConsoleOutput.php:49
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\output
‪output($text, array $arguments=[])
Definition: ConsoleOutput.php:86
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\getInput
‪ArgvInput getInput()
Definition: ConsoleOutput.php:307
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\outputTable
‪outputTable($rows, $headers=null)
Definition: ConsoleOutput.php:131
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\$input
‪ArgvInput $input
Definition: ConsoleOutput.php:37
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\progressSet
‪progressSet($current)
Definition: ConsoleOutput.php:290
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\ask
‪string ask($question, $default=null, array $autocomplete=null)
Definition: ConsoleOutput.php:171
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\getMaximumLineLength
‪int getMaximumLineLength()
Definition: ConsoleOutput.php:73
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\askAndValidate
‪mixed askAndValidate($question, $validator, $attempts=null, $default=null, array $autocomplete=null)
Definition: ConsoleOutput.php:227
‪TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput\progressFinish
‪progressFinish()
Definition: ConsoleOutput.php:298