Skip to content

Commit cfccd75

Browse files
authored
Merge pull request #2 from SMillerDev/fix/cleanup
Fix/cleanup
2 parents 94fa25c + fbfd940 commit cfccd75

24 files changed

+232
-61
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
# JIRA plugin
88
atlassian-ide-plugin.xml
99

10+
config.json

README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
# PHP-Drafter
1+
# PHPDraft
22
This is a parser for API Blueprint files in PHP.
33

44
## Usage
55
For direct usage you can run:
66
```bash
7-
$ ./php-drafter.phar blueprint-file.apib > blueprint-webpage.html
7+
$ ./phpdraft.phar blueprint-file.apib > blueprint-webpage.html
88
```
99
You can also install it first:
1010
```bash
11-
$ cp php-drafter.phar /usr/bin/php-drafter
12-
$ chmod +x /usr/bin/php-drafter
13-
$ php-drafter blueprint-file.apib > blueprint-webpage.html
11+
$ cp phpdraft.phar /usr/bin/phpdraft
12+
$ chmod +x /usr/bin/phpdraft
13+
$ phpdraft blueprint-file.apib > blueprint-webpage.html
1414
```
1515

16+
## Including Files
17+
It is possible to include other files in your blueprint by using a special include directive with a path to the included file relative to the current file's directory. Included files can be written in API Blueprint, Markdown or HTML (or JSON for response examples). Included files can include other files, so be careful of circular references.
18+
19+
```markdown
20+
<!-- include(filename.md) -->
21+
```
22+
23+
For tools that do not support this include directive it will just render out as an HTML comment. API Blueprint may support its own mechanism of including files in the future, and this syntax was chosen to not interfere with the [external documents proposal](https://github.com/apiaryio/api-blueprint/issues/20) while allowing `PHPDraft` users to include documents today.
24+
25+
_Thanks to [aglio](https://github.com/danielgtaylor/aglio) for the idea._
26+
1627
## Writing API documentation
1728

1829
For writing API documentation using [API Blueprint](http://apiblueprint.org/) syntax. You can read about its [specification](https://github.com/apiaryio/api-blueprint/blob/master/API%20Blueprint%20Specification.md).
@@ -65,7 +76,7 @@ Return the information for the Person
6576

6677

6778
## Dependencies
68-
PHP-Drafter requires [drafter](https://github.com/apiaryio/drafter) to be installed. Refer to the drafter page for the installation details.
79+
PHPDraft requires [drafter](https://github.com/apiaryio/drafter) to be installed. Refer to the drafter page for the installation details.
6980

7081
## Libraries
7182
This app usage the following libraries:

config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"tmpdir": "/tmp/drafter"
2+
"tmpdir": "/tmp/drafter",
3+
"logo": "/home/smillernl/Documents/5acf473a9e6a3deb8e98a2b6373d0a83.png"
34
}

config.json.sample

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"tmpdir": "/tmp/drafter",
3+
"logo": "/path/to/some/logo.png"
4+
}

index.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
<?php
2+
/**
3+
* Set up include path for source handling
4+
*/
25
set_include_path(get_include_path().":".__DIR__.'/src/');
36
$config = json_decode(file_get_contents(__DIR__."/config.json"));
7+
8+
/**
9+
* Set up required classes (with the autoloader)
10+
*/
411
require_once 'PHPDraft/Core/Autoloader.php';
512
use PHPDraft\In\ApibFileParser;
613
use PHPDraft\Parse\ApibToJson;
714
use PHPDraft\Parse\JsonToHTML;
15+
16+
if($argc < 1)
17+
{
18+
file_put_contents('php://stderr', "Missing file to parse\n");
19+
exit(2);
20+
}
21+
822
$apib = new ApibFileParser($argv[1]);
923
$json = new ApibToJson($apib);
1024
$json->parseToJson();
1125
$html = new JsonToHTML($json);
12-
echo $html->get_html();
26+
$html->get_html();
1327
?>

src/PHPDraft/In/ApibFileParser.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@
1010

1111
class ApibFileParser
1212
{
13-
protected $out_string;
13+
/**
14+
* Complete API Blueprint
15+
* @var string
16+
*/
17+
protected $full_apib;
1418

19+
/**
20+
* Location of the API Blueprint to parse
21+
* @var string
22+
*/
1523
protected $location;
1624

1725
/**
@@ -21,8 +29,8 @@ class ApibFileParser
2129
*/
2230
public function __construct($filename = 'index.apib')
2331
{
24-
$this->location = pathinfo($filename, PATHINFO_DIRNAME) . '/';
25-
$this->out_string = $this->get_apib($filename);
32+
$this->location = pathinfo($filename, PATHINFO_DIRNAME) . '/';
33+
$this->full_apib = $this->get_apib($filename);
2634
}
2735

2836
/**
@@ -52,7 +60,7 @@ function get_apib($filename)
5260
*/
5361
function __toString()
5462
{
55-
return $this->out_string;
63+
return $this->full_apib;
5664
}
5765

5866
}

src/PHPDraft/Model/APIBlueprintElement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ abstract class APIBlueprintElement
4141
protected $parent = NULL;
4242

4343
/**
44-
* Parse an object to an element
44+
* Parse a JSON object to an element
4545
*
4646
* @param \stdClass $object an object to parse
4747
*

src/PHPDraft/Model/Category.php

+4-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file contains the Category.php
44
*
5-
* @package php-drafter\SOMETHING
5+
* @package PHPDraft\Model
66
* @author Sean Molenaar<[email protected]>
77
*/
88

@@ -17,25 +17,11 @@ class Category extends APIBlueprintElement
1717
public $structures = [];
1818

1919
/**
20-
* Add a struct dependency
20+
* Fill class values based on JSON object
2121
*
22-
* @param string $object Name of the struct to add
22+
* @param \stdClass $object JSON object
2323
*
24-
* @internal param string $name Name of the type
25-
*/
26-
public function add_struct($object)
27-
{
28-
echo "<pre>";
29-
var_dump($object);
30-
echo "</pre>";
31-
}
32-
33-
/**
34-
* Parse the category
35-
*
36-
* @param \stdClass $object
37-
*
38-
* @return $this
24+
* @return $this self-reference
3925
*/
4026
function parse($object)
4127
{

src/PHPDraft/Model/DataStructureElement.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file contains the DataStructureElement.php
44
*
5-
* @package php-drafter\SOMETHING
5+
* @package PHPDraft\Model
66
* @author Sean Molenaar<[email protected]>
77
*/
88

@@ -15,26 +15,31 @@ class DataStructureElement
1515
* @var string
1616
*/
1717
public $key;
18+
1819
/**
1920
* Object JSON type
2021
* @var string
2122
*/
2223
public $type;
24+
2325
/**
2426
* Object description
2527
* @var string
2628
*/
2729
public $description;
30+
2831
/**
2932
* Type of element
3033
* @var string
3134
*/
3235
public $element = NULL;
36+
3337
/**
3438
* Object value
3539
* @var mixed|DataStructureElement[]
3640
*/
3741
public $value = NULL;
42+
3843
/**
3944
* Object status (required|optional)
4045
* @var string
@@ -48,7 +53,7 @@ class DataStructureElement
4853
public $deps;
4954

5055
/**
51-
* Unreported datatypes
56+
* Default datatypes
5257
* @var array
5358
*/
5459
protected $defaults = ['boolean', 'string', 'number', 'object', 'array'];

src/PHPDraft/Model/RequestBodyElement.php renamed to src/PHPDraft/Model/Elements/RequestBodyElement.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* @author Sean Molenaar<[email protected]>
77
*/
88

9-
namespace PHPDraft\Model;
9+
namespace PHPDraft\Model\Elements;
1010

11+
use PHPDraft\Model\DataStructureElement;
1112

1213
class RequestBodyElement extends DataStructureElement
1314
{
@@ -62,6 +63,13 @@ public function parse($object, &$dependencies)
6263
return $this;
6364
}
6465

66+
/**
67+
* Print the request body as a string
68+
*
69+
* @param string $type The type of request
70+
*
71+
* @return string Request body
72+
*/
6573
public function print_request($type = 'application/x-www-form-urlencoded')
6674
{
6775
if (is_array($this->value))

src/PHPDraft/Model/HTTPRequest.php

+28-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace PHPDraft\Model;
1010

11+
use PHPDraft\Model\Elements\RequestBodyElement;
12+
1113
class HTTPRequest
1214
{
1315
/**
@@ -32,14 +34,25 @@ class HTTPRequest
3234
* Body of the request (if POST or PUT)
3335
* @var RequestBodyElement[]
3436
*/
35-
public $body;
37+
public $body = [];
3638

39+
/**
40+
* HTTPRequest constructor.
41+
*
42+
* @param Transition $parent Parent entity
43+
*/
3744
public function __construct(&$parent)
3845
{
3946
$this->parent = &$parent;
40-
//TODO: Parse body
4147
}
4248

49+
/**
50+
* Fill class values based on JSON object
51+
*
52+
* @param \stdClass $object JSON object
53+
*
54+
* @return $this self-reference
55+
*/
4356
public function parse($object)
4457
{
4558
$this->method = $object->attributes->method;
@@ -67,6 +80,11 @@ public function parse($object)
6780
return $this;
6881
}
6982

83+
/**
84+
* Parse the objects into a request body
85+
*
86+
* @param \stdClass[] $objects JSON objects
87+
*/
7088
private function parse_structure($objects)
7189
{
7290
foreach ($objects as $object)
@@ -80,6 +98,13 @@ private function parse_structure($objects)
8098
}
8199
}
82100

101+
/**
102+
* Generate a cURL command for the HTTP request
103+
*
104+
* @param string $base_url URL to the base server
105+
*
106+
* @return string An executable cURL command
107+
*/
83108
public function get_curl_command($base_url)
84109
{
85110
$options = [];
@@ -96,7 +121,7 @@ public function get_curl_command($base_url)
96121
$options[] = '-H "'.$header.': '.$value. '"';
97122
}
98123
$options[] = '-v';
99-
return htmlspecialchars('curl '.join(' ', $options). ' "'.$base_url.$this->parent->build_url().'"');
124+
return htmlspecialchars('curl '.join(' ', $options). ' "'.$this->parent->build_url($base_url).'"');
100125
}
101126

102127

src/PHPDraft/Model/HTTPResponse.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class HTTPResponse
1212
{
1313
/**
14-
* Description
14+
* HTTP Status code
1515
* @var int
1616
*/
1717
public $statuscode;
@@ -45,6 +45,13 @@ public function __construct($parent)
4545
$this->parent = &$parent;
4646
}
4747

48+
/**
49+
* Fill class values based on JSON object
50+
*
51+
* @param \stdClass $object JSON object
52+
*
53+
* @return $this self-reference
54+
*/
4855
public function parse($object)
4956
{
5057
$this->statuscode = intval($object->attributes->statusCode);

src/PHPDraft/Model/Resource.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file contains the Resource.php
44
*
5-
* @package php-drafter\SOMETHING
5+
* @package PHPDraft\Model
66
* @author Sean Molenaar<[email protected]>
77
*/
88

@@ -27,6 +27,13 @@ public function __construct(&$parent)
2727
$this->parent = $parent;
2828
}
2929

30+
/**
31+
* Fill class values based on JSON object
32+
*
33+
* @param \stdClass $object JSON object
34+
*
35+
* @return $this self-reference
36+
*/
3037
function parse($object)
3138
{
3239
parent::parse($object);
@@ -37,7 +44,7 @@ function parse($object)
3744
{
3845
if ($item->element === 'copy') continue;
3946
$transition = new Transition($this);
40-
array_push($this->children, $transition->parse($item));
47+
$this->children[] = $transition->parse($item);
4148
}
4249

4350
return $this;

0 commit comments

Comments
 (0)