Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Console] Added formatter style stack.
- Loading branch information
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
src/Symfony/Component/Console/Formatter/OutputFormatterStyleStack.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} |