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