From 6a14200442d4af0b033d6e90f6032057acad4d0a Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Fri, 28 Oct 2016 18:20:00 +0200 Subject: [PATCH] Enable multiple requests and responses per transition --- index.php | 12 +- src/PHPDraft/Model/Comparable.php | 22 ++++ src/PHPDraft/Model/HTTPRequest.php | 13 +- src/PHPDraft/Model/HTTPResponse.php | 14 +- src/PHPDraft/Model/Transition.php | 62 ++++++--- .../Out/HTML/{default.php => default.phtml} | 123 +++++++++--------- src/PHPDraft/Out/TemplateGenerator.php | 10 +- 7 files changed, 170 insertions(+), 86 deletions(-) create mode 100644 src/PHPDraft/Model/Comparable.php rename src/PHPDraft/Out/HTML/{default.php => default.phtml} (75%) diff --git a/index.php b/index.php index 7e5c1f01..9d40049c 100755 --- a/index.php +++ b/index.php @@ -16,20 +16,22 @@ define('VERSION', '0'); $values = UI::main($argv); -$apib = new ApibFileParser($values['file']); -$json = new Drafter($apib); -$html = new JsonToHTML($json->parseToJson()); +$apib = new ApibFileParser($values['file']); +$json = new Drafter($apib); +$html = new JsonToHTML($json->parseToJson()); $html->sorting = $values['sorting']; $html->get_html($values['template'], $values['image'], $values['css'], $values['js']); -function phpdraft_var_dump($var) +function phpdraft_var_dump(...$vars) { if (defined('__PHPDRAFT_PHAR__')) { return; } echo '
';
-    var_dump($var);
+    foreach ($vars as $var) {
+        var_dump($var);
+    }
     echo '
'; } diff --git a/src/PHPDraft/Model/Comparable.php b/src/PHPDraft/Model/Comparable.php new file mode 100644 index 00000000..361427df --- /dev/null +++ b/src/PHPDraft/Model/Comparable.php @@ -0,0 +1,22 @@ +method === $b->method) && ($this->body === $b->body) && ($this->headers === $b->headers)); + } } \ No newline at end of file diff --git a/src/PHPDraft/Model/HTTPResponse.php b/src/PHPDraft/Model/HTTPResponse.php index ca99486b..47a03614 100644 --- a/src/PHPDraft/Model/HTTPResponse.php +++ b/src/PHPDraft/Model/HTTPResponse.php @@ -10,7 +10,7 @@ use PHPDraft\Model\Elements\DataStructureElement; -class HTTPResponse +class HTTPResponse implements Comparable { /** * HTTP Status code @@ -127,4 +127,16 @@ protected function parse_structure($objects) $this->structure[] = $struct; } } + + /** + * Check if item is the same as other item + * + * @param self $b Object to compare to + * + * @return bool + */ + public function is_equal_to($b) + { + return (($this->statuscode === $b->statuscode) && ($this->headers === $b->headers) && ($this->content === $b->content)); + } } \ No newline at end of file diff --git a/src/PHPDraft/Model/Transition.php b/src/PHPDraft/Model/Transition.php index ad891870..92ff5f20 100644 --- a/src/PHPDraft/Model/Transition.php +++ b/src/PHPDraft/Model/Transition.php @@ -43,16 +43,16 @@ class Transition extends HierarchyElement /** * The request * - * @var HTTPRequest + * @var HTTPRequest[] */ - public $request; + public $requests = []; /** * The responses * * @var HTTPResponse[] */ - public $responses; + public $responses = []; /** * Structures used (if any) @@ -97,14 +97,38 @@ function parse($object) $this->data_variables = $struct->parse($object->attributes->data, $deps); } - if (isset($object->content[0]->content)) { - foreach ($object->content[0]->content as $item) { - if ($item->element === 'httpRequest') { - $this->request = new HTTPRequest($this); - $this->request->parse($item); - } elseif ($item->element === 'httpResponse') { - $response = new HTTPResponse($this); - $this->responses[] = $response->parse($item); + if (!is_array($object->content)) { + return $this; + } + foreach ($object->content as $transition_item) { + if (!isset($transition_item->content)) { + continue; + } + foreach ($transition_item->content as $item) { + $value = null; + switch ($item->element) { + case 'httpRequest': + $value = new HTTPRequest($this); + $list = &$this->requests; + break; + case 'httpResponse': + $value = new HTTPResponse($this); + $list = &$this->responses; + break; + default: + continue; + } + $value->parse($item); + + if (empty($list)) { + $list[] = $value; + continue; + } + foreach ($list as $existing_value) { + phpdraft_var_dump($value->is_equal_to($existing_value)); + if (!$value->is_equal_to($existing_value)) { + $list[] = $value; + } } } } @@ -203,11 +227,13 @@ private function find_overlap($str1, $str2) /** * Get the HTTP method of the child request * + * @param int $request Request to get the method for + * * @return string HTTP Method */ - public function get_method() + public function get_method($request = 0) { - return (isset($this->request->method)) ? $this->request->method : 'NONE'; + return (isset($this->requests[$request]->method)) ? $this->requests[$request]->method : 'NONE'; } /** @@ -217,11 +243,17 @@ public function get_method() * * @param array $additional additional arguments to pass * + * @param int $key number of the request to generate for + * * @return string A cURL CLI command */ - public function get_curl_command($base_url, $additional = []) + public function get_curl_command($base_url, $additional = [], $key = 0) { - return $this->request->get_curl_command($base_url, $additional); + if (!isset($this->requests[$key])) { + return ''; + } + + return $this->requests[$key]->get_curl_command($base_url, $additional); } } \ No newline at end of file diff --git a/src/PHPDraft/Out/HTML/default.php b/src/PHPDraft/Out/HTML/default.phtml similarity index 75% rename from src/PHPDraft/Out/HTML/default.php rename to src/PHPDraft/Out/HTML/default.phtml index 22c8a3a4..c8738323 100644 --- a/src/PHPDraft/Out/HTML/default.php +++ b/src/PHPDraft/Out/HTML/default.phtml @@ -151,68 +151,73 @@ class="btn btn-default curl"

description; ?>

-
-
-

Request - -

-
- request)): ?> -
- url_variables !== []): ?> -
Example URI
- base_data['HOST']; ?> - build_url(); ?> - - request->headers !== []): ?> -
Headers
-
    - request->headers as $name => $value): ?> -
  • - : - -
  • - -
- - request->body)): ?> -
Body
- request->body as $value): ?> - - - - request->headers['Content-Type'])) ? $transition->request->headers['Content-Type'] : null; ?> - print_request($type); ?> - - - - - request->struct)): ?> -
Structure
-
- request->struct ?> + requests)): ?> + requests as $request): ?> +
+
+

Request + +

- - - url_variables !== null): ?> -
URI Parameters
-
- url_variables; ?> -
- +
+ url_variables !== []): ?> +
Example URI
+ base_data['HOST']; ?> + build_url(); ?> + + headers !== []): ?> +
Headers
+
    + headers as $name => $value): ?> +
  • + : + +
  • + +
+ + body)): ?> +
Body
+ body as $value): ?> + + + + headers['Content-Type'])) ? $request->headers['Content-Type'] : null; ?> + print_request($type); ?> + + + + + struct)): ?> - data_variables !== null): ?> -
Data object
- data_variables ?> - -
-
+
Structure
+
+ struct ?> +
+ + + url_variables !== null): ?> +
URI Parameters
+
+ url_variables; ?> +
+ + + data_variables !== null): ?> +
Data object
+ data_variables ?> + +
+
+ + responses)): ?> responses as $response): ?>
diff --git a/src/PHPDraft/Out/TemplateGenerator.php b/src/PHPDraft/Out/TemplateGenerator.php index 7ff01511..e196310c 100644 --- a/src/PHPDraft/Out/TemplateGenerator.php +++ b/src/PHPDraft/Out/TemplateGenerator.php @@ -85,16 +85,16 @@ public function __construct($template, $image) public function get($object) { $include = null; - if (stream_resolve_include_path($this->template . DIRECTORY_SEPARATOR . $this->template . '.php')) { - $include = $this->template . DIRECTORY_SEPARATOR . $this->template . '.php'; + if (stream_resolve_include_path($this->template . DIRECTORY_SEPARATOR . $this->template . '.phtml')) { + $include = $this->template . DIRECTORY_SEPARATOR . $this->template . '.phtml'; } - if (stream_resolve_include_path($this->template . '.php')) { + if (stream_resolve_include_path($this->template . '.phtml')) { $include = $this->template . '.php'; } - if (stream_resolve_include_path('PHPDraft/Out/HTML/' . $this->template . '.php')) { - $include = 'PHPDraft/Out/HTML/' . $this->template . '.php'; + if (stream_resolve_include_path('PHPDraft/Out/HTML/' . $this->template . '.phtml')) { + $include = 'PHPDraft/Out/HTML/' . $this->template . '.phtml'; } if ($include === null) { file_put_contents('php://stderr', "Couldn't find template '$this->template'\n");