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;
15 * @author Jean-François Simon <contact@jfsimon.fr>
17 class OutputFormatterStyleStack
20 * @var OutputFormatterStyleInterface[]
25 * @var OutputFormatterStyleInterface
32 * @param OutputFormatterStyleInterface $emptyStyle
34 public function __construct(OutputFormatterStyleInterface $emptyStyle = null)
36 $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
41 * Resets stack (ie. empty internal arrays).
43 public function reset()
45 $this->styles = array();
49 * Pushes a style in the stack.
51 * @param OutputFormatterStyleInterface $style
53 public function push(OutputFormatterStyleInterface $style)
55 $this->styles[] = $style;
59 * Pops a style from the stack.
61 * @param OutputFormatterStyleInterface $style
63 * @return OutputFormatterStyleInterface
65 * @throws \InvalidArgumentException When style tags incorrectly nested
67 public function pop(OutputFormatterStyleInterface $style = null)
69 if (empty($this->styles)) {
70 return $this->emptyStyle;
73 if (null === $style) {
74 return array_pop($this->styles);
77 foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
78 if ($style->apply('') === $stackedStyle->apply('')) {
79 $this->styles = array_slice($this->styles, 0, $index);
85 throw new \InvalidArgumentException('Incorrectly nested style tag found.');
89 * Computes current style with stacks top codes.
91 * @return OutputFormatterStyle
93 public function getCurrent()
95 if (empty($this->styles)) {
96 return $this->emptyStyle;
99 return $this->styles[count($this->styles)-1];
103 * @param OutputFormatterStyleInterface $emptyStyle
105 * @return OutputFormatterStyleStack
107 public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle)
109 $this->emptyStyle = $emptyStyle;
115 * @return OutputFormatterStyleInterface
117 public function getEmptyStyle()
119 return $this->emptyStyle;