|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Noitran\Opendox\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class TransformDocsCommand |
|
10
|
|
|
*/ |
|
11
|
|
|
class TransformDocsCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The console command name. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $name = 'opendox:transform'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The console command description. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $description = 'Transforms OpenAPI yaml file to json.'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Execute the console command. |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function handle(): void |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->info('Transforming yaml to json.'); |
|
35
|
1 |
|
$settings = config('opendox.documentation_source'); |
|
36
|
|
|
|
|
37
|
1 |
|
if ($settings['extension'] === 'json') { |
|
38
|
|
|
$this->info( |
|
39
|
|
|
'Documentation source file in opendox.php is set to "json". conversion will be skipped. Just copying...' |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$this->convert( |
|
44
|
1 |
|
$settings['path'], |
|
45
|
1 |
|
$settings['filename'], |
|
46
|
1 |
|
$settings['extension'] |
|
47
|
|
|
); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $pathTo |
|
52
|
|
|
* @param string $fileName |
|
53
|
|
|
* @param string $fileExtension |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
1 |
|
protected function convert(string $pathTo, string $fileName, string $fileExtension): void |
|
58
|
|
|
{ |
|
59
|
1 |
|
$contents = $this->transform($pathTo . $fileName . '.' . $fileExtension); |
|
60
|
1 |
|
$saveToPath = config('opendox.documentation_source.save_to'); |
|
61
|
|
|
|
|
62
|
1 |
|
if (! file_exists($saveToPath)) { |
|
63
|
|
|
mkdir($saveToPath); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
$fullSaveToPath = $saveToPath . '/' . $fileName . '.json'; |
|
67
|
|
|
|
|
68
|
1 |
|
file_put_contents($fullSaveToPath, $contents); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $pathToFile |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
1 |
|
protected function transform(string $pathToFile): string |
|
77
|
|
|
{ |
|
78
|
1 |
|
$contents = file_get_contents($pathToFile); |
|
79
|
1 |
|
$bag = Yaml::parse($contents); |
|
80
|
|
|
|
|
81
|
1 |
|
return json_encode($bag, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|