OutputFormatterStyleStack.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. public function reset(): void
  32. {
  33. $this->styles = [];
  34. }
  35. /**
  36. * Pushes a style in the stack.
  37. */
  38. public function push(OutputFormatterStyleInterface $style): void
  39. {
  40. $this->styles[] = $style;
  41. }
  42. /**
  43. * Pops a style from the stack.
  44. *
  45. * @throws InvalidArgumentException When style tags incorrectly nested
  46. */
  47. public function pop(?OutputFormatterStyleInterface $style = null): OutputFormatterStyleInterface
  48. {
  49. if (!$this->styles) {
  50. return $this->emptyStyle;
  51. }
  52. if (null === $style) {
  53. return array_pop($this->styles);
  54. }
  55. foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
  56. if ($style->apply('') === $stackedStyle->apply('')) {
  57. $this->styles = \array_slice($this->styles, 0, $index);
  58. return $stackedStyle;
  59. }
  60. }
  61. throw new InvalidArgumentException('Incorrectly nested style tag found.');
  62. }
  63. /**
  64. * Computes current style with stacks top codes.
  65. */
  66. public function getCurrent(): OutputFormatterStyleInterface
  67. {
  68. if (!$this->styles) {
  69. return $this->emptyStyle;
  70. }
  71. return $this->styles[\count($this->styles) - 1];
  72. }
  73. /**
  74. * @return $this
  75. */
  76. public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle): static
  77. {
  78. $this->emptyStyle = $emptyStyle;
  79. return $this;
  80. }
  81. public function getEmptyStyle(): OutputFormatterStyleInterface
  82. {
  83. return $this->emptyStyle;
  84. }
  85. }