FormatterHelper.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 Boolean $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. $messages = (array) $messages;
  44. $len = 0;
  45. $lines = array();
  46. foreach ($messages as $message) {
  47. $message = OutputFormatter::escape($message);
  48. $lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
  49. $len = max($this->strlen($message) + ($large ? 4 : 2), $len);
  50. }
  51. $messages = $large ? array(str_repeat(' ', $len)) : array();
  52. foreach ($lines as $line) {
  53. $messages[] = $line.str_repeat(' ', $len - $this->strlen($line));
  54. }
  55. if ($large) {
  56. $messages[] = str_repeat(' ', $len);
  57. }
  58. foreach ($messages as &$message) {
  59. $message = sprintf('<%s>%s</%s>', $style, $message, $style);
  60. }
  61. return implode("\n", $messages);
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function getName()
  67. {
  68. return 'formatter';
  69. }
  70. }