| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Formatter; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Contracts\Service\ResetInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* @author Jean-François Simon <[email protected]> |
| 19 |
*/ |
| 20 |
class OutputFormatterStyleStack implements ResetInterface |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var OutputFormatterStyleInterface[] |
| 24 |
*/ |
| 25 |
private array $styles = []; |
| 26 |
|
| 27 |
private OutputFormatterStyleInterface $emptyStyle; |
| 28 |
|
| 29 |
public function __construct(?OutputFormatterStyleInterface $emptyStyle = null) |
| 30 |
{ |
| 31 |
$this->emptyStyle = $emptyStyle ?? new OutputFormatterStyle(); |
| 32 |
$this->reset(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Resets stack (ie. empty internal arrays). |
| 37 |
*/ |
| 38 |
public function reset(): void |
| 39 |
{ |
| 40 |
$this->styles = []; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Pushes a style in the stack. |
| 45 |
*/ |
| 46 |
public function push(OutputFormatterStyleInterface $style): void |
| 47 |
{ |
| 48 |
$this->styles[] = $style; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Pops a style from the stack. |
| 53 |
* |
| 54 |
* @throws InvalidArgumentException When style tags incorrectly nested |
| 55 |
*/ |
| 56 |
public function pop(?OutputFormatterStyleInterface $style = null): OutputFormatterStyleInterface |
| 57 |
{ |
| 58 |
if (!$this->styles) { |
| 59 |
return $this->emptyStyle; |
| 60 |
} |
| 61 |
|
| 62 |
if (null === $style) { |
| 63 |
return array_pop($this->styles); |
| 64 |
} |
| 65 |
|
| 66 |
foreach (array_reverse($this->styles, true) as $index => $stackedStyle) { |
| 67 |
if ($style->apply('') === $stackedStyle->apply('')) { |
| 68 |
$this->styles = \array_slice($this->styles, 0, $index); |
| 69 |
|
| 70 |
return $stackedStyle; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
throw new InvalidArgumentException('Incorrectly nested style tag found.'); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Computes current style with stacks top codes. |
| 79 |
*/ |
| 80 |
public function getCurrent(): OutputFormatterStyleInterface |
| 81 |
{ |
| 82 |
if (!$this->styles) { |
| 83 |
return $this->emptyStyle; |
| 84 |
} |
| 85 |
|
| 86 |
return $this->styles[\count($this->styles) - 1]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @return $this |
| 91 |
*/ |
| 92 |
public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle): static |
| 93 |
{ |
| 94 |
$this->emptyStyle = $emptyStyle; |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
public function getEmptyStyle(): OutputFormatterStyleInterface |
| 100 |
{ |
| 101 |
return $this->emptyStyle; |
| 102 |
} |
| 103 |
} |