Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
56.52% |
13 / 23 |
|
28.57% |
4 / 14 |
CRAP | |
0.00% |
0 / 1 |
| IndentedOutputDecorator | |
56.52% |
13 / 23 |
|
28.57% |
4 / 14 |
37.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| write | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| writeln | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| indentMessages | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| setVerbosity | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getVerbosity | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isQuiet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isVerbose | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isVeryVerbose | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isDebug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setDecorated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isDecorated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setFormatter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFormatter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Console\Output; |
| 3 | |
| 4 | use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 5 | use Symfony\Component\Console\Output\OutputInterface; |
| 6 | |
| 7 | class IndentedOutputDecorator implements OutputInterface |
| 8 | { |
| 9 | private string $indentation; |
| 10 | |
| 11 | private bool $startWithIndenting = true; |
| 12 | |
| 13 | public function __construct( |
| 14 | private readonly OutputInterface $output, |
| 15 | int $indent = 4 |
| 16 | ) { |
| 17 | $this->indentation = str_repeat(' ', $indent); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @param string|iterable<int, string> $messages |
| 22 | */ |
| 23 | public function write(string|iterable $messages, bool $newline = false, int $options = 0): void |
| 24 | { |
| 25 | $indentedMessages = $this->indentMessages($messages); |
| 26 | $this->startWithIndenting = false; |
| 27 | $this->output->write($indentedMessages, $newline, $options); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param string|iterable<int, string> $messages |
| 32 | */ |
| 33 | public function writeln(string|iterable $messages, int $options = 0): void |
| 34 | { |
| 35 | $indentedMessages = $this->indentMessages($messages); |
| 36 | $this->startWithIndenting = true; |
| 37 | $this->output->writeln($indentedMessages, $options); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param string|iterable<int, string> $messages |
| 42 | * @return iterable<int, string> |
| 43 | */ |
| 44 | private function indentMessages(string|iterable $messages): iterable |
| 45 | { |
| 46 | if (is_array($messages)) { |
| 47 | $messages = implode("\n", $messages); |
| 48 | } |
| 49 | if ($this->startWithIndenting) { |
| 50 | $messages = $this->indentation . $messages; |
| 51 | } |
| 52 | $messages = str_replace("\n", "\n" . $this->indentation, $messages); |
| 53 | |
| 54 | return explode("\n", $messages); |
| 55 | } |
| 56 | |
| 57 | public function setVerbosity(int $level): void |
| 58 | { |
| 59 | $this->output->setVerbosity($level); |
| 60 | } |
| 61 | |
| 62 | public function getVerbosity(): int |
| 63 | { |
| 64 | return $this->output->getVerbosity(); |
| 65 | } |
| 66 | |
| 67 | public function isQuiet(): bool |
| 68 | { |
| 69 | return $this->output->isQuiet(); |
| 70 | } |
| 71 | |
| 72 | public function isVerbose(): bool |
| 73 | { |
| 74 | return $this->output->isVerbose(); |
| 75 | } |
| 76 | |
| 77 | public function isVeryVerbose(): bool |
| 78 | { |
| 79 | return $this->output->isVeryVerbose(); |
| 80 | } |
| 81 | |
| 82 | public function isDebug(): bool |
| 83 | { |
| 84 | return $this->output->isDebug(); |
| 85 | } |
| 86 | |
| 87 | public function setDecorated(bool $decorated): void |
| 88 | { |
| 89 | $this->output->setDecorated($decorated); |
| 90 | } |
| 91 | |
| 92 | public function isDecorated(): bool |
| 93 | { |
| 94 | return $this->output->isDecorated(); |
| 95 | } |
| 96 | |
| 97 | public function setFormatter(OutputFormatterInterface $formatter): void |
| 98 | { |
| 99 | $this->output->setFormatter($formatter); |
| 100 | } |
| 101 | |
| 102 | public function getFormatter(): OutputFormatterInterface |
| 103 | { |
| 104 | return $this->output->getFormatter(); |
| 105 | } |
| 106 | } |