/application/libraries/Doctrine/Symfony/Component/Console/Formatter/OutputFormatter.php

https://github.com/Sa-ryong/Stadioom-php · PHP · 185 lines · 78 code · 21 blank · 86 comment · 10 complexity · 51006b66de0d737cee4ee0ea84eb4fe9 MD5 · raw file

  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\Formatter;
  11. /**
  12. * Formatter class for console output.
  13. *
  14. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  15. *
  16. * @api
  17. */
  18. class OutputFormatter implements OutputFormatterInterface
  19. {
  20. private $decorated;
  21. private $styles = array();
  22. /**
  23. * Initializes console output formatter.
  24. *
  25. * @param Boolean $decorated Whether this formatter should actually decorate strings
  26. * @param array $styles Array of "name => FormatterStyle" instance
  27. *
  28. * @api
  29. */
  30. public function __construct($decorated = null, array $styles = array())
  31. {
  32. $this->decorated = (Boolean) $decorated;
  33. $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
  34. $this->setStyle('info', new OutputFormatterStyle('green'));
  35. $this->setStyle('comment', new OutputFormatterStyle('yellow'));
  36. $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
  37. foreach ($styles as $name => $style) {
  38. $this->setStyle($name, $style);
  39. }
  40. }
  41. /**
  42. * Sets the decorated flag.
  43. *
  44. * @param Boolean $decorated Whether to decorated the messages or not
  45. *
  46. * @api
  47. */
  48. public function setDecorated($decorated)
  49. {
  50. $this->decorated = (Boolean) $decorated;
  51. }
  52. /**
  53. * Gets the decorated flag.
  54. *
  55. * @return Boolean true if the output will decorate messages, false otherwise
  56. *
  57. * @api
  58. */
  59. public function isDecorated()
  60. {
  61. return $this->decorated;
  62. }
  63. /**
  64. * Sets a new style.
  65. *
  66. * @param string $name The style name
  67. * @param OutputFormatterStyleInterface $style The style instance
  68. *
  69. * @api
  70. */
  71. public function setStyle($name, OutputFormatterStyleInterface $style)
  72. {
  73. $this->styles[strtolower($name)] = $style;
  74. }
  75. /**
  76. * Checks if output formatter has style with specified name.
  77. *
  78. * @param string $name
  79. *
  80. * @return Boolean
  81. *
  82. * @api
  83. */
  84. public function hasStyle($name)
  85. {
  86. return isset($this->styles[strtolower($name)]);
  87. }
  88. /**
  89. * Gets style options from style with specified name.
  90. *
  91. * @param string $name
  92. *
  93. * @return OutputFormatterStyleInterface
  94. *
  95. * @api
  96. */
  97. public function getStyle($name)
  98. {
  99. if (!$this->hasStyle($name)) {
  100. throw new \InvalidArgumentException('Undefined style: ' . $name);
  101. }
  102. return $this->styles[strtolower($name)];
  103. }
  104. /**
  105. * Formats a message according to the given styles.
  106. *
  107. * @param string $message The message to style
  108. *
  109. * @return string The styled message
  110. *
  111. * @api
  112. */
  113. public function format($message)
  114. {
  115. return preg_replace_callback('#<([a-z][a-z0-9_=;-]+)>(.*?)</\\1?>#i', array($this, 'replaceStyle'), $message);
  116. }
  117. /**
  118. * Replaces style of the output.
  119. *
  120. * @param array $match
  121. *
  122. * @return string The replaced style
  123. */
  124. private function replaceStyle($match)
  125. {
  126. if (!$this->isDecorated()) {
  127. return $match[2];
  128. }
  129. if (isset($this->styles[strtolower($match[1])])) {
  130. $style = $this->styles[strtolower($match[1])];
  131. } else {
  132. $style = $this->createStyleFromString($match[1]);
  133. if (false === $style) {
  134. return $match[0];
  135. }
  136. }
  137. return $style->apply($this->format($match[2]));
  138. }
  139. /**
  140. * Tries to create new style instance from string.
  141. *
  142. * @param string $string
  143. *
  144. * @return Symfony\Component\Console\Format\FormatterStyle|Boolean false if string is not format string
  145. */
  146. private function createStyleFromString($string)
  147. {
  148. if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
  149. return false;
  150. }
  151. $style = new OutputFormatterStyle();
  152. foreach ($matches as $match) {
  153. array_shift($match);
  154. if ('fg' == $match[0]) {
  155. $style->setForeground($match[1]);
  156. } elseif ('bg' == $match[0]) {
  157. $style->setBackground($match[1]);
  158. } else {
  159. $style->setOption($match[1]);
  160. }
  161. }
  162. return $style;
  163. }
  164. }