|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TgScraper\Commands; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use TgScraper\Constants\Versions; |
|
14
|
|
|
use TgScraper\Generator; |
|
15
|
|
|
use Throwable; |
|
16
|
|
|
|
|
17
|
|
|
class ExportSchemaCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
protected static $defaultName = 'app:export-schema'; |
|
21
|
|
|
|
|
22
|
|
|
protected function configure(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this |
|
25
|
|
|
->setDescription('Export schema as JSON or YAML.') |
|
26
|
|
|
->setHelp('This command allows you to create a schema for a specific version of the Telegram bot API.') |
|
27
|
|
|
->addArgument('destination', InputArgument::REQUIRED, 'Destination file') |
|
28
|
|
|
->addOption( |
|
29
|
|
|
'yaml', |
|
30
|
|
|
null, |
|
31
|
|
|
InputOption::VALUE_NONE, |
|
32
|
|
|
'Export schema as YAML instead of JSON (this option takes precedence over "--postman")' |
|
33
|
|
|
) |
|
34
|
|
|
->addOption('postman', null, InputOption::VALUE_NONE, 'Export schema as a Postman-compatible JSON') |
|
35
|
|
|
->addOption('options', 'o', InputOption::VALUE_REQUIRED, 'Encoder options', 0) |
|
36
|
|
|
->addOption('readable', 'r', InputOption::VALUE_NONE, '(JSON only) Generate a human-readable JSON') |
|
37
|
|
|
->addOption('inline', null, InputOption::VALUE_REQUIRED, '(YAML only) Inline level', 6) |
|
38
|
|
|
->addOption('indent', null, InputOption::VALUE_REQUIRED, '(YAML only) Indent level', 4) |
|
39
|
|
|
->addOption('layer', 'l', InputOption::VALUE_REQUIRED, 'Bot API version to use', 'latest'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
43
|
|
|
{ |
|
44
|
|
|
$logger = new ConsoleLogger($output); |
|
45
|
|
|
$url = Versions::getVersionFromText($input->getOption('layer')); |
|
46
|
|
|
$logger->info('Using URL: ' . $url); |
|
47
|
|
|
try { |
|
48
|
|
|
$output->writeln('Fetching data from URL...'); |
|
49
|
|
|
$generator = new Generator($logger, $url); |
|
50
|
|
|
} catch (Throwable) { |
|
51
|
|
|
return Command::FAILURE; |
|
52
|
|
|
} |
|
53
|
|
|
$output->writeln('Exporting schema from data...'); |
|
54
|
|
|
$options = $input->getOption('options'); |
|
55
|
|
|
$useYaml = $input->getOption('yaml'); |
|
56
|
|
|
if ($useYaml) { |
|
57
|
|
|
$data = $generator->toYaml($input->getOption('inline'), $input->getOption('indent'), $options); |
|
58
|
|
|
} |
|
59
|
|
|
$destination = $input->getArgument('destination'); |
|
60
|
|
|
if ($input->getOption('readable')) { |
|
61
|
|
|
$options |= JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; |
|
62
|
|
|
} |
|
63
|
|
|
$data ??= $input->getOption('postman') ? $generator->toPostman($options) : $generator->toJson($options); |
|
|
|
|
|
|
64
|
|
|
$output->writeln('Saving scheme to file...'); |
|
65
|
|
|
$result = file_put_contents($destination, $data); |
|
|
|
|
|
|
66
|
|
|
if (false === $result) { |
|
67
|
|
|
$logger->critical('Unable to save file to ' . $destination); |
|
|
|
|
|
|
68
|
|
|
return Command::FAILURE; |
|
69
|
|
|
} |
|
70
|
|
|
$output->writeln('Done!'); |
|
71
|
|
|
return Command::SUCCESS; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |