-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGenMarkdown.php
63 lines (51 loc) · 1.78 KB
/
GenMarkdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Swaggest\JsonCli;
use Swaggest\JsonCli\GenPhp\BuilderOptions;
use Swaggest\JsonSchema\Schema;
use Swaggest\PhpCodeBuilder\Markdown\TypeBuilder;
use Yaoi\Command;
class GenMarkdown extends Base
{
use BuilderOptions;
/**
* @param Command\Definition $definition
* @param \stdClass|static $options
*/
public static function setUpDefinition(Command\Definition $definition, $options)
{
$definition->description = 'Generate Markdown document from JSON schema';
Base::setupGenOptions($definition, $options);
}
public function performAction()
{
try {
$skipRoot = false;
$baseName = null;
$schema = $this->loadSchema($skipRoot, $baseName);
$jb = new TypeBuilder();
$jb->trimNamePrefix = $this->defPtr;
if (!$schema instanceof Schema) {
$this->response->error('failed to assert Schema type, ' . get_class($schema) . ' received');
throw new ExitCode('', 1);
}
$jb->getTypeString($schema);
$jb->sortTypes();
$out = $jb->tableOfContents();
foreach ($jb->types as $typeName => $doc) {
$out .= $doc;
}
if ($this->output) {
if (!file_exists(dirname($this->output))) {
$this->response->error('Destination directory does not exist, please create: ' . dirname($this->output));
throw new ExitCode('', 1);
}
file_put_contents($this->output, $out);
} else {
echo $out;
}
} catch (\Exception $e) {
$this->response->error($e->getMessage());
throw new ExitCode('', 1);
}
}
}