1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TgScraper\Commands; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use TgScraper\Constants\Versions; |
15
|
|
|
use TgScraper\Generator; |
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
class CreateStubsCommand extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
protected static $defaultName = 'app:create-stubs'; |
22
|
|
|
|
23
|
|
|
protected function validateData(string|false $data): bool |
24
|
|
|
{ |
25
|
|
|
return false !== $data and !empty($data); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function configure(): void |
29
|
|
|
{ |
30
|
|
|
$this |
31
|
|
|
->setDescription('Create stubs from bot API schema.') |
32
|
|
|
->setHelp('This command allows you to create class stubs for all types of the Telegram bot API.') |
33
|
|
|
->addArgument('destination', InputArgument::REQUIRED, 'Destination directory') |
34
|
|
|
->addOption('namespace-prefix', null, InputOption::VALUE_REQUIRED, 'Namespace prefix for stubs', 'TelegramApi') |
35
|
|
|
->addOption( |
36
|
|
|
'json', |
37
|
|
|
null, |
38
|
|
|
InputOption::VALUE_REQUIRED, |
39
|
|
|
'Path to JSON file to use instead of fetching from URL (this option takes precedence over "--layer")' |
40
|
|
|
) |
41
|
|
|
->addOption( |
42
|
|
|
'yaml', |
43
|
|
|
null, |
44
|
|
|
InputOption::VALUE_REQUIRED, |
45
|
|
|
'Path to YAML file to use instead of fetching from URL (this option takes precedence over "--layer" and "--json")' |
46
|
|
|
) |
47
|
|
|
->addOption('layer', 'l', InputOption::VALUE_REQUIRED, 'Bot API version to use', 'latest'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
51
|
|
|
{ |
52
|
|
|
$logger = new ConsoleLogger($output); |
53
|
|
|
$url = Versions::getVersionFromText($input->getOption('layer')); |
54
|
|
|
$yamlPath = $input->getOption('yaml'); |
55
|
|
|
if (empty($yamlPath)) { |
56
|
|
|
$jsonPath = $input->getOption('json'); |
57
|
|
|
if (empty($jsonPath)) { |
58
|
|
|
$logger->info('Using URL: ' . $url); |
59
|
|
|
try { |
60
|
|
|
$output->writeln('Fetching data from URL...'); |
61
|
|
|
$generator = new Generator($logger, $url); |
62
|
|
|
} catch (Throwable) { |
63
|
|
|
return Command::FAILURE; |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
$data = file_get_contents($jsonPath); |
67
|
|
|
if (!$this->validateData($data)) { |
68
|
|
|
$logger->critical('Invalid JSON file provided'); |
69
|
|
|
return Command::INVALID; |
70
|
|
|
} |
71
|
|
|
$logger->info('Using JSON schema: ' . $jsonPath); |
|
|
|
|
72
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
73
|
|
|
$generator = Generator::fromJson($logger, $data); |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
$data = file_get_contents($yamlPath); |
77
|
|
|
if (!$this->validateData($data)) { |
78
|
|
|
$logger->critical('Invalid YAML file provided'); |
79
|
|
|
return Command::INVALID; |
80
|
|
|
} |
81
|
|
|
$logger->info('Using YAML schema: ' . $yamlPath); |
|
|
|
|
82
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
83
|
|
|
$generator = Generator::fromYaml($logger, $data); |
84
|
|
|
} |
85
|
|
|
try { |
86
|
|
|
$output->writeln('Creating stubs...'); |
87
|
|
|
$generator->toStubs($input->getArgument('destination'), $input->getOption('namespace-prefix')); |
|
|
|
|
88
|
|
|
} catch (Exception) { |
89
|
|
|
$logger->critical('Could not create stubs.'); |
90
|
|
|
return Command::FAILURE; |
91
|
|
|
} |
92
|
|
|
$output->writeln('Done!'); |
93
|
|
|
return Command::SUCCESS; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |