| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Console\Target; |
| 4: | |
| 5: | use Salient\Contract\Console\ConsoleInterface as Console; |
| 6: | use Salient\Core\Facade\Err; |
| 7: | use Salient\Utility\Exception\InvalidArgumentTypeException; |
| 8: | use Salient\Utility\File; |
| 9: | use DateTime; |
| 10: | use DateTimeZone; |
| 11: | use LogicException; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | class StreamTarget extends AbstractStreamTarget |
| 19: | { |
| 20: | public const DEFAULT_TIMESTAMP_FORMAT = '[d M y H:i:s.vO] '; |
| 21: | |
| 22: | |
| 23: | protected $Stream; |
| 24: | protected ?string $Uri; |
| 25: | protected bool $IsStdout; |
| 26: | protected bool $IsStderr; |
| 27: | protected bool $IsTty; |
| 28: | protected bool $Close; |
| 29: | protected bool $AddTimestamp; |
| 30: | protected string $TimestampFormat; |
| 31: | protected ?DateTimeZone $Timezone; |
| 32: | protected ?string $Filename = null; |
| 33: | |
| 34: | private static bool $HasPendingClearLine = false; |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | public function __construct( |
| 48: | $stream, |
| 49: | bool $close = false, |
| 50: | ?bool $addTimestamp = null, |
| 51: | string $timestampFormat = StreamTarget::DEFAULT_TIMESTAMP_FORMAT, |
| 52: | $timezone = null |
| 53: | ) { |
| 54: | if (!File::isStream($stream)) { |
| 55: | throw new InvalidArgumentTypeException(1, 'stream', 'resource (stream)', $stream); |
| 56: | } |
| 57: | |
| 58: | $this->applyStream($stream); |
| 59: | $this->Close = $close; |
| 60: | $this->AddTimestamp = $addTimestamp |
| 61: | || ($addTimestamp === null && !$this->IsStdout && !$this->IsStderr); |
| 62: | if ($this->AddTimestamp) { |
| 63: | $this->TimestampFormat = $timestampFormat; |
| 64: | $this->Timezone = is_string($timezone) |
| 65: | ? new DateTimeZone($timezone) |
| 66: | : $timezone; |
| 67: | } |
| 68: | } |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | public static function fromFile( |
| 81: | string $filename, |
| 82: | ?bool $addTimestamp = null, |
| 83: | string $timestampFormat = StreamTarget::DEFAULT_TIMESTAMP_FORMAT, |
| 84: | $timezone = null |
| 85: | ): self { |
| 86: | File::create($filename, 0600); |
| 87: | $stream = File::open($filename, 'a'); |
| 88: | $instance = new self($stream, true, $addTimestamp, $timestampFormat, $timezone); |
| 89: | $instance->Filename = $filename; |
| 90: | return $instance; |
| 91: | } |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | public function __destruct() |
| 97: | { |
| 98: | $this->close(); |
| 99: | } |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | public function close(): void |
| 105: | { |
| 106: | if (!$this->Stream) { |
| 107: | return; |
| 108: | } |
| 109: | |
| 110: | if ( |
| 111: | $this->IsTty |
| 112: | && self::$HasPendingClearLine |
| 113: | && File::isStream($this->Stream) |
| 114: | ) { |
| 115: | $this->clearLine(true); |
| 116: | } |
| 117: | |
| 118: | if ($this->Close) { |
| 119: | File::close($this->Stream, $this->Filename ?? $this->Uri); |
| 120: | } |
| 121: | |
| 122: | $this->Stream = null; |
| 123: | $this->Uri = null; |
| 124: | $this->IsStdout = false; |
| 125: | $this->IsStderr = false; |
| 126: | $this->IsTty = false; |
| 127: | $this->Filename = null; |
| 128: | } |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | public function isStdout(): bool |
| 134: | { |
| 135: | return $this->IsStdout; |
| 136: | } |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | public function isStderr(): bool |
| 142: | { |
| 143: | return $this->IsStderr; |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | public function isTty(): bool |
| 150: | { |
| 151: | return $this->IsTty; |
| 152: | } |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | public function getUri(): ?string |
| 158: | { |
| 159: | return $this->Filename ?? $this->Uri; |
| 160: | } |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | public function reopen(?string $filename = null): void |
| 166: | { |
| 167: | $this->assertIsValid(); |
| 168: | |
| 169: | |
| 170: | |
| 171: | $filename ??= $this->Filename; |
| 172: | if ($filename === null || !$this->Close) { |
| 173: | return; |
| 174: | } |
| 175: | |
| 176: | File::close($this->Stream, $this->Filename ?? $this->Uri); |
| 177: | File::create($filename, 0600); |
| 178: | $this->applyStream(File::open($filename, 'a')); |
| 179: | $this->Filename = $filename; |
| 180: | } |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | protected function doWrite(int $level, string $message, array $context): void |
| 186: | { |
| 187: | $this->assertIsValid(); |
| 188: | |
| 189: | if ($this->AddTimestamp) { |
| 190: | $now = (new DateTime('now', $this->Timezone))->format($this->TimestampFormat); |
| 191: | $message = $now . str_replace("\n", "\n" . $now, $message); |
| 192: | } |
| 193: | |
| 194: | |
| 195: | |
| 196: | if ($this->IsTty) { |
| 197: | if (self::$HasPendingClearLine) { |
| 198: | $this->clearLine($level < Console::LEVEL_WARNING); |
| 199: | } |
| 200: | if ($message === "\r") { |
| 201: | return; |
| 202: | } |
| 203: | if ($message !== '' && $message[-1] === "\r") { |
| 204: | File::write($this->Stream, self::NO_AUTO_WRAP . rtrim($message, "\r")); |
| 205: | self::$HasPendingClearLine = true; |
| 206: | return; |
| 207: | } |
| 208: | } |
| 209: | |
| 210: | File::write($this->Stream, rtrim($message, "\r") . "\n"); |
| 211: | } |
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | protected function applyStream($stream): void |
| 217: | { |
| 218: | $this->Stream = $stream; |
| 219: | $this->Uri = stream_get_meta_data($stream)['uri'] ?? null; |
| 220: | $this->IsStdout = $this->Uri === 'php://stdout'; |
| 221: | $this->IsStderr = $this->Uri === 'php://stderr'; |
| 222: | $this->IsTty = stream_isatty($stream); |
| 223: | |
| 224: | stream_set_write_buffer($stream, 0); |
| 225: | } |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | private function clearLine(bool $checkForError = false): void |
| 231: | { |
| 232: | $this->assertIsValid(); |
| 233: | |
| 234: | $data = $checkForError |
| 235: | && Err::isLoaded() |
| 236: | && Err::isShuttingDownOnError() |
| 237: | ? self::AUTO_WRAP . "\n" |
| 238: | : "\r" . self::CLEAR_LINE . self::AUTO_WRAP; |
| 239: | File::write($this->Stream, $data); |
| 240: | self::$HasPendingClearLine = false; |
| 241: | } |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | protected function assertIsValid(): void |
| 247: | { |
| 248: | if (!$this->Stream) { |
| 249: | throw new LogicException('Target is closed'); |
| 250: | } |
| 251: | } |
| 252: | } |
| 253: | |