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[]
28 public function __construct(OutputFormatterStyleInterface $emptyStyle = null)
30 $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle();
35 * Resets stack (ie. empty internal arrays).
37 public function reset()
39 $this->styles = array();
43 * Pushes a style in the stack.
45 public function push(OutputFormatterStyleInterface $style)
47 $this->styles[] = $style;
51 * Pops a style from the stack.
53 * @return OutputFormatterStyleInterface
55 * @throws InvalidArgumentException When style tags incorrectly nested
57 public function pop(OutputFormatterStyleInterface $style = null)
59 if (empty($this->styles)) {
60 return $this->emptyStyle;
63 if (null === $style) {
64 return array_pop($this->styles);
67 foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
68 if ($style->apply('') === $stackedStyle->apply('')) {
69 $this->styles = \array_slice($this->styles, 0, $index);
75 throw new InvalidArgumentException('Incorrectly nested style tag found.');
79 * Computes current style with stacks top codes.
81 * @return OutputFormatterStyle
83 public function getCurrent()
85 if (empty($this->styles)) {
86 return $this->emptyStyle;
89 return $this->styles[\count($this->styles) - 1];
95 public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle)
97 $this->emptyStyle = $emptyStyle;
103 * @return OutputFormatterStyleInterface
105 public function getEmptyStyle()
107 return $this->emptyStyle;