Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
SymfonyStyle.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\Console\Style;
013
014 use Symfony\Component\Console\Application;
015 use Symfony\Component\Console\Exception\RuntimeException;
016 use Symfony\Component\Console\Formatter\OutputFormatter;
017 use Symfony\Component\Console\Helper\Helper;
018 use Symfony\Component\Console\Helper\ProgressBar;
019 use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
020 use Symfony\Component\Console\Helper\Table;
021 use Symfony\Component\Console\Input\InputInterface;
022 use Symfony\Component\Console\Output\BufferedOutput;
023 use Symfony\Component\Console\Output\OutputInterface;
024 use Symfony\Component\Console\Question\ChoiceQuestion;
025 use Symfony\Component\Console\Question\ConfirmationQuestion;
026 use Symfony\Component\Console\Question\Question;
027
028 /**
029 * Output decorator helpers for the Symfony Style Guide.
030 *
031 * @author Kevin Bond <kevinbond@gmail.com>
032 */
033 class SymfonyStyle extends OutputStyle
034 {
035 const MAX_LINE_LENGTH = 120;
036
037 private $input;
038 private $questionHelper;
039 private $progressBar;
040 private $lineLength;
041 private $bufferedOutput;
042
043 /**
044 * @param InputInterface $input
045 * @param OutputInterface $output
046 */
047 public function __construct(InputInterface $input, OutputInterface $output)
048 {
049 $this->input = $input;
050 $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
051 // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
052 $this->lineLength = min($this->getTerminalWidth() - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
053
054 parent::__construct($output);
055 }
056
057 /**
058 * Formats a message as a block of text.
059 *
060 * @param string|array $messages The message to write in the block
061 * @param string|null $type The block type (added in [] on first line)
062 * @param string|null $style The style to apply to the whole block
063 * @param string $prefix The prefix for the block
064 * @param bool $padding Whether to add vertical padding
065 */
066 public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
067 {
068 $messages = is_array($messages) ? array_values($messages) : array($messages);
069
070 $this->autoPrependBlock();
071 $this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, true));
072 $this->newLine();
073 }
074
075 /**
076 * {@inheritdoc}
077 */
078 public function title($message)
079 {
080 $this->autoPrependBlock();
081 $this->writeln(array(
082 sprintf('<comment>%s</>', $message),
083 sprintf('<comment>%s</>', str_repeat('=', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
084 ));
085 $this->newLine();
086 }
087
088 /**
089 * {@inheritdoc}
090 */
091 public function section($message)
092 {
093 $this->autoPrependBlock();
094 $this->writeln(array(
095 sprintf('<comment>%s</>', $message),
096 sprintf('<comment>%s</>', str_repeat('-', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
097 ));
098 $this->newLine();
099 }
100
101 /**
102 * {@inheritdoc}
103 */
104 public function listing(array $elements)
105 {
106 $this->autoPrependText();
107 $elements = array_map(function ($element) {
108 return sprintf(' * %s', $element);
109 }, $elements);
110
111 $this->writeln($elements);
112 $this->newLine();
113 }
114
115 /**
116 * {@inheritdoc}
117 */
118 public function text($message)
119 {
120 $this->autoPrependText();
121
122 $messages = is_array($message) ? array_values($message) : array($message);
123 foreach ($messages as $message) {
124 $this->writeln(sprintf(' %s', $message));
125 }
126 }
127
128 /**
129 * Formats a command comment.
130 *
131 * @param string|array $message
132 */
133 public function comment($message)
134 {
135 $messages = is_array($message) ? array_values($message) : array($message);
136
137 $this->autoPrependBlock();
138 $this->writeln($this->createBlock($messages, null, null, '<fg=default;bg=default> // </>'));
139 $this->newLine();
140 }
141
142 /**
143 * {@inheritdoc}
144 */
145 public function success($message)
146 {
147 $this->block($message, 'OK', 'fg=black;bg=green', ' ', true);
148 }
149
150 /**
151 * {@inheritdoc}
152 */
153 public function error($message)
154 {
155 $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true);
156 }
157
158 /**
159 * {@inheritdoc}
160 */
161 public function warning($message)
162 {
163 $this->block($message, 'WARNING', 'fg=white;bg=red', ' ', true);
164 }
165
166 /**
167 * {@inheritdoc}
168 */
169 public function note($message)
170 {
171 $this->block($message, 'NOTE', 'fg=yellow', ' ! ');
172 }
173
174 /**
175 * {@inheritdoc}
176 */
177 public function caution($message)
178 {
179 $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true);
180 }
181
182 /**
183 * {@inheritdoc}
184 */
185 public function table(array $headers, array $rows)
186 {
187 $style = clone Table::getStyleDefinition('symfony-style-guide');
188 $style->setCellHeaderFormat('<info>%s</info>');
189
190 $table = new Table($this);
191 $table->setHeaders($headers);
192 $table->setRows($rows);
193 $table->setStyle($style);
194
195 $table->render();
196 $this->newLine();
197 }
198
199 /**
200 * {@inheritdoc}
201 */
202 public function ask($question, $default = null, $validator = null)
203 {
204 $question = new Question($question, $default);
205 $question->setValidator($validator);
206
207 return $this->askQuestion($question);
208 }
209
210 /**
211 * {@inheritdoc}
212 */
213 public function askHidden($question, $validator = null)
214 {
215 $question = new Question($question);
216
217 $question->setHidden(true);
218 $question->setValidator($validator);
219
220 return $this->askQuestion($question);
221 }
222
223 /**
224 * {@inheritdoc}
225 */
226 public function confirm($question, $default = true)
227 {
228 return $this->askQuestion(new ConfirmationQuestion($question, $default));
229 }
230
231 /**
232 * {@inheritdoc}
233 */
234 public function choice($question, array $choices, $default = null)
235 {
236 if (null !== $default) {
237 $values = array_flip($choices);
238 $default = $values[$default];
239 }
240
241 return $this->askQuestion(new ChoiceQuestion($question, $choices, $default));
242 }
243
244 /**
245 * {@inheritdoc}
246 */
247 public function progressStart($max = 0)
248 {
249 $this->progressBar = $this->createProgressBar($max);
250 $this->progressBar->start();
251 }
252
253 /**
254 * {@inheritdoc}
255 */
256 public function progressAdvance($step = 1)
257 {
258 $this->getProgressBar()->advance($step);
259 }
260
261 /**
262 * {@inheritdoc}
263 */
264 public function progressFinish()
265 {
266 $this->getProgressBar()->finish();
267 $this->newLine(2);
268 $this->progressBar = null;
269 }
270
271 /**
272 * {@inheritdoc}
273 */
274 public function createProgressBar($max = 0)
275 {
276 $progressBar = parent::createProgressBar($max);
277
278 if ('\\' !== DIRECTORY_SEPARATOR) {
279 $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591
280 $progressBar->setProgressCharacter('');
281 $progressBar->setBarCharacter('▓'); // dark shade character \u2593
282 }
283
284 return $progressBar;
285 }
286
287 /**
288 * @param Question $question
289 *
290 * @return string
291 */
292 public function askQuestion(Question $question)
293 {
294 if ($this->input->isInteractive()) {
295 $this->autoPrependBlock();
296 }
297
298 if (!$this->questionHelper) {
299 $this->questionHelper = new SymfonyQuestionHelper();
300 }
301
302 $answer = $this->questionHelper->ask($this->input, $this, $question);
303
304 if ($this->input->isInteractive()) {
305 $this->newLine();
306 $this->bufferedOutput->write("\n");
307 }
308
309 return $answer;
310 }
311
312 /**
313 * {@inheritdoc}
314 */
315 public function writeln($messages, $type = self::OUTPUT_NORMAL)
316 {
317 parent::writeln($messages, $type);
318 $this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
319 }
320
321 /**
322 * {@inheritdoc}
323 */
324 public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
325 {
326 parent::write($messages, $newline, $type);
327 $this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
328 }
329
330 /**
331 * {@inheritdoc}
332 */
333 public function newLine($count = 1)
334 {
335 parent::newLine($count);
336 $this->bufferedOutput->write(str_repeat("\n", $count));
337 }
338
339 /**
340 * @return ProgressBar
341 */
342 private function getProgressBar()
343 {
344 if (!$this->progressBar) {
345 throw new RuntimeException('The ProgressBar is not started.');
346 }
347
348 return $this->progressBar;
349 }
350
351 private function getTerminalWidth()
352 {
353 $application = new Application();
354 $dimensions = $application->getTerminalDimensions();
355
356 return $dimensions[0] ?: self::MAX_LINE_LENGTH;
357 }
358
359 private function autoPrependBlock()
360 {
361 $chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
362
363 if (!isset($chars[0])) {
364 return $this->newLine(); //empty history, so we should start with a new line.
365 }
366 //Prepend new line for each non LF chars (This means no blank line was output before)
367 $this->newLine(2 - substr_count($chars, "\n"));
368 }
369
370 private function autoPrependText()
371 {
372 $fetched = $this->bufferedOutput->fetch();
373 //Prepend new line if last char isn't EOL:
374 if ("\n" !== substr($fetched, -1)) {
375 $this->newLine();
376 }
377 }
378
379 private function reduceBuffer($messages)
380 {
381 // We need to know if the two last chars are PHP_EOL
382 // Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
383 return array_map(function ($value) {
384 return substr($value, -4);
385 }, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));
386 }
387
388 private function createBlock($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = false)
389 {
390 $indentLength = 0;
391 $prefixLength = Helper::strlenWithoutDecoration($this->getFormatter(), $prefix);
392 $lines = array();
393
394 if (null !== $type) {
395 $type = sprintf('[%s] ', $type);
396 $indentLength = strlen($type);
397 $lineIndentation = str_repeat(' ', $indentLength);
398 }
399
400 // wrap and add newlines for each element
401 foreach ($messages as $key => $message) {
402 if ($escape) {
403 $message = OutputFormatter::escape($message);
404 }
405
406 $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, PHP_EOL, true)));
407
408 if (count($messages) > 1 && $key < count($messages) - 1) {
409 $lines[] = '';
410 }
411 }
412
413 $firstLineIndex = 0;
414 if ($padding && $this->isDecorated()) {
415 $firstLineIndex = 1;
416 array_unshift($lines, '');
417 $lines[] = '';
418 }
419
420 foreach ($lines as $i => &$line) {
421 if (null !== $type) {
422 $line = $firstLineIndex === $i ? $type.$line : $lineIndentation.$line;
423 }
424
425 $line = $prefix.$line;
426 $line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
427
428 if ($style) {
429 $line = sprintf('<%s>%s</>', $style, $line);
430 }
431 }
432
433 return $lines;
434 }
435 }
436