Skip to content

Commit

Permalink
feat: generate OpenAPI JSON from APIB
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Aug 30, 2024
1 parent c95725b commit 0298420
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
/build/out
/build/*.phar
/tests/statics/index.*
src/Michelf/*
src/.gitignore
vendor/**

Expand All @@ -24,3 +23,4 @@ atlassian-ide-plugin.xml

/coverage.xml
/event.json
!/src/PHPDraft/Out/
6 changes: 6 additions & 0 deletions phpdraft
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ try
->opt('help:h', 'This help text', false)
->opt('version:v', 'Print the version for PHPDraft.', false)
->opt('file:f', 'Specifies the file to parse.', false)
->opt('openapi:a', 'Output location for an OpenAPI file.', false)
->opt('yes:y', 'Always accept using the online mode.', false, 'bool')
->opt('online:o', 'Always use the online mode.', false, 'bool')
->opt('template:t', 'Specifies the template to use. (defaults to \'default\').', false)
Expand Down Expand Up @@ -90,6 +91,11 @@ try
$data = json_decode($json_string);
}

if (isset($args['openapi'])) {
$openapi = ParserFactory::getOpenAPI()->init($data);
$openapi->write($args['openapi']);
}

$html = ParserFactory::getJson()->init($data);
$name = 'PHPD_SORT_' . strtoupper($args->getOpt('sort', ''));
$html->sorting = Sorting::${$name} ?? Sorting::PHPD_SORT_NONE->value;
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class HTTPRequest implements Comparable
*
* @var string
*/
public string $description;
public string $description = '';

/**
* Parent class.
Expand Down
4 changes: 2 additions & 2 deletions src/PHPDraft/Model/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Transition extends HierarchyElement
/**
* HTTP method used.
*
* @var string
* @var string|null
*/
public string $method;
public ?string $method = NULL;

/**
* URI.
Expand Down
64 changes: 42 additions & 22 deletions src/PHPDraft/Out/BaseTemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,21 @@ abstract class BaseTemplateRenderer
* @var int
*/
public int $sorting;

/**
* CSS Files to load.
*
* @var string[]
*/
public array $css = [];
/**
* JS Files to load.
*
* @var string[]
*/
public array $js = [];
/**
* The image to use as a logo.
*
* @var string|null
*/
protected ?string $image = null;
/**
* The template file to load.
* JSON representation of an API Blueprint.
*
* @var string
* @var object
*/
protected string $template;
protected object $object;

/**
* The base data of the API.
*
* @var array<string, mixed>
*/
protected array $base_data;
protected array $base_data = [];

/**
* JSON object of the API blueprint.
*
Expand All @@ -66,4 +51,39 @@ abstract class BaseTemplateRenderer
* @var ObjectStructureElement[]
*/
protected array $base_structures = [];

/**
* Parse base data
*
* @param object $object
*/
protected function parse_base_data(object $object): void
{
//Prepare base data
if (!is_array($object->content[0]->content)) {
return;
}

$this->base_data['TITLE'] = $object->content[0]->meta->title->content ?? '';

foreach ($object->content[0]->attributes->metadata->content as $meta) {
$this->base_data[$meta->content->key->content] = $meta->content->value->content;
}

foreach ($object->content[0]->content as $value) {
if ($value->element === 'copy') {
$this->base_data['DESC'] = $value->content;
continue;
}

$cat = new Category();
$cat = $cat->parse($value);

if (($value->meta->classes->content[0]->content ?? null) === 'dataStructures') {
$this->base_structures = array_merge($this->base_structures, $cat->structures);
} else {
$this->categories[] = $cat;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,36 @@
use Twig\TwigFilter;
use Twig\TwigTest;

class TemplateRenderer extends BaseTemplateRenderer
class HtmlTemplateRenderer extends BaseTemplateRenderer
{


/**
* CSS Files to load.
*
* @var string[]
*/
public array $css = [];

/**
* JS Files to load.
*
* @var string[]
*/
public array $js = [];
/**
* The image to use as a logo.
*
* @var string|null
*/
protected ?string $image = null;
/**
* The template file to load.
*
* @var string
*/
protected string $template;

/**
* TemplateGenerator constructor.
*
Expand Down Expand Up @@ -106,41 +134,6 @@ public function get(object $object): string
]);
}

/**
* Parse base data
*
* @param object $object
*/
private function parse_base_data(object $object): void
{
//Prepare base data
if (!is_array($object->content[0]->content)) {
return;
}

$this->base_data['TITLE'] = $object->content[0]->meta->title->content ?? '';

foreach ($object->content[0]->attributes->metadata->content as $meta) {
$this->base_data[$meta->content->key->content] = $meta->content->value->content;
}

foreach ($object->content[0]->content as $value) {
if ($value->element === 'copy') {
$this->base_data['DESC'] = $value->content;
continue;
}

$cat = new Category();
$cat = $cat->parse($value);

if (($value->meta->classes->content[0]->content ?? null) === 'dataStructures') {
$this->base_structures = array_merge($this->base_structures, $cat->structures);
} else {
$this->categories[] = $cat;
}
}
}

/**
* Get the path to a file to include.
*
Expand Down
Loading

0 comments on commit 0298420

Please sign in to comment.