OutputFormatterStyleStack.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * @author Jean-François Simon <contact@jfsimon.fr>
  13. */
  14. class OutputFormatterStyleStack
  15. {
  16. /**
  17. * @var OutputFormatterStyleInterface[]
  18. */
  19. private $styles;
  20. /**
  21. * @var OutputFormatterStyleInterface
  22. */
  23. private $emptyStyle;
  24. /**
  25. * Constructor.
  26. *
  27. * @param OutputFormatterStyleInterface|null $emptyStyle
  28. */
  29. public function __construct(OutputFormatterStyleInterface $emptyStyle = null)
  30. {
  31. $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
  32. $this->reset();
  33. }
  34. /**
  35. * Resets stack (ie. empty internal arrays).
  36. */
  37. public function reset()
  38. {
  39. $this->styles = array();
  40. }
  41. /**
  42. * Pushes a style in the stack.
  43. *
  44. * @param OutputFormatterStyleInterface $style
  45. */
  46. public function push(OutputFormatterStyleInterface $style)
  47. {
  48. $this->styles[] = $style;
  49. }
  50. /**
  51. * Pops a style from the stack.
  52. *
  53. * @param OutputFormatterStyleInterface|null $style
  54. *
  55. * @return OutputFormatterStyleInterface
  56. *
  57. * @throws \InvalidArgumentException When style tags incorrectly nested
  58. */
  59. public function pop(OutputFormatterStyleInterface $style = null)
  60. {
  61. if (empty($this->styles)) {
  62. return $this->emptyStyle;
  63. }
  64. if (null === $style) {
  65. return array_pop($this->styles);
  66. }
  67. foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
  68. if ($style->apply('') === $stackedStyle->apply('')) {
  69. $this->styles = array_slice($this->styles, 0, $index);
  70. return $stackedStyle;
  71. }
  72. }
  73. throw new \InvalidArgumentException('Incorrectly nested style tag found.');
  74. }
  75. /**
  76. * Computes current style with stacks top codes.
  77. *
  78. * @return OutputFormatterStyle
  79. */
  80. public function getCurrent()
  81. {
  82. if (empty($this->styles)) {
  83. return $this->emptyStyle;
  84. }
  85. return $this->styles[count($this->styles) - 1];
  86. }
  87. /**
  88. * @param OutputFormatterStyleInterface $emptyStyle
  89. *
  90. * @return OutputFormatterStyleStack
  91. */
  92. public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle)
  93. {
  94. $this->emptyStyle = $emptyStyle;
  95. return $this;
  96. }
  97. /**
  98. * @return OutputFormatterStyleInterface
  99. */
  100. public function getEmptyStyle()
  101. {
  102. return $this->emptyStyle;
  103. }
  104. }