Some refactorings in content generation

This commit is contained in:
Anthony Axenov 2023-08-10 15:43:18 +08:00
parent 2081aa935a
commit 9947987c20
Signed by: anthony
GPG Key ID: EA9EC32FF7CCD4EC
8 changed files with 194 additions and 103 deletions

View File

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

View File

@ -8,14 +8,14 @@ use PmConverter\Exporters\Http\HttpRequest;
use PmConverter\Exporters\RequestContract;
/**
*
* Class to determine file content with any request format
*/
abstract class AbstractRequest implements RequestContract
{
/**
* @var string
*/
protected string $http = 'HTTP/1.1'; //TODO verb
protected string $http = 'HTTP/1.1'; //TODO proto
/**
* @var string
@ -53,6 +53,8 @@ abstract class AbstractRequest implements RequestContract
protected string $url;
/**
* Sets name from collection item to request object
*
* @param string $name
* @return HttpRequest
*/
@ -63,6 +65,8 @@ abstract class AbstractRequest implements RequestContract
}
/**
* Returns name of request
*
* @return 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
* @return HttpRequest
*/
@ -81,6 +87,8 @@ abstract class AbstractRequest implements RequestContract
}
/**
* Sets HTTP verb from collection item to request object
*
* @param string $verb
* @return HttpRequest
*/
@ -91,6 +99,8 @@ abstract class AbstractRequest implements RequestContract
}
/**
* Sets URL from collection item to request object
*
* @param string $url
* @return HttpRequest
*/
@ -101,6 +111,8 @@ abstract class AbstractRequest implements RequestContract
}
/**
* Sets headers from collection item to request object
*
* @param object[]|null $headers
* @return $this
*/
@ -116,6 +128,8 @@ abstract class AbstractRequest implements RequestContract
}
/**
* Sets body mode from collection item to request object
*
* @param string $bodymode
* @return HttpRequest
*/
@ -126,8 +140,10 @@ abstract class AbstractRequest implements RequestContract
}
/**
* @param string $body
* @return HttpRequest
* Sets body from collection item to request object
*
* @param object $body
* @return $this
*/
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
*/
abstract public function __toString(): string;

View File

@ -11,7 +11,8 @@ use PmConverter\Exporters\{
class CurlConverter extends AbstractConverter implements ConverterContract
{
protected const FILE_EXT = 'sh';
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;
/**
*
* Class to determine file content with curl request format
*/
class CurlRequest extends AbstractRequest
{
/**
* @return string
* @inheritDoc
*/
protected function prepareBody(): ?string
protected function prepareDescription(): array
{
switch ($this->bodymode) {
case 'formdata':
$body = [];
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 empty($this->description)
? []
: ['# ' . str_replace("\n", "\n# ", $this->description), ''];
}
/**
* @return string
* @inheritDoc
*/
public function __toString(): string
protected function prepareHeaders(): array
{
$output[] = '#!/bin/sh';
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 \ ";
$output = [];
foreach ($this->headers as $header_key => $header) {
if ($header['disabled']) {
continue;
}
$output[] = sprintf("\t--header '%s=%s' \ ", $header_key, $header['value']);
}
if (!is_null($body = $this->prepareBody())) {
$output[] = match ($this->bodymode) {
'formdata' => $body,
default => "\t--data '$body'",
};
return $output;
}
/**
* @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), '\ ');
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
{
protected const FILE_EXT = '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;
/**
*
* Class to determine file content with http request format
*/
class HttpRequest extends AbstractRequest
{
/**
* @return string
* @inheritDoc
*/
protected function prepareBody(): ?string
protected function prepareDescription(): array
{
switch ($this->bodymode) {
case 'formdata':
$body = [];
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 empty($this->description)
? []
: ['# ' . str_replace("\n", "\n# ", $this->description), ''];
}
/**
* @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";
foreach ($this->headers as $header_key => $header) {
$output[] = sprintf('%s%s: %s', $header['disabled'] ? '# ' : '', $header_key, $header['value']);
}
$output[] = '';
$output[] = (string)$this->prepareBody();
return $output;
}
/**
* @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);
}
}

View File

@ -11,7 +11,8 @@ use PmConverter\Exporters\{
class WgetConverter extends AbstractConverter implements ConverterContract
{
protected const FILE_EXT = 'sh';
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;
/**
*
* Class to determine file content with wget request format
*/
class WgetRequest extends AbstractRequest
{
/**
* @return string
* @inheritDoc
*/
protected function prepareBody(): ?string
protected function prepareDescription(): array
{
switch ($this->bodymode) {
case 'formdata':
$lines = [];
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 empty($this->description)
? []
: ['# ' . str_replace("\n", "\n# ", $this->description), ''];
}
/**
* @return string
* @inheritDoc
*/
public function __toString(): string
protected function prepareHeaders(): array
{
$output[] = '#!/bin/sh';
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 \ ";
$output = [];
foreach ($this->headers as $header_key => $header) {
if ($header['disabled']) {
continue;
}
$output[] = sprintf("\t--header '%s=%s' \ ", $header_key, $header['value']);
}
if (!is_null($body = $this->prepareBody())) {
$output[] = "\t--body-data '$body' \ ";
return $output;
}
/**
* @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[] = "\t'$this->url'";
return implode(PHP_EOL, $output);
return implode(PHP_EOL, array_merge($output, ['']));
}
}