| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Console\Target; |
| 4: | |
| 5: | use Salient\Contract\Console\Format\FormatterInterface as Formatter; |
| 6: | use Salient\Contract\Console\Target\HasPrefix; |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | abstract class AbstractTargetWithPrefix extends AbstractTarget implements HasPrefix |
| 12: | { |
| 13: | private ?string $Prefix = null; |
| 14: | private int $PrefixWidth = 0; |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | abstract protected function doWrite(int $level, string $message, array $context): void; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | final public function write(int $level, string $message, array $context = []): void |
| 26: | { |
| 27: | $this->assertIsValid(); |
| 28: | |
| 29: | if ($this->Prefix !== null) { |
| 30: | $message = $this->Prefix . str_replace("\n", "\n" . $this->Prefix, $message); |
| 31: | } |
| 32: | |
| 33: | $this->doWrite($level, $message, $context); |
| 34: | } |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | final public function setPrefix(?string $prefix) |
| 40: | { |
| 41: | $this->assertIsValid(); |
| 42: | |
| 43: | if ($prefix === null || $prefix === '') { |
| 44: | $this->Prefix = null; |
| 45: | $this->PrefixWidth = 0; |
| 46: | } else { |
| 47: | $formatted = $this |
| 48: | ->getFormatter() |
| 49: | ->getTagFormat(Formatter::TAG_LOW_PRIORITY) |
| 50: | ->apply($prefix); |
| 51: | $this->PrefixWidth = mb_strlen($prefix); |
| 52: | $this->Prefix = $formatted; |
| 53: | } |
| 54: | return $this; |
| 55: | } |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | final public function getPrefix(): ?string |
| 61: | { |
| 62: | $this->assertIsValid(); |
| 63: | |
| 64: | return $this->Prefix; |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | public function getWidth(): ?int |
| 71: | { |
| 72: | $this->assertIsValid(); |
| 73: | |
| 74: | return 80 - $this->PrefixWidth; |
| 75: | } |
| 76: | } |
| 77: | |