4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Formatter;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
17 * @author Jean-François Simon <contact@jfsimon.fr>
19 class OutputFormatterStyleStack
22 * @var OutputFormatterStyleInterface[]
27 * @var OutputFormatterStyleInterface
34 * @param OutputFormatterStyleInterface|null $emptyStyle
36 public function __construct(OutputFormatterStyleInterface $emptyStyle = null)
38 $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
43 * Resets stack (ie. empty internal arrays).
45 public function reset()
47 $this->styles = array();
51 * Pushes a style in the stack.
53 * @param OutputFormatterStyleInterface $style
55 public function push(OutputFormatterStyleInterface $style)
57 $this->styles[] = $style;
61 * Pops a style from the stack.
63 * @param OutputFormatterStyleInterface|null $style
65 * @return OutputFormatterStyleInterface
67 * @throws InvalidArgumentException When style tags incorrectly nested
69 public function pop(OutputFormatterStyleInterface $style = null)
71 if (empty($this->styles)) {
72 return $this->emptyStyle;
75 if (null === $style) {
76 return array_pop($this->styles);
79 foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
80 if ($style->apply('') === $stackedStyle->apply('')) {
81 $this->styles = array_slice($this->styles, 0, $index);
87 throw new InvalidArgumentException('Incorrectly nested style tag found.');
91 * Computes current style with stacks top codes.
93 * @return OutputFormatterStyle
95 public function getCurrent()
97 if (empty($this->styles)) {
98 return $this->emptyStyle;
101 return $this->styles[count($this->styles) - 1];
105 * @param OutputFormatterStyleInterface $emptyStyle
109 public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle)
111 $this->emptyStyle = $emptyStyle;
117 * @return OutputFormatterStyleInterface
119 public function getEmptyStyle()
121 return $this->emptyStyle;