Skip to content

Commit 94fa25c

Browse files
authored
Merge pull request #1 from SMillerDev/feature/curl-hint
Adding cURL command generation
2 parents 00b7dbb + 923d716 commit 94fa25c

File tree

8 files changed

+76
-33
lines changed

8 files changed

+76
-33
lines changed

.gitignore

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
1-
# Created by .ignore support plugin (hsz.mobi)
2-
### JetBrains template
3-
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
4-
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
5-
6-
# User-specific stuff:
7-
.idea/workspace.xml
8-
.idea/vcs.xml
9-
.idea/tasks.xml
10-
.idea/dictionaries
11-
.idea/jsLibraryMappings.xml
12-
13-
# Sensitive or high-churn files:
14-
.idea/dataSources.ids
15-
.idea/dataSources.xml
16-
.idea/dataSources.local.xml
17-
.idea/sqlDataSources.xml
18-
.idea/dynamic.xml
19-
.idea/uiDesigner.xml
20-
21-
## File-based project format:
22-
*.iws
1+
/.idea/
232

243
# IntelliJ
254
/out/

src/PHPDraft/HTML/default.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<?php
22
/**
3-
* This file contains the default.php
3+
* This file contains the default template
44
*
55
* @package PHPDraft\HTML
66
* @author Sean Molenaar<[email protected]>
77
*/
8+
89
/**
910
* @var \PHPDraft\Model\APIBlueprintElement[]
1011
*/
11-
1212
$base = $this->categories;
1313
?>
1414
<!DOCTYPE html>
1515
<html lang="en">
1616
<head>
1717
<title><?= $this->base_data['TITLE']; ?></title>
1818
<meta name="viewport" content="width=device-width, initial-scale=1">
19-
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
20-
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
19+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
20+
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
2121
<style>
2222
<?= file_get_contents(__DIR__ . '/index.css');?>
2323
</style>
@@ -128,12 +128,24 @@ class="panel panel-default <?= $transition->get_method(); ?>">
128128
<em><?= $transition->build_url(); ?></em>
129129
<?php endif; ?>
130130

131+
132+
131133
<h4 class="request">Request
132134
<a class="btn-xs pull-right" role="button" data-toggle="collapse"
133135
href="#request-coll-<?= $transition->get_href(); ?>" aria-expanded="false"
134136
aria-controls="collapseMenu">
135137
<span class="glyphicon glyphicon-plus"></span>
136138
</a>
139+
<a type="button"
140+
class="btn btn-xs pull-right"
141+
role="button"
142+
title="cURL command"
143+
tabindex="0"
144+
data-placement="left"
145+
data-toggle="popover"
146+
data-content="<?= $transition->get_curl_command($this->base_data['HOST']); ?>">
147+
<span class="glyphicon glyphicon-copy"></span>
148+
</a>
137149
</h4>
138150
<div class="collapse collapsed request-panel"
139151
id="request-coll-<?= $transition->get_href(); ?>">
@@ -153,7 +165,8 @@ class="value"><?= $value; ?></span>
153165
<?php if (!empty($transition->request->body)): ?>
154166
<h5>Body</h5>
155167
<?php foreach ($transition->request->body as $value): ?>
156-
<?= $value->print_request('application/x-www-form-urlencoded');?>
168+
<?php $type = (isset($transition->request->headers['Content-Type']))?$transition->request->headers['Content-Type']:NULL;?>
169+
<?= $value->print_request($type);?>
157170
<?= $value ?>
158171
<?php endforeach; ?>
159172
<?php endif; ?>
@@ -268,7 +281,8 @@ class="value"><?= $value; ?></span>
268281
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
269282
crossorigin="anonymous"></script>
270283
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
271-
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
284+
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
272285
crossorigin="anonymous"></script>
286+
<script><?= file_get_contents(__DIR__ . '/index.js');?></script>
273287
</body>
274288
</html>

src/PHPDraft/HTML/index.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,11 @@ body .col-md-10 h2:first-of-type {
9999
.row dl.dl-horizontal dd {
100100
margin-left: 100px;
101101
}
102+
103+
a .curl-icon {
104+
background-size: 20px 20px;
105+
height: 20px;
106+
width: 20px;
107+
display: inline-block;
108+
background: url('https://curl.haxx.se/logo/curl-symbol.png') left center;
109+
}

src/PHPDraft/HTML/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Created by smillernl on 23-8-16.
3+
*/
4+
$(function () {
5+
$('[data-toggle="popover"]').popover()
6+
})

src/PHPDraft/Model/HTTPRequest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,25 @@ private function parse_structure($objects)
7979
$this->body[] = $struct;
8080
}
8181
}
82+
83+
public function get_curl_command($base_url)
84+
{
85+
$options = [];
86+
87+
$type = (isset($this->headers['Content-Type']))?$this->headers['Content-Type']:NULL;
88+
89+
$options[] = '-X'.$this->method;
90+
foreach ($this->body as $body)
91+
{
92+
$options[] = '--data-binary "'.strip_tags($body->print_request($type)).'"';
93+
}
94+
foreach ($this->headers as $header=>$value)
95+
{
96+
$options[] = '-H "'.$header.': '.$value. '"';
97+
}
98+
$options[] = '-v';
99+
return htmlspecialchars('curl '.join(' ', $options). ' "'.$base_url.$this->parent->build_url().'"');
100+
}
101+
102+
82103
}

src/PHPDraft/Model/RequestBodyElement.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,25 @@ public function print_request($type = 'application/x-www-form-urlencoded')
6666
{
6767
if (is_array($this->value))
6868
{
69-
$return = '<code>';
69+
$return = '<code class="request-body">';
70+
$list = [];
7071
foreach ($this->value as $object)
7172
{
7273
if (get_class($object) === self::class)
7374
{
74-
$return .= $object->print_request($type);
75+
$list[] = $object->print_request($type);
7576
}
7677
}
7778

79+
switch ($type) {
80+
case 'application/x-www-form-urlencoded':
81+
$return .= join('&amp;', $list);
82+
break;
83+
default:
84+
$return .= join(PHP_EOL, $list);
85+
break;
86+
}
87+
7888
$return .= '</code>';
7989

8090
return $return;
@@ -85,13 +95,13 @@ public function print_request($type = 'application/x-www-form-urlencoded')
8595
switch ($type)
8696
{
8797
case 'application/x-www-form-urlencoded':
88-
return $this->key . "=<kbd>" . $value . '</kbd>';
98+
return $this->key . "=<span>" . $value . '</span>';
8999
break;
90100
default:
91101
$object = [];
92102
$object[$this->key] = $value;
93103

94-
return json_encode($object) . PHP_EOL;
104+
return json_encode($object);
95105
break;
96106
}
97107
}

src/PHPDraft/Model/Tests/DataStructureElementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file contains the APIBlueprintElementTest.php
44
*
5-
* @package php-drafter\SOMETHING
5+
* @package php-drafter\Model
66
* @author Sean Molenaar<[email protected]>
77
*/
88

src/PHPDraft/Model/Transition.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,9 @@ public function get_method()
144144
{
145145
return (isset($this->request->method)) ? $this->request->method : 'NONE';
146146
}
147+
148+
public function get_curl_command($base_url)
149+
{
150+
return $this->request->get_curl_command($base_url);
151+
}
147152
}

0 commit comments

Comments
 (0)