Skip to content

Commit

Permalink
[Console] Added formatter style stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon committed Apr 11, 2012
1 parent 02e1b81 commit b63bd0e
Showing 1 changed file with 93 additions and 0 deletions.
@@ -0,0 +1,93 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Formatter;

/**
* @author Jean-François Simon <contact@jfsimon.fr>
*/
class OutputFormatterStyleStack
{
/**
* @var OutputFormatterStyle[]
*/
private $styles;

/**
* Constructor.
*/
public function __construct()
{
$this->reset();
}

/**
* Resets stack (ie. empty internal arrays).
*/
public function reset()
{
$this->styles = array();
}

/**
* Pushes a style in the stack.
*
* @param OutputFormatterStyleInterface $style
*/
public function push(OutputFormatterStyleInterface $style)
{
$this->styles[] = $style;
}

/**
* Pops a style from the stack.
*
* @param OutputFormatterStyleInterface $style
*
* @return OutputFormatterStyleInterface
*
* @throws \InvalidArgumentException When style tags incorrectly nested
*/
public function pop(OutputFormatterStyleInterface $style = null)
{
if (empty($this->styles)) {
return new OutputFormatterStyle();
}

if (null === $style) {
return array_pop($this->styles);
}

foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
if ($style->apply('') === $stackedStyle->apply('')) {
$this->styles = array_slice($this->styles, 0, $index);

return $stackedStyle;
}
}

throw new \InvalidArgumentException('Incorrectly nested style tag found.');
}

/**
* Computes current style with stacks top codes.
*
* @return OutputFormatterStyle
*/
public function getCurrent()
{
if (empty($this->styles)) {
return new OutputFormatterStyle();
}

return $this->styles[count($this->styles)-1];
}
}

0 comments on commit b63bd0e

Please sign in to comment.