TYPO3  7.6
typo3/sysext/extbase/Classes/Mvc/Cli/ConsoleOutput.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Cli;
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 
23 use Symfony\Component\Console\Output\ConsoleOutput as SymfonyConsoleOutput;
24 
29 {
33  protected $output;
34 
38  protected $dialogHelper;
39 
43  protected $progressHelper;
44 
48  protected $tableHelper;
49 
55  public function __construct()
56  {
57  $this->output = new SymfonyConsoleOutput();
58  $this->output->getFormatter()->setStyle('b', new OutputFormatterStyle(null, null, array('bold')));
59  $this->output->getFormatter()->setStyle('i', new OutputFormatterStyle('black', 'white'));
60  $this->output->getFormatter()->setStyle('u', new OutputFormatterStyle(null, null, array('underscore')));
61  $this->output->getFormatter()->setStyle('em', new OutputFormatterStyle(null, null, array('reverse')));
62  $this->output->getFormatter()->setStyle('strike', new OutputFormatterStyle(null, null, array('conceal')));
63  }
64 
70  public function getMaximumLineLength()
71  {
72  return 79;
73  }
74 
84  public function output($text, array $arguments = array())
85  {
86  if ($arguments !== array()) {
87  $text = vsprintf($text, $arguments);
88  }
89  $this->output->write($text);
90  }
91 
101  public function outputLine($text = '', array $arguments = array())
102  {
103  $this->output($text . PHP_EOL, $arguments);
104  }
105 
116  public function outputFormatted($text = '', array $arguments = array(), $leftPadding = 0)
117  {
118  $lines = explode(PHP_EOL, $text);
119  foreach ($lines as $line) {
120  $formattedText = str_repeat(' ', $leftPadding) . wordwrap($line, $this->getMaximumLineLength() - $leftPadding, PHP_EOL . str_repeat(' ', $leftPadding), true);
121  $this->outputLine($formattedText, $arguments);
122  }
123  }
124 
131  public function outputTable($rows, $headers = null)
132  {
133  $tableHelper = $this->getTableHelper();
134  if ($headers !== null) {
135  $tableHelper->setHeaders($headers);
136  }
137  $tableHelper->setRows($rows);
138  $tableHelper->render($this->output);
139  }
140 
152  public function select($question, $choices, $default = null, $multiSelect = false, $attempts = false)
153  {
154  return $this->getDialogHelper()->select($this->output, $question, $choices, $default, $attempts, 'Value "%s" is invalid', $multiSelect);
155  }
156 
166  public function ask($question, $default = null, array $autocomplete = null)
167  {
168  return $this->getDialogHelper()->ask($this->output, $question, $default, $autocomplete);
169  }
170 
180  public function askConfirmation($question, $default = true)
181  {
182  return $this->getDialogHelper()->askConfirmation($this->output, $question, $default);
183  }
184 
193  public function askHiddenResponse($question, $fallback = true)
194  {
195  return $this->getDialogHelper()->askHiddenResponse($this->output, $question, $fallback);
196  }
197 
213  public function askAndValidate($question, $validator, $attempts = false, $default = null, array $autocomplete = null)
214  {
215  return $this->getDialogHelper()->askAndValidate($this->output, $question, $validator, $attempts, $default, $autocomplete);
216  }
217 
233  public function askHiddenResponseAndValidate($question, $validator, $attempts = false, $fallback = true)
234  {
235  return $this->getDialogHelper()->askHiddenResponseAndValidate($this->output, $question, $validator, $attempts, $fallback);
236  }
237 
244  public function progressStart($max = null)
245  {
246  $this->getProgressHelper()->start($this->output, $max);
247  }
248 
257  public function progressAdvance($step = 1, $redraw = false)
258  {
259  $this->getProgressHelper()->advance($step, $redraw);
260  }
261 
270  public function progressSet($current, $redraw = false)
271  {
272  $this->getProgressHelper()->setCurrent($current, $redraw);
273  }
274 
280  public function progressFinish()
281  {
282  $this->getProgressHelper()->finish();
283  }
284 
290  protected function getDialogHelper()
291  {
292  if ($this->dialogHelper === null) {
293  $this->dialogHelper = new DialogHelper(false);
294  $helperSet = new HelperSet(array(new FormatterHelper()));
295  $this->dialogHelper->setHelperSet($helperSet);
296  }
297  return $this->dialogHelper;
298  }
299 
305  protected function getProgressHelper()
306  {
307  if ($this->progressHelper === null) {
308  $this->progressHelper = new ProgressHelper(false);
309  }
310  return $this->progressHelper;
311  }
312 
318  protected function getTableHelper()
319  {
320  if ($this->tableHelper === null) {
321  $this->tableHelper = new TableHelper(false);
322  }
323  return $this->tableHelper;
324  }
325 }