Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Formatter; Chris@0: Chris@0: use Symfony\Component\Console\Exception\InvalidArgumentException; Chris@0: Chris@0: /** Chris@0: * @author Jean-François Simon Chris@0: */ Chris@0: class OutputFormatterStyleStack Chris@0: { Chris@0: /** Chris@0: * @var OutputFormatterStyleInterface[] Chris@0: */ Chris@0: private $styles; Chris@0: Chris@0: private $emptyStyle; Chris@0: Chris@0: public function __construct(OutputFormatterStyleInterface $emptyStyle = null) Chris@0: { Chris@0: $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle(); Chris@0: $this->reset(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resets stack (ie. empty internal arrays). Chris@0: */ Chris@0: public function reset() Chris@0: { Chris@17: $this->styles = []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Pushes a style in the stack. Chris@0: */ Chris@0: public function push(OutputFormatterStyleInterface $style) Chris@0: { Chris@0: $this->styles[] = $style; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Pops a style from the stack. Chris@0: * Chris@0: * @return OutputFormatterStyleInterface Chris@0: * Chris@0: * @throws InvalidArgumentException When style tags incorrectly nested Chris@0: */ Chris@0: public function pop(OutputFormatterStyleInterface $style = null) Chris@0: { Chris@0: if (empty($this->styles)) { Chris@0: return $this->emptyStyle; Chris@0: } Chris@0: Chris@0: if (null === $style) { Chris@0: return array_pop($this->styles); Chris@0: } Chris@0: Chris@0: foreach (array_reverse($this->styles, true) as $index => $stackedStyle) { Chris@0: if ($style->apply('') === $stackedStyle->apply('')) { Chris@17: $this->styles = \array_slice($this->styles, 0, $index); Chris@0: Chris@0: return $stackedStyle; Chris@0: } Chris@0: } Chris@0: Chris@0: throw new InvalidArgumentException('Incorrectly nested style tag found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Computes current style with stacks top codes. Chris@0: * Chris@0: * @return OutputFormatterStyle Chris@0: */ Chris@0: public function getCurrent() Chris@0: { Chris@0: if (empty($this->styles)) { Chris@0: return $this->emptyStyle; Chris@0: } Chris@0: Chris@17: return $this->styles[\count($this->styles) - 1]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return $this Chris@0: */ Chris@0: public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle) Chris@0: { Chris@0: $this->emptyStyle = $emptyStyle; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return OutputFormatterStyleInterface Chris@0: */ Chris@0: public function getEmptyStyle() Chris@0: { Chris@0: return $this->emptyStyle; Chris@0: } Chris@0: }