4 Commits

Author SHA1 Message Date
c790adf611 Merge pull request 'v1.2.1' (#4) from dev into master
Reviewed-on: #4
2023-08-13 02:51:31 +00:00
0ac5e64c17 Merge pull request 'v1.2.0' (#3) from dev into master
Reviewed-on: #3
2023-08-13 02:28:33 +00:00
c5f928dc47 Merge pull request 'v1.1.1' (#2) from dev into master
Reviewed-on: #2
2023-08-10 05:33:30 +00:00
9ed0ddf79b Merge pull request 'v1.1.0' (#1) from dev into master
Reviewed-on: #1
2023-08-04 01:28:43 +00:00
5 changed files with 24 additions and 23 deletions

View File

@@ -32,7 +32,7 @@ These formats are supported for now: `http`, `curl`, `wget`.
- tests, phpcs, psalm, etc.;
- web version.
## Install and upgrade
## Installation
```
composer global r axenov/pm-convert

View File

@@ -1,7 +1,7 @@
{
"name": "axenov/pm-convert",
"type": "library",
"description": "Postman collection converter",
"description": "Postman collection coverter",
"license": "MIT",
"homepage": "https://axenov.dev/",
"authors": [

View File

@@ -32,9 +32,9 @@ abstract class AbstractConverter implements ConverterContract
protected array $vars = [];
/**
* @var Environment|null
* @var Environment
*/
protected ?Environment $env = null;
protected Environment $env;
/**
* Sets an environment with vars
@@ -71,7 +71,7 @@ abstract class AbstractConverter implements ConverterContract
*/
protected function setVariables(): static
{
if (isset($this->collection?->variable)) {
if ($this->collection->variable) {
foreach ($this->collection->variable as $var) {
$this->vars["{{{$var->key}}}"] = $var->value;
}
@@ -173,11 +173,11 @@ abstract class AbstractConverter implements ConverterContract
*/
protected function interpolate(string $content): string
{
if (empty($this->vars) && !$this->env?->hasVars()) {
if (empty($this->vars) && $this->env->hasVars()) {
return $content;
}
$matches = [];
if (preg_match_all('/\{\{[a-zA-Z][a-zA-Z0-9]*}}/m', $content, $matches, PREG_PATTERN_ORDER) > 0) {
if (preg_match_all('/\{\{[a-zA-Z][a-zA-Z0-9]+}}/', $content, $matches, PREG_PATTERN_ORDER) > 0) {
foreach ($matches[0] as $key => $var) {
if (str_contains($content, $var)) {
$content = str_replace($var, $this->vars[$var] ?? $this->env[$var] ?? $var, $content);

View File

@@ -53,7 +53,7 @@ class WgetRequest extends AbstractRequest
$output[] = http_build_query($params);
return $output;
default:
return ["\t'$this->body' \ "];
return ["\t$this->body"];
}
}
@@ -75,6 +75,7 @@ class WgetRequest extends AbstractRequest
$this->prepareHeaders(),
$this->prepareBody()
);
$output[] = rtrim(array_pop($output), '\ ');
$output[] = "\t'$this->url'";
return implode(PHP_EOL, array_merge($output, ['']));
}

View File

@@ -7,21 +7,22 @@ namespace PmConverter;
use Exception;
use InvalidArgumentException;
use JsonException;
use PmConverter\Exceptions\{
CannotCreateDirectoryException,
DirectoryIsNotReadableException,
DirectoryIsNotWriteableException,
DirectoryNotExistsException};
use PmConverter\Exporters\{
ConverterContract,
ConvertFormat};
use PmConverter\Exceptions\CannotCreateDirectoryException;
use PmConverter\Exceptions\DirectoryIsNotReadableException;
use PmConverter\Exceptions\DirectoryIsNotWriteableException;
use PmConverter\Exceptions\DirectoryNotExistsException;
use PmConverter\Exporters\ConverterContract;
use PmConverter\Exporters\ConvertFormat;
/**
*
*/
class Processor
{
/**
* Converter version
*/
public const VERSION = '1.2.3';
public const VERSION = '1.2.1';
/**
* @var string[] Paths to collection files
@@ -88,6 +89,9 @@ class Processor
* Parses an array of arguments came from cli
*
* @return void
* @throws DirectoryIsNotWriteableException
* @throws DirectoryNotExistsException
* @throws DirectoryIsNotReadableException
*/
protected function parseArgs(): void
{
@@ -225,9 +229,6 @@ class Processor
*/
protected function initEnv(): void
{
if (!isset($this->envFile)) {
return;
}
$content = file_get_contents(FileSystem::normalizePath($this->envFile));
$content = json_decode($content, flags: JSON_THROW_ON_ERROR);
if (!property_exists($content, 'environment') || empty($content?->environment)) {
@@ -281,7 +282,7 @@ class Processor
{
$time = (hrtime(true) - $this->initTime) / 1_000_000;
$ram = (memory_get_peak_usage(true) - $this->initRam) / 1024 / 1024;
printf('Converted %d of %d in %.3f ms using %.3f MiB RAM%s', $success, $count, $time, $ram, PHP_EOL);
printf('Converted %d of %d in %.3f ms using %.3f MiB RAM', $success, $count, $time, $ram);
}
/**
@@ -317,7 +318,7 @@ class Processor
'',
'Possible ARGUMENTS:',
"\t-f, --file - a PATH to single collection located in PATH to convert from",
"\t-d, --dir - a directory with collections located in PATH to convert from",
"\t-d, --dir - a directory with collections located in COLLECTION_FILEPATH to convert from",
"\t-o, --output - a directory OUTPUT_PATH to put results in",
"\t-e, --env - use environment file with variable values to replace in request",
"\t-p, --preserve - do not delete OUTPUT_PATH (if exists)",
@@ -337,7 +338,6 @@ class Processor
"\t--http - generate raw *.http files (default)",
"\t--curl - generate shell scripts with curl command",
"\t--wget - generate shell scripts with wget command",
'',
'If no FORMATS specified then --http implied.',
'Any of FORMATS can be specified at the same time.',
'',