v1.2.0 #3

Merged
anthony merged 4 commits from dev into master 2023-08-13 02:28:35 +00:00
8 changed files with 194 additions and 103 deletions
Showing only changes of commit 9947987c20 - Show all commits

View File

@ -82,7 +82,7 @@ abstract class AbstractConverter
*/ */
protected function initRequest(object $item): RequestContract protected function initRequest(object $item): RequestContract
{ {
$request_class = static::REQUEST; $request_class = static::REQUEST_CLASS;
$result = new $request_class(); $result = new $request_class();
$result->setName($item->name); $result->setName($item->name);
$result->setDescription($item->request?->description ?? null); $result->setDescription($item->request?->description ?? null);

View File

@ -8,14 +8,14 @@ use PmConverter\Exporters\Http\HttpRequest;
use PmConverter\Exporters\RequestContract; use PmConverter\Exporters\RequestContract;
/** /**
* * Class to determine file content with any request format
*/ */
abstract class AbstractRequest implements RequestContract abstract class AbstractRequest implements RequestContract
{ {
/** /**
* @var string * @var string
*/ */
protected string $http = 'HTTP/1.1'; //TODO verb protected string $http = 'HTTP/1.1'; //TODO proto
/** /**
* @var string * @var string
@ -53,6 +53,8 @@ abstract class AbstractRequest implements RequestContract
protected string $url; protected string $url;
/** /**
* Sets name from collection item to request object
*
* @param string $name * @param string $name
* @return HttpRequest * @return HttpRequest
*/ */
@ -63,6 +65,8 @@ abstract class AbstractRequest implements RequestContract
} }
/** /**
* Returns name of request
*
* @return string * @return string
*/ */
public function getName(): string public function getName(): string
@ -71,6 +75,8 @@ abstract class AbstractRequest implements RequestContract
} }
/** /**
* Sets description from collection item to request object
*
* @param string|null $description * @param string|null $description
* @return HttpRequest * @return HttpRequest
*/ */
@ -81,6 +87,8 @@ abstract class AbstractRequest implements RequestContract
} }
/** /**
* Sets HTTP verb from collection item to request object
*
* @param string $verb * @param string $verb
* @return HttpRequest * @return HttpRequest
*/ */
@ -91,6 +99,8 @@ abstract class AbstractRequest implements RequestContract
} }
/** /**
* Sets URL from collection item to request object
*
* @param string $url * @param string $url
* @return HttpRequest * @return HttpRequest
*/ */
@ -101,6 +111,8 @@ abstract class AbstractRequest implements RequestContract
} }
/** /**
* Sets headers from collection item to request object
*
* @param object[]|null $headers * @param object[]|null $headers
* @return $this * @return $this
*/ */
@ -116,6 +128,8 @@ abstract class AbstractRequest implements RequestContract
} }
/** /**
* Sets body mode from collection item to request object
*
* @param string $bodymode * @param string $bodymode
* @return HttpRequest * @return HttpRequest
*/ */
@ -126,8 +140,10 @@ abstract class AbstractRequest implements RequestContract
} }
/** /**
* @param string $body * Sets body from collection item to request object
* @return HttpRequest *
* @param object $body
* @return $this
*/ */
public function setBody(object $body): static public function setBody(object $body): static
{ {
@ -153,11 +169,29 @@ abstract class AbstractRequest implements RequestContract
} }
/** /**
* @return string * Returns array of description lines
*
* @return array
*/ */
abstract protected function prepareBody(): ?string; abstract protected function prepareDescription(): array;
/** /**
* Returns array of headers
*
* @return array
*/
abstract protected function prepareHeaders(): array;
/**
* Returns array of request body lines
*
* @return array
*/
abstract protected function prepareBody(): array;
/**
* Converts request object to string to be written in result file
*
* @return string * @return string
*/ */
abstract public function __toString(): string; abstract public function __toString(): string;

View File

@ -11,7 +11,8 @@ use PmConverter\Exporters\{
class CurlConverter extends AbstractConverter implements ConverterContract class CurlConverter extends AbstractConverter implements ConverterContract
{ {
protected const FILE_EXT = 'sh'; protected const FILE_EXT = 'sh';
protected const OUTPUT_DIR = 'curl'; protected const OUTPUT_DIR = 'curl';
protected const REQUEST = CurlRequest::class; protected const REQUEST_CLASS = CurlRequest::class;
} }

View File

@ -7,59 +7,77 @@ namespace PmConverter\Exporters\Curl;
use PmConverter\Exporters\Abstract\AbstractRequest; use PmConverter\Exporters\Abstract\AbstractRequest;
/** /**
* * Class to determine file content with curl request format
*/ */
class CurlRequest extends AbstractRequest class CurlRequest extends AbstractRequest
{ {
/** /**
* @return string * @inheritDoc
*/ */
protected function prepareBody(): ?string protected function prepareDescription(): array
{ {
switch ($this->bodymode) { return empty($this->description)
case 'formdata': ? []
$body = []; : ['# ' . str_replace("\n", "\n# ", $this->description), ''];
foreach ($this->body as $data) {
$body[] = sprintf(
"%s\t--form '%s=%s' \ ",
isset($data->disabled) ? '# ' : '',
$data->key,
$data->type === 'file' ? "@$data->src" : $data->value
);
}
return implode(PHP_EOL, $body);
default:
return $this->body;
}
} }
/** /**
* @return string * @inheritDoc
*/ */
public function __toString(): string protected function prepareHeaders(): array
{ {
$output[] = '#!/bin/sh'; $output = [];
if ($this->description) {
$output[] = '# ' . str_replace("\n", "\n# ", $this->description);
$output[] = '';
}
$output[] = "curl \ ";
$output[] = "\t--http1.1 \ "; //TODO verb
$output[] = "\t--request $this->verb \ ";
$output[] = "\t--location $this->url \ ";
foreach ($this->headers as $header_key => $header) { foreach ($this->headers as $header_key => $header) {
if ($header['disabled']) { if ($header['disabled']) {
continue; continue;
} }
$output[] = sprintf("\t--header '%s=%s' \ ", $header_key, $header['value']); $output[] = sprintf("\t--header '%s=%s' \ ", $header_key, $header['value']);
} }
if (!is_null($body = $this->prepareBody())) { return $output;
$output[] = match ($this->bodymode) {
'formdata' => $body,
default => "\t--data '$body'",
};
} }
/**
* @inheritDoc
*/
protected function prepareBody(): array
{
$output = [];
switch ($this->bodymode) {
case 'formdata':
foreach ($this->body as $data) {
$output[] = sprintf(
"%s\t--form '%s=%s' \ ",
isset($data->disabled) ? '# ' : '',
$data->key,
$data->type === 'file' ? "@$data->src" : $data->value
);
}
break;
default:
$output = ["\t--data '$this->body'"];
break;
}
return $output;
}
/**
* @inheritDoc
*/
public function __toString(): string
{
$output = array_merge(
['#!/bin/sh'],
$this->prepareDescription(),
[
"curl \ ",
"\t--http1.1 \ ", //TODO proto
"\t--request $this->verb \ ",
"\t--location $this->url \ ",
],
$this->prepareHeaders(),
$this->prepareBody()
);
$output[] = rtrim(array_pop($output), '\ '); $output[] = rtrim(array_pop($output), '\ ');
return implode(PHP_EOL, $output); return implode(PHP_EOL, array_merge($output, ['']));
} }
} }

View File

@ -11,7 +11,8 @@ use PmConverter\Exporters\{
class HttpConverter extends AbstractConverter implements ConverterContract class HttpConverter extends AbstractConverter implements ConverterContract
{ {
protected const FILE_EXT = 'http'; protected const FILE_EXT = 'http';
protected const OUTPUT_DIR = 'http'; protected const OUTPUT_DIR = 'http';
protected const REQUEST = HttpRequest::class; protected const REQUEST_CLASS = HttpRequest::class;
} }

View File

@ -7,47 +7,64 @@ namespace PmConverter\Exporters\Http;
use PmConverter\Exporters\Abstract\AbstractRequest; use PmConverter\Exporters\Abstract\AbstractRequest;
/** /**
* * Class to determine file content with http request format
*/ */
class HttpRequest extends AbstractRequest class HttpRequest extends AbstractRequest
{ {
/** /**
* @return string * @inheritDoc
*/ */
protected function prepareBody(): ?string protected function prepareDescription(): array
{ {
switch ($this->bodymode) { return empty($this->description)
case 'formdata': ? []
$body = []; : ['# ' . str_replace("\n", "\n# ", $this->description), ''];
foreach ($this->body as $data) {
$body[] = sprintf(
'%s%s=%s',
empty($data->disabled) ? '' : '# ',
$data->key,
$data->type === 'file' ? "$data->src" : $data->value
);
}
return implode(PHP_EOL, $body);
default:
return $this->body;
}
} }
/** /**
* @return string * @inheritDoc
*/ */
public function __toString(): string protected function prepareHeaders(): array
{ {
if ($this->description) {
$output[] = '# ' . str_replace("\n", "\n# ", $this->description);
$output[] = '';
}
$output[] = "$this->verb $this->url $this->http"; $output[] = "$this->verb $this->url $this->http";
foreach ($this->headers as $header_key => $header) { foreach ($this->headers as $header_key => $header) {
$output[] = sprintf('%s%s: %s', $header['disabled'] ? '# ' : '', $header_key, $header['value']); $output[] = sprintf('%s%s: %s', $header['disabled'] ? '# ' : '', $header_key, $header['value']);
} }
$output[] = ''; return $output;
$output[] = (string)$this->prepareBody(); }
/**
* @inheritDoc
*/
protected function prepareBody(): array
{
switch ($this->bodymode) {
case 'formdata':
$output = [''];
foreach ($this->body as $data) {
$output[] = sprintf(
'%s%s=%s',
empty($data->disabled) ? '' : '# ',
$data->key,
$data->type === 'file' ? $data->src : $data->value
);
}
return $output;
default:
return ['', $this->body];
}
}
/**
* @inheritDoc
*/
public function __toString(): string
{
$output = array_merge(
$this->prepareDescription(),
$this->prepareHeaders(),
$this->prepareBody()
);
return implode(PHP_EOL, $output); return implode(PHP_EOL, $output);
} }
} }

View File

@ -11,7 +11,8 @@ use PmConverter\Exporters\{
class WgetConverter extends AbstractConverter implements ConverterContract class WgetConverter extends AbstractConverter implements ConverterContract
{ {
protected const FILE_EXT = 'sh'; protected const FILE_EXT = 'sh';
protected const OUTPUT_DIR = 'wget'; protected const OUTPUT_DIR = 'wget';
protected const REQUEST = WgetRequest::class; protected const REQUEST_CLASS = WgetRequest::class;
} }

View File

@ -7,57 +7,76 @@ namespace PmConverter\Exporters\Wget;
use PmConverter\Exporters\Abstract\AbstractRequest; use PmConverter\Exporters\Abstract\AbstractRequest;
/** /**
* * Class to determine file content with wget request format
*/ */
class WgetRequest extends AbstractRequest class WgetRequest extends AbstractRequest
{ {
/** /**
* @return string * @inheritDoc
*/ */
protected function prepareBody(): ?string protected function prepareDescription(): array
{ {
switch ($this->bodymode) { return empty($this->description)
case 'formdata': ? []
$lines = []; : ['# ' . str_replace("\n", "\n# ", $this->description), ''];
foreach ($this->body as &$data) {
if ($data->type === 'file') {
continue;
}
$lines[$data->key] = $data->value;
}
$body[] = http_build_query($lines);
return implode(PHP_EOL, $body);
default:
return $this->body;
}
} }
/** /**
* @return string * @inheritDoc
*/ */
public function __toString(): string protected function prepareHeaders(): array
{ {
$output[] = '#!/bin/sh'; $output = [];
if ($this->description) {
$output[] = '# ' . str_replace("\n", "\n# ", $this->description);
$output[] = '';
}
$output[] = 'wget \ ';
$output[] = "\t--no-check-certificate \ ";
$output[] = "\t--quiet \ ";
$output[] = "\t--timeout=0 \ ";
$output[] = "\t--method $this->verb \ ";
foreach ($this->headers as $header_key => $header) { foreach ($this->headers as $header_key => $header) {
if ($header['disabled']) { if ($header['disabled']) {
continue; continue;
} }
$output[] = sprintf("\t--header '%s=%s' \ ", $header_key, $header['value']); $output[] = sprintf("\t--header '%s=%s' \ ", $header_key, $header['value']);
} }
if (!is_null($body = $this->prepareBody())) { return $output;
$output[] = "\t--body-data '$body' \ ";
} }
/**
* @inheritDoc
*/
protected function prepareBody(): array
{
switch ($this->bodymode) {
case 'formdata':
$params = [];
foreach ($this->body as $data) {
if ($data->type === 'file') {
continue;
}
$params[$data->key] = $data->value;
}
$output[] = http_build_query($params);
return $output;
default:
return ["\t$this->body"];
}
}
/**
* @inheritDoc
*/
public function __toString(): string
{
$output = array_merge(
['#!/bin/sh'],
$this->prepareDescription(),
[
'wget \ ',
"\t--no-check-certificate \ ",
"\t--quiet \ ",
"\t--timeout=0 \ ",
"\t--method $this->verb \ ",
],
$this->prepareHeaders(),
$this->prepareBody()
);
$output[] = rtrim(array_pop($output), '\ '); $output[] = rtrim(array_pop($output), '\ ');
$output[] = "\t'$this->url'"; $output[] = "\t'$this->url'";
return implode(PHP_EOL, $output); return implode(PHP_EOL, array_merge($output, ['']));
} }
} }