From b73989963f4ddb9e561033b63dd746b206d518f2 Mon Sep 17 00:00:00 2001 From: inhere Date: Sat, 29 Jun 2019 11:53:16 +0800 Subject: [PATCH] update some logic --- libs/cli-utils/src/Cli.php | 5 +++-- libs/cli-utils/src/Highlighter.php | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libs/cli-utils/src/Cli.php b/libs/cli-utils/src/Cli.php index 1e0d88c..505d226 100644 --- a/libs/cli-utils/src/Cli.php +++ b/libs/cli-utils/src/Cli.php @@ -178,9 +178,9 @@ public static function log(string $msg, array $data = [], string $type = 'info', } $optString = $userOpts ? ' ' . implode(' ', $userOpts) : ''; + $dataString = $data ? PHP_EOL . json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) : ''; - self::write(sprintf('%s [%s]%s %s %s', date('Y/m/d H:i:s'), $type, $optString, trim($msg), - $data ? PHP_EOL . json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) : '')); + self::writef('%s [%s]%s %s %s', date('Y/m/d H:i:s'), $type, $optString, trim($msg), $dataString); } /******************************************************************************* @@ -245,6 +245,7 @@ public static function isAnsiSupport(): bool */ public static function isInteractive($fileDescriptor): bool { + /** @noinspection PhpComposerExtensionStubsInspection */ return function_exists('posix_isatty') && @posix_isatty($fileDescriptor); } diff --git a/libs/cli-utils/src/Highlighter.php b/libs/cli-utils/src/Highlighter.php index 6146423..2dc8f80 100644 --- a/libs/cli-utils/src/Highlighter.php +++ b/libs/cli-utils/src/Highlighter.php @@ -123,8 +123,18 @@ public function highlight(string $source, bool $withLineNumber = false): string return implode(PHP_EOL, $lines); } + /** + * @param string $file + * @param bool $withLineNumber + * + * @return string + */ public function highlightFile(string $file, bool $withLineNumber = false): string { + if (!file_exists($file)) { + throw new InvalidArgumentException("the target file is not exist! file: $file"); + } + $source = file_get_contents($file); return $this->highlight($source, $withLineNumber);