OutputFormatterStyleStack.php 2.5 KB

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