Просмотр файла vendor/symfony/console/Formatter/OutputFormatterStyleStack.php

Размер файла: 2.5Kb
  1. <?php
  2.  
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11.  
  12. namespace Symfony\Component\Console\Formatter;
  13.  
  14. use Symfony\Component\Console\Exception\InvalidArgumentException;
  15. use Symfony\Contracts\Service\ResetInterface;
  16.  
  17. /**
  18. * @author Jean-François Simon <contact@jfsimon.fr>
  19. */
  20. class OutputFormatterStyleStack implements ResetInterface
  21. {
  22. /**
  23. * @var OutputFormatterStyleInterface[]
  24. */
  25. private $styles;
  26.  
  27. private $emptyStyle;
  28.  
  29. public function __construct(OutputFormatterStyleInterface $emptyStyle = null)
  30. {
  31. $this->emptyStyle = $emptyStyle ?? new OutputFormatterStyle();
  32. $this->reset();
  33. }
  34.  
  35. /**
  36. * Resets stack (ie. empty internal arrays).
  37. */
  38. public function reset()
  39. {
  40. $this->styles = [];
  41. }
  42.  
  43. /**
  44. * Pushes a style in the stack.
  45. */
  46. public function push(OutputFormatterStyleInterface $style)
  47. {
  48. $this->styles[] = $style;
  49. }
  50.  
  51. /**
  52. * Pops a style from the stack.
  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.  
  64. if (null === $style) {
  65. return array_pop($this->styles);
  66. }
  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.  
  72. return $stackedStyle;
  73. }
  74. }
  75.  
  76. throw new InvalidArgumentException('Incorrectly nested style tag found.');
  77. }
  78.  
  79. /**
  80. * Computes current style with stacks top codes.
  81. *
  82. * @return OutputFormatterStyle
  83. */
  84. public function getCurrent()
  85. {
  86. if (empty($this->styles)) {
  87. return $this->emptyStyle;
  88. }
  89.  
  90. return $this->styles[\count($this->styles) - 1];
  91. }
  92.  
  93. /**
  94. * @return $this
  95. */
  96. public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle)
  97. {
  98. $this->emptyStyle = $emptyStyle;
  99.  
  100. return $this;
  101. }
  102.  
  103. /**
  104. * @return OutputFormatterStyleInterface
  105. */
  106. public function getEmptyStyle()
  107. {
  108. return $this->emptyStyle;
  109. }
  110. }