Refactorings
- important curl and wget improvements - initial test coverage - setters and getters in Request classes - namespace fixes - some additions in README - docblocks and code-style
This commit is contained in:
196
src/Converters/Abstract/AbstractConverter.php
Normal file
196
src/Converters/Abstract/AbstractConverter.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\Abstract;
|
||||
|
||||
use Exception;
|
||||
use PmConverter\Converters\{
|
||||
ConverterContract,
|
||||
RequestContract};
|
||||
use PmConverter\Environment;
|
||||
use PmConverter\Exceptions\InvalidHttpVersionException;
|
||||
use PmConverter\FileSystem;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class AbstractConverter implements ConverterContract
|
||||
{
|
||||
/**
|
||||
* @var object|null
|
||||
*/
|
||||
protected ?object $collection = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $outputPath;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $vars = [];
|
||||
|
||||
/**
|
||||
* @var Environment|null
|
||||
*/
|
||||
protected ?Environment $env = null;
|
||||
|
||||
/**
|
||||
* Sets an environment with vars
|
||||
*
|
||||
* @param Environment $env
|
||||
* @return $this
|
||||
*/
|
||||
public function withEnv(Environment $env): static
|
||||
{
|
||||
$this->env = $env;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts collection requests
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function convert(object $collection, string $outputPath): void
|
||||
{
|
||||
$outputPath = sprintf('%s%s%s', $outputPath, DIRECTORY_SEPARATOR, static::OUTPUT_DIR);
|
||||
$this->outputPath = FileSystem::makeDir($outputPath);
|
||||
$this->collection = $collection;
|
||||
$this->setVariables();
|
||||
foreach ($collection->item as $item) {
|
||||
$this->convertItem($item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares collection variables
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setVariables(): static
|
||||
{
|
||||
if (isset($this->collection?->variable)) {
|
||||
foreach ($this->collection->variable as $var) {
|
||||
$this->vars["{{{$var->key}}}"] = $var->value;
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns output path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOutputPath(): string
|
||||
{
|
||||
return $this->outputPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether item contains another items or not
|
||||
*
|
||||
* @param object $item
|
||||
* @return bool
|
||||
*/
|
||||
protected function isItemFolder(object $item): bool
|
||||
{
|
||||
return !empty($item->item) && empty($item->request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an item to request object and writes it into file
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function convertItem(mixed $item): void
|
||||
{
|
||||
if ($this->isItemFolder($item)) {
|
||||
static $dir_tree;
|
||||
foreach ($item->item as $subitem) {
|
||||
$dir_tree[] = $item->name;
|
||||
$path = implode(DIRECTORY_SEPARATOR, $dir_tree);
|
||||
if ($this->isItemFolder($subitem)) {
|
||||
$this->convertItem($subitem);
|
||||
} else {
|
||||
$this->writeRequest($this->initRequest($subitem), $path);
|
||||
}
|
||||
array_pop($dir_tree);
|
||||
}
|
||||
} else {
|
||||
$this->writeRequest($this->initRequest($item));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialiazes request object to be written in file
|
||||
*
|
||||
* @param object $item
|
||||
* @return RequestContract
|
||||
* @throws InvalidHttpVersionException
|
||||
*/
|
||||
protected function initRequest(object $item): RequestContract
|
||||
{
|
||||
$request_class = static::REQUEST_CLASS;
|
||||
|
||||
/** @var RequestContract $result */
|
||||
$result = new $request_class();
|
||||
$result->setName($item->name);
|
||||
$result->setHttpVersion(1.1); //TODO http version?
|
||||
$result->setDescription($item->request?->description ?? null);
|
||||
$result->setVerb($item->request->method);
|
||||
$result->setUrl($item->request->url->raw);
|
||||
$result->setHeaders($item->request->header);
|
||||
$result->setAuth($item->request?->auth ?? $this->collection?->auth ?? null);
|
||||
if ($item->request->method !== 'GET' && !empty($item->request->body)) {
|
||||
$result->setBody($item->request->body);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes converted request object to file
|
||||
*
|
||||
* @param RequestContract $request
|
||||
* @param string|null $subpath
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function writeRequest(RequestContract $request, string $subpath = null): bool
|
||||
{
|
||||
$filedir = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $subpath);
|
||||
$filedir = FileSystem::makeDir($filedir);
|
||||
$filepath = sprintf('%s%s%s.%s', $filedir, DIRECTORY_SEPARATOR, $request->getName(), static::FILE_EXT);
|
||||
$content = $this->interpolate((string)$request);
|
||||
return file_put_contents($filepath, $content) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces variables in request with values from collection or environment
|
||||
*
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
protected function interpolate(string $content): string
|
||||
{
|
||||
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) {
|
||||
foreach ($matches[0] as $key => $var) {
|
||||
if (str_contains($content, $var)) {
|
||||
$content = str_replace($var, $this->vars[$var] ?? $this->env[$var] ?? $var, $content);
|
||||
unset($matches[0][$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (!empty($matches[0])) {
|
||||
// fwrite(STDERR, sprintf(' No values found: %s%s', implode(', ', $matches[0]), PHP_EOL));
|
||||
// }
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
295
src/Converters/Abstract/AbstractRequest.php
Normal file
295
src/Converters/Abstract/AbstractRequest.php
Normal file
@@ -0,0 +1,295 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\Abstract;
|
||||
|
||||
use PmConverter\Converters\RequestContract;
|
||||
use PmConverter\Exceptions\{
|
||||
EmptyHttpVerbException,
|
||||
InvalidHttpVersionException};
|
||||
use PmConverter\HttpVersions;
|
||||
use Stringable;
|
||||
|
||||
/**
|
||||
* Class to determine file content with any request format
|
||||
*/
|
||||
abstract class AbstractRequest implements Stringable, RequestContract
|
||||
{
|
||||
/**
|
||||
* @var string HTTP verb (GET, POST, etc.)
|
||||
*/
|
||||
protected string $verb;
|
||||
|
||||
/**
|
||||
* @var string URL where to send a request
|
||||
*/
|
||||
protected string $url;
|
||||
|
||||
/**
|
||||
* @var float HTTP protocol version
|
||||
*/
|
||||
protected float $httpVersion = 1.1;
|
||||
|
||||
/**
|
||||
* @var string Request name
|
||||
*/
|
||||
protected string $name;
|
||||
|
||||
/**
|
||||
* @var string|null Request description
|
||||
*/
|
||||
protected ?string $description = null;
|
||||
|
||||
/**
|
||||
* @var array Request headers
|
||||
*/
|
||||
protected array $headers = [];
|
||||
|
||||
/**
|
||||
* @var mixed Request body
|
||||
*/
|
||||
protected mixed $body = null;
|
||||
|
||||
/**
|
||||
* @var string Request body type
|
||||
*/
|
||||
protected string $bodymode = 'raw';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setHttpVersion(float $version): static
|
||||
{
|
||||
if (!in_array($version, HttpVersions::values())) {
|
||||
throw new InvalidHttpVersionException(
|
||||
'Only these HTTP versions are supported: ' . implode(', ', HttpVersions::values())
|
||||
);
|
||||
}
|
||||
$this->httpVersion = $version;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getHttpVersion(): float
|
||||
{
|
||||
return $this->httpVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setName(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return str_replace(DIRECTORY_SEPARATOR, '_', $this->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setDescription(?string $description): static
|
||||
{
|
||||
$this->description = $description;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDescription(): ?string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setVerb(string $verb): static
|
||||
{
|
||||
$this->verb = $verb;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getVerb(): string
|
||||
{
|
||||
empty($this->verb) && throw new EmptyHttpVerbException('Request HTTP verb must be defined before conversion');
|
||||
return $this->verb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setUrl(string $url): static
|
||||
{
|
||||
$this->url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUrl(): string
|
||||
{
|
||||
return $this->url ?: '<empty url>';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setHeaders(?array $headers): static
|
||||
{
|
||||
foreach ($headers as $header) {
|
||||
$this->setHeader($header->key, $header->value, $header?->disabled ?? false);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setHeader(string $name, mixed $value, bool $disabled = false): static
|
||||
{
|
||||
$this->headers[$name] = [
|
||||
'value' => $value,
|
||||
'disabled' => $disabled,
|
||||
];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setAuth(?object $auth): static
|
||||
{
|
||||
if (!empty($auth)) {
|
||||
switch ($auth->type) {
|
||||
case 'bearer':
|
||||
$this->setHeader('Authorization', 'Bearer ' . $auth->{$auth->type}[0]->value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setBodymode(string $bodymode): static
|
||||
{
|
||||
$this->bodymode = $bodymode;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getBodymode(): string
|
||||
{
|
||||
return $this->bodymode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setBody(object $body): static
|
||||
{
|
||||
$this->setBodymode($body->mode);
|
||||
if ($body->mode === 'formdata') {
|
||||
$this->setHeader('Content-Type', 'multipart/form-data')
|
||||
->setFormdataBody($body);
|
||||
} elseif (!empty($body->options) && $body->options->{$this->bodymode}->language === 'json') {
|
||||
$this->setHeader('Content-Type', 'application/json')
|
||||
->setJsonBody($body);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets body content from multipart/formdata
|
||||
*
|
||||
* @param object $body
|
||||
* @return $this
|
||||
*/
|
||||
protected function setFormdataBody(object $body): static
|
||||
{
|
||||
foreach ($body->formdata as $field) {
|
||||
$this->body[$field->key] = [
|
||||
'value' => $field->type === 'file' ? $field->src : $field->value,
|
||||
'type' => $field->type,
|
||||
];
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets body content from application/json
|
||||
*
|
||||
* @param object $body
|
||||
* @return $this
|
||||
*/
|
||||
protected function setJsonBody(object $body): static
|
||||
{
|
||||
$this->body = $body->{$this->getBodymode()};
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getBody(): mixed
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array of description lines
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
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;
|
||||
}
|
||||
18
src/Converters/ConvertFormat.php
Normal file
18
src/Converters/ConvertFormat.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters;
|
||||
|
||||
|
||||
use PmConverter\Converters\{
|
||||
Curl\CurlConverter,
|
||||
Http\HttpConverter,
|
||||
Wget\WgetConverter};
|
||||
|
||||
enum ConvertFormat: string
|
||||
{
|
||||
case Http = HttpConverter::class;
|
||||
case Curl = CurlConverter::class;
|
||||
case Wget = WgetConverter::class;
|
||||
}
|
||||
11
src/Converters/ConverterContract.php
Normal file
11
src/Converters/ConverterContract.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters;
|
||||
|
||||
interface ConverterContract
|
||||
{
|
||||
public function convert(object $collection, string $outputPath): void;
|
||||
public function getOutputPath(): string;
|
||||
}
|
||||
18
src/Converters/Curl/CurlConverter.php
Normal file
18
src/Converters/Curl/CurlConverter.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\Curl;
|
||||
|
||||
use PmConverter\Converters\{
|
||||
Abstract\AbstractConverter,
|
||||
ConverterContract};
|
||||
|
||||
class CurlConverter extends AbstractConverter implements ConverterContract
|
||||
{
|
||||
protected const FILE_EXT = 'sh';
|
||||
|
||||
protected const OUTPUT_DIR = 'curl';
|
||||
|
||||
protected const REQUEST_CLASS = CurlRequest::class;
|
||||
}
|
||||
83
src/Converters/Curl/CurlRequest.php
Normal file
83
src/Converters/Curl/CurlRequest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\Curl;
|
||||
|
||||
use PmConverter\Converters\Abstract\AbstractRequest;
|
||||
|
||||
/**
|
||||
* Class to determine file content with curl request format
|
||||
*/
|
||||
class CurlRequest extends AbstractRequest
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareDescription(): array
|
||||
{
|
||||
return empty($this->description)
|
||||
? []
|
||||
: ['# ' . str_replace("\n", "\n# ", $this->description), ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareHeaders(): array
|
||||
{
|
||||
$output = [];
|
||||
foreach ($this->headers as $header_key => $header) {
|
||||
if ($header['disabled']) {
|
||||
continue;
|
||||
}
|
||||
$output[] = sprintf("\t--header '%s: %s' \ ", $header_key, $header['value']);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareBody(): array
|
||||
{
|
||||
$output = [];
|
||||
switch ($this->bodymode) {
|
||||
case 'formdata':
|
||||
foreach ($this->body as $key => $data) {
|
||||
$output[] = sprintf(
|
||||
"%s\t--form '%s=%s' \ ",
|
||||
isset($data['disabled']) ? '# ' : '',
|
||||
$key,
|
||||
$data['type'] === 'file' ? "@" . $data['value'] : $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, array_merge($output, ['']));
|
||||
}
|
||||
}
|
||||
18
src/Converters/Http/HttpConverter.php
Normal file
18
src/Converters/Http/HttpConverter.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\Http;
|
||||
|
||||
use PmConverter\Converters\{
|
||||
Abstract\AbstractConverter,
|
||||
ConverterContract};
|
||||
|
||||
class HttpConverter extends AbstractConverter implements ConverterContract
|
||||
{
|
||||
protected const FILE_EXT = 'http';
|
||||
|
||||
protected const OUTPUT_DIR = 'http';
|
||||
|
||||
protected const REQUEST_CLASS = HttpRequest::class;
|
||||
}
|
||||
74
src/Converters/Http/HttpRequest.php
Normal file
74
src/Converters/Http/HttpRequest.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\Http;
|
||||
|
||||
use PmConverter\Converters\Abstract\AbstractRequest;
|
||||
use PmConverter\Exceptions\{
|
||||
EmptyHttpVerbException};
|
||||
|
||||
/**
|
||||
* Class to determine file content with http request format
|
||||
*/
|
||||
class HttpRequest extends AbstractRequest
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareDescription(): array
|
||||
{
|
||||
return empty($this->description)
|
||||
? []
|
||||
: ['# ' . str_replace("\n", "\n# ", $this->description), ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws EmptyHttpVerbException
|
||||
*/
|
||||
protected function prepareHeaders(): array
|
||||
{
|
||||
$output[] = sprintf('%s %s HTTP/%s', $this->getVerb(), $this->getUrl(), $this->getHttpVersion());
|
||||
foreach ($this->headers as $name => $data) {
|
||||
$output[] = sprintf('%s%s: %s', $data['disabled'] ? '# ' : '', $name, $data['value']);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareBody(): array
|
||||
{
|
||||
switch ($this->getBodymode()) {
|
||||
case 'formdata':
|
||||
$output = [''];
|
||||
foreach ($this->body as $key => $data) {
|
||||
$output[] = sprintf(
|
||||
'%s%s=%s',
|
||||
empty($data['disabled']) ? '' : '# ',
|
||||
$key,
|
||||
$data['type'] === 'file' ? '@' . $data['value'] : $data['value']
|
||||
);
|
||||
}
|
||||
return $output;
|
||||
default:
|
||||
return ['', $this->body];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws EmptyHttpVerbException
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
$output = array_merge(
|
||||
$this->prepareDescription(),
|
||||
$this->prepareHeaders(),
|
||||
$this->prepareBody()
|
||||
);
|
||||
return implode(PHP_EOL, $output);
|
||||
}
|
||||
}
|
||||
153
src/Converters/RequestContract.php
Normal file
153
src/Converters/RequestContract.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters;
|
||||
|
||||
use PmConverter\Converters\Http\HttpRequest;
|
||||
use PmConverter\Exceptions\{
|
||||
EmptyHttpVerbException,
|
||||
InvalidHttpVersionException};
|
||||
|
||||
interface RequestContract
|
||||
{
|
||||
/**
|
||||
* Sets HTTP protocol version
|
||||
*
|
||||
* @param float $version
|
||||
* @return $this
|
||||
* @throws InvalidHttpVersionException
|
||||
*/
|
||||
public function setHttpVersion(float $version): static;
|
||||
|
||||
/**
|
||||
* Returns HTTP protocol version
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getHttpVersion(): float;
|
||||
|
||||
/**
|
||||
* Sets name from collection item to request object
|
||||
*
|
||||
* @param string $name
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public function setName(string $name): static;
|
||||
|
||||
/**
|
||||
* Returns name of request
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Sets description from collection item to request object
|
||||
*
|
||||
* @param string|null $description
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public function setDescription(?string $description): static;
|
||||
|
||||
/**
|
||||
* Returns description request
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDescription(): ?string;
|
||||
|
||||
/**
|
||||
* Sets HTTP verb from collection item to request object
|
||||
*
|
||||
* @param string $verb
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public function setVerb(string $verb): static;
|
||||
|
||||
/**
|
||||
* Returns HTTP verb of request
|
||||
*
|
||||
* @return string
|
||||
* @throws EmptyHttpVerbException
|
||||
*/
|
||||
public function getVerb(): string;
|
||||
|
||||
/**
|
||||
* Sets URL from collection item to request object
|
||||
*
|
||||
* @param string $url
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public function setUrl(string $url): static;
|
||||
|
||||
/**
|
||||
* Returns URL of request
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl(): string;
|
||||
|
||||
/**
|
||||
* Sets headers from collection item to request object
|
||||
*
|
||||
* @param object[]|null $headers
|
||||
* @return $this
|
||||
*/
|
||||
public function setHeaders(?array $headers): static;
|
||||
|
||||
/**
|
||||
* Sets one header to request object
|
||||
*
|
||||
* @param string $name Header's name
|
||||
* @param mixed $value Header's value
|
||||
* @param bool $disabled Pass true to skip (or comment out) this header
|
||||
* @return $this
|
||||
*/
|
||||
public function setHeader(string $name, mixed $value, bool $disabled = false): static;
|
||||
|
||||
/**
|
||||
* Returns array of prepared headers
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders(): array;
|
||||
|
||||
/**
|
||||
* Sets authorization headers
|
||||
*
|
||||
* @param object|null $auth
|
||||
* @return $this
|
||||
*/
|
||||
public function setAuth(object $auth): static;
|
||||
|
||||
/**
|
||||
* Sets body mode from collection item to request object
|
||||
*
|
||||
* @param string $bodymode
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public function setBodymode(string $bodymode): static;
|
||||
|
||||
/**
|
||||
* Returns body mode of request
|
||||
*
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public function getBodymode(): string;
|
||||
|
||||
/**
|
||||
* Sets body from collection item to request object
|
||||
*
|
||||
* @param object $body
|
||||
* @return $this
|
||||
*/
|
||||
public function setBody(object $body): static;
|
||||
|
||||
/**
|
||||
* Returns body content
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getBody(): mixed;
|
||||
}
|
||||
18
src/Converters/Wget/WgetConverter.php
Normal file
18
src/Converters/Wget/WgetConverter.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\Wget;
|
||||
|
||||
use PmConverter\Converters\{
|
||||
Abstract\AbstractConverter,
|
||||
ConverterContract};
|
||||
|
||||
class WgetConverter extends AbstractConverter implements ConverterContract
|
||||
{
|
||||
protected const FILE_EXT = 'sh';
|
||||
|
||||
protected const OUTPUT_DIR = 'wget';
|
||||
|
||||
protected const REQUEST_CLASS = WgetRequest::class;
|
||||
}
|
||||
94
src/Converters/Wget/WgetRequest.php
Normal file
94
src/Converters/Wget/WgetRequest.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\Wget;
|
||||
|
||||
use PmConverter\Converters\Abstract\AbstractRequest;
|
||||
use PmConverter\Exceptions\EmptyHttpVerbException;
|
||||
|
||||
/**
|
||||
* Class to determine file content with wget request format
|
||||
*/
|
||||
class WgetRequest extends AbstractRequest
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareDescription(): array
|
||||
{
|
||||
return empty($this->description)
|
||||
? []
|
||||
: ['# ' . str_replace("\n", "\n# ", $this->description), ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareHeaders(): array
|
||||
{
|
||||
$output = [];
|
||||
foreach ($this->headers as $header_key => $header) {
|
||||
if ($header['disabled']) {
|
||||
continue;
|
||||
}
|
||||
$output[] = sprintf("\t--header '%s: %s' \ ", $header_key, $header['value']);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareBody(): array
|
||||
{
|
||||
switch ($this->bodymode) {
|
||||
case 'formdata':
|
||||
$output = [];
|
||||
foreach ($this->body as $key => $data) {
|
||||
if ($data['type'] === 'file') {
|
||||
continue;
|
||||
}
|
||||
$output[$key] = $data['value'];
|
||||
}
|
||||
return $output;
|
||||
default:
|
||||
return [$this->body];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws EmptyHttpVerbException
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
$output = array_merge(
|
||||
['#!/bin/sh'],
|
||||
$this->prepareDescription(),
|
||||
[
|
||||
'wget \ ',
|
||||
"\t--no-check-certificate \ ",
|
||||
"\t--timeout 0 \ ",
|
||||
"\t--method $this->verb \ ",
|
||||
],
|
||||
$this->prepareHeaders(),
|
||||
);
|
||||
if ($this->getBodymode() === 'formdata') {
|
||||
if ($this->getBody()) {
|
||||
if ($this->getVerb() === 'GET') {
|
||||
$output[] = sprintf("\t%s?%s", $this->getUrl(), http_build_query($this->prepareBody()));
|
||||
} else {
|
||||
$output[] = sprintf("\t--body-data '%s' \ ", http_build_query($this->prepareBody()));
|
||||
$output[] = sprintf("\t%s", $this->getUrl());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($this->getVerb() !== 'GET') {
|
||||
$output[] = sprintf("\t--body-data '%s' \ ", implode("\n", $this->prepareBody()));
|
||||
$output[] = sprintf("\t%s", $this->getUrl());
|
||||
}
|
||||
}
|
||||
return implode(PHP_EOL, array_merge($output, ['']));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user