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