Drupal investigation

OutputFormatterStyleStack.php 2.8KB

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