FormatterHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Helper;
  11. use Symfony\Component\Console\Formatter\OutputFormatter;
  12. /**
  13. * The Formatter class provides helpers to format messages.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class FormatterHelper extends Helper
  18. {
  19. /**
  20. * Formats a message within a section.
  21. *
  22. * @param string $section The section name
  23. * @param string $message The message
  24. * @param string $style The style to apply to the section
  25. *
  26. * @return string The format section
  27. */
  28. public function formatSection($section, $message, $style = 'info')
  29. {
  30. return sprintf('<%s>[%s]</%s> %s', $style, $section, $style, $message);
  31. }
  32. /**
  33. * Formats a message as a block of text.
  34. *
  35. * @param string|array $messages The message to write in the block
  36. * @param string $style The style to apply to the whole block
  37. * @param bool $large Whether to return a large block
  38. *
  39. * @return string The formatter message
  40. */
  41. public function formatBlock($messages, $style, $large = false)
  42. {
  43. if (!\is_array($messages)) {
  44. $messages = [$messages];
  45. }
  46. $len = 0;
  47. $lines = [];
  48. foreach ($messages as $message) {
  49. $message = OutputFormatter::escape($message);
  50. $lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
  51. $len = max($this->strlen($message) + ($large ? 4 : 2), $len);
  52. }
  53. $messages = $large ? [str_repeat(' ', $len)] : [];
  54. for ($i = 0; isset($lines[$i]); ++$i) {
  55. $messages[] = $lines[$i].str_repeat(' ', $len - $this->strlen($lines[$i]));
  56. }
  57. if ($large) {
  58. $messages[] = str_repeat(' ', $len);
  59. }
  60. for ($i = 0; isset($messages[$i]); ++$i) {
  61. $messages[$i] = sprintf('<%s>%s</%s>', $style, $messages[$i], $style);
  62. }
  63. return implode("\n", $messages);
  64. }
  65. /**
  66. * Truncates a message to the given length.
  67. *
  68. * @param string $message
  69. * @param int $length
  70. * @param string $suffix
  71. *
  72. * @return string
  73. */
  74. public function truncate($message, $length, $suffix = '...')
  75. {
  76. $computedLength = $length - $this->strlen($suffix);
  77. if ($computedLength > $this->strlen($message)) {
  78. return $message;
  79. }
  80. if (false === $encoding = mb_detect_encoding($message, null, true)) {
  81. return substr($message, 0, $length).$suffix;
  82. }
  83. return mb_substr($message, 0, $length, $encoding).$suffix;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getName()
  89. {
  90. return 'formatter';
  91. }
  92. }