From 5666702ccce35d15eb329093f2463f628da51508 Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Tue, 3 Oct 2023 09:01:37 +0800 Subject: [PATCH] Error stacktrace with --dev enabled while conversion is in progress Also shorter constants. --- pm-convert | 8 ++-- src/Converters/Abstract/AbstractConverter.php | 8 ++-- src/Converters/Abstract/AbstractRequest.php | 2 +- src/Converters/Curl/CurlRequest.php | 2 +- src/Converters/Http/HttpRequest.php | 2 +- .../Postman20/Postman20Converter.php | 2 +- .../Postman21/Postman21Converter.php | 2 +- src/Converters/Wget/WgetRequest.php | 2 +- src/FileSystem.php | 4 +- src/Processor.php | 41 ++++++++++++------- 10 files changed, 44 insertions(+), 29 deletions(-) diff --git a/pm-convert b/pm-convert index 6a9ae06..cb64cd5 100755 --- a/pm-convert +++ b/pm-convert @@ -2,6 +2,8 @@ convert(); } catch (InvalidArgumentException $e) { - fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), PHP_EOL)); - print(implode(PHP_EOL, $processor->usage())); + fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), EOL)); + print(implode(EOL, $processor->usage())); die(1); } catch (Exception $e) { - fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), PHP_EOL)); + fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), EOL)); die(1); } diff --git a/src/Converters/Abstract/AbstractConverter.php b/src/Converters/Abstract/AbstractConverter.php index a2672ee..669be45 100644 --- a/src/Converters/Abstract/AbstractConverter.php +++ b/src/Converters/Abstract/AbstractConverter.php @@ -58,7 +58,7 @@ abstract class AbstractConverter implements ConverterContract */ protected function prepareOutputDir(string $outputPath): void { - $outputPath = sprintf('%s%s%s', $outputPath, DIRECTORY_SEPARATOR, static::OUTPUT_DIR); + $outputPath = sprintf('%s%s%s', $outputPath, DS, static::OUTPUT_DIR); $this->outputPath = FileSystem::makeDir($outputPath); } @@ -132,7 +132,7 @@ abstract class AbstractConverter implements ConverterContract static $dir_tree; foreach ($item->item as $subitem) { $dir_tree[] = $item->name; - $path = implode(DIRECTORY_SEPARATOR, $dir_tree); + $path = implode(DS, $dir_tree); if ($this->isItemFolder($subitem)) { $this->convertItem($subitem); } else { @@ -181,9 +181,9 @@ abstract class AbstractConverter implements ConverterContract */ protected function writeRequest(RequestContract $request, string $subpath = null): bool { - $filedir = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $subpath); + $filedir = sprintf('%s%s%s', $this->outputPath, DS, $subpath); $filedir = FileSystem::makeDir($filedir); - $filepath = sprintf('%s%s%s.%s', $filedir, DIRECTORY_SEPARATOR, $request->getName(), static::FILE_EXT); + $filepath = sprintf('%s%s%s.%s', $filedir, DS, $request->getName(), static::FILE_EXT); $content = $this->interpolate((string)$request); return file_put_contents($filepath, $content) > 0; } diff --git a/src/Converters/Abstract/AbstractRequest.php b/src/Converters/Abstract/AbstractRequest.php index ace779b..7af094e 100644 --- a/src/Converters/Abstract/AbstractRequest.php +++ b/src/Converters/Abstract/AbstractRequest.php @@ -92,7 +92,7 @@ abstract class AbstractRequest implements Stringable, RequestContract */ public function getName(): string { - return str_replace(DIRECTORY_SEPARATOR, '_', $this->name); + return str_replace(DS, '_', $this->name); } /** diff --git a/src/Converters/Curl/CurlRequest.php b/src/Converters/Curl/CurlRequest.php index 3dd06a8..259d807 100644 --- a/src/Converters/Curl/CurlRequest.php +++ b/src/Converters/Curl/CurlRequest.php @@ -78,6 +78,6 @@ class CurlRequest extends AbstractRequest $this->prepareBody() ); $output[] = rtrim(array_pop($output), '\ '); - return implode(PHP_EOL, array_merge($output, [''])); + return implode(EOL, array_merge($output, [''])); } } diff --git a/src/Converters/Http/HttpRequest.php b/src/Converters/Http/HttpRequest.php index 6297429..8a304be 100644 --- a/src/Converters/Http/HttpRequest.php +++ b/src/Converters/Http/HttpRequest.php @@ -69,6 +69,6 @@ class HttpRequest extends AbstractRequest $this->prepareHeaders(), $this->prepareBody() ); - return implode(PHP_EOL, $output); + return implode(EOL, $output); } } diff --git a/src/Converters/Postman20/Postman20Converter.php b/src/Converters/Postman20/Postman20Converter.php index 76adfc4..c068baa 100644 --- a/src/Converters/Postman20/Postman20Converter.php +++ b/src/Converters/Postman20/Postman20Converter.php @@ -57,7 +57,7 @@ class Postman20Converter extends AbstractConverter implements ConverterContract protected function writeCollection(): bool { $filedir = FileSystem::makeDir($this->outputPath); - $filepath = sprintf('%s%s%s.%s', $filedir, DIRECTORY_SEPARATOR, $this->collection->name(), static::FILE_EXT); + $filepath = sprintf('%s%s%s.%s', $filedir, DS, $this->collection->name(), static::FILE_EXT); return file_put_contents($filepath, $this->collection) > 0; } diff --git a/src/Converters/Postman21/Postman21Converter.php b/src/Converters/Postman21/Postman21Converter.php index 948de30..73f1b5d 100644 --- a/src/Converters/Postman21/Postman21Converter.php +++ b/src/Converters/Postman21/Postman21Converter.php @@ -57,7 +57,7 @@ class Postman21Converter extends AbstractConverter implements ConverterContract protected function writeCollection(): bool { $filedir = FileSystem::makeDir($this->outputPath); - $filepath = sprintf('%s%s%s.%s', $filedir, DIRECTORY_SEPARATOR, $this->collection->name(), static::FILE_EXT); + $filepath = sprintf('%s%s%s.%s', $filedir, DS, $this->collection->name(), static::FILE_EXT); return file_put_contents($filepath, $this->collection) > 0; } diff --git a/src/Converters/Wget/WgetRequest.php b/src/Converters/Wget/WgetRequest.php index 9613ab1..5d9f5c7 100644 --- a/src/Converters/Wget/WgetRequest.php +++ b/src/Converters/Wget/WgetRequest.php @@ -89,6 +89,6 @@ class WgetRequest extends AbstractRequest $output[] = sprintf("\t%s", $this->getUrl()); } } - return implode(PHP_EOL, array_merge($output, [''])); + return implode(EOL, array_merge($output, [''])); } } diff --git a/src/FileSystem.php b/src/FileSystem.php index 7cd4219..45c8f1a 100644 --- a/src/FileSystem.php +++ b/src/FileSystem.php @@ -25,7 +25,7 @@ class FileSystem public static function normalizePath(string $path): string { $path = str_replace('~', $_SERVER['HOME'], $path); - return rtrim($path, DIRECTORY_SEPARATOR); + return rtrim($path, DS); } /** @@ -101,7 +101,7 @@ class FileSystem $path = static::normalizePath($path); $records = array_diff(@scandir($path) ?: [], ['.', '..']); foreach ($records as &$record) { - $record = sprintf('%s%s%s', $path, DIRECTORY_SEPARATOR, $record); + $record = sprintf('%s%s%s', $path, DS, $record); } return $records; } diff --git a/src/Processor.php b/src/Processor.php index acd9a29..a8bab6b 100644 --- a/src/Processor.php +++ b/src/Processor.php @@ -78,6 +78,11 @@ class Processor */ protected Environment $env; + /** + * @var bool Flag to output some debug-specific messages + */ + protected bool $devMode = false; + /** * Constructor * @@ -98,7 +103,7 @@ class Processor protected function parseArgs(): void { if (count($this->argv) < 2) { - die(implode(PHP_EOL, $this->usage()) . PHP_EOL); + die(implode(EOL, $this->usage()) . EOL); } foreach ($this->argv as $idx => $arg) { switch ($arg) { @@ -108,7 +113,7 @@ class Processor $normpath = FileSystem::normalizePath($rawpath); if (!FileSystem::isCollectionFile($normpath)) { throw new InvalidArgumentException( - sprintf("not a valid collection:%s\t%s %s", PHP_EOL, $arg, $rawpath) + sprintf("not a valid collection:%s\t%s %s", EOL, $arg, $rawpath) ); } $this->collectionPaths[] = $this->argv[$idx + 1]; @@ -179,11 +184,15 @@ class Processor case '-v': case '--version': - die(implode(PHP_EOL, $this->version()) . PHP_EOL); + die(implode(EOL, $this->version()) . EOL); case '-h': case '--help': - die(implode(PHP_EOL, $this->usage()) . PHP_EOL); + die(implode(EOL, $this->usage()) . EOL); + + case '--dev': + $this->devMode = true; + break; } } if (empty($this->collectionPaths)) { @@ -277,22 +286,26 @@ class Processor $this->initCollections(); $this->initEnv(); $count = count($this->collections); - $current = 0; - $success = 0; - print(implode(PHP_EOL, array_merge($this->version(), $this->copyright())) . PHP_EOL . PHP_EOL); + $current = $success = 0; + print(implode(EOL, array_merge($this->version(), $this->copyright())) . EOL . EOL); foreach ($this->collections as $collectionName => $collection) { ++$current; - printf("Converting '%s' (%d/%d):%s", $collectionName, $current, $count, PHP_EOL); + printf("Converting '%s' (%d/%d):%s", $collectionName, $current, $count, EOL); foreach ($this->converters as $type => $exporter) { - printf('> %s%s', strtolower($type), PHP_EOL); - $outputPath = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $collectionName); + printf('> %s%s', strtolower($type), EOL); + $outputPath = sprintf('%s%s%s', $this->outputPath, DS, $collectionName); if (!empty($this->env)) { $exporter->withEnv($this->env); } - $exporter->convert($collection, $outputPath); - printf(' OK: %s%s', $exporter->getOutputPath(), PHP_EOL); + try { + $exporter->convert($collection, $outputPath); + printf(' OK: %s%s', $exporter->getOutputPath(), EOL); + } catch (Exception $e) { + printf(' ERROR %s: %s%s', $e->getCode(), $e->getMessage(), EOL); + $this->devMode && array_map(static fn ($line) => printf(" %s%s", $line, EOL), $e->getTrace()); + } } - print(PHP_EOL); + print(EOL); ++$success; } unset($this->converters, $type, $exporter, $outputPath, $this->collections, $collectionName, $collection); @@ -315,7 +328,7 @@ class Processor $timeFmt = 'sec'; } $ram = (memory_get_peak_usage(true) - $this->initRam) / 1024 / 1024; - printf("Converted %d/%d in %.2f $timeFmt using up to %.2f MiB RAM%s", $success, $count, $time, $ram, PHP_EOL); + printf("Converted %d/%d in %.2f $timeFmt using up to %.2f MiB RAM%s", $success, $count, $time, $ram, EOL); } /**