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\TgScraper; |
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
|
|
|
->addOption( |
49
|
|
|
'prefer-stable', |
50
|
|
|
null, |
51
|
|
|
InputOption::VALUE_NONE, |
52
|
|
|
'Prefer latest stable version (takes precedence over "--layer")' |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
57
|
|
|
{ |
58
|
|
|
$logger = new ConsoleLogger($output); |
59
|
|
|
$version = Versions::getVersionFromText($input->getOption('layer')); |
60
|
|
|
if ($input->getOption('prefer-stable')) { |
61
|
|
|
$version = Versions::STABLE; |
62
|
|
|
} |
63
|
|
|
$yamlPath = $input->getOption('yaml'); |
64
|
|
|
if (empty($yamlPath)) { |
65
|
|
|
$jsonPath = $input->getOption('json'); |
66
|
|
|
if (empty($jsonPath)) { |
67
|
|
|
$logger->info('Using version: ' . $version); |
68
|
|
|
try { |
69
|
|
|
$output->writeln('Fetching data for version...'); |
70
|
|
|
$generator = TgScraper::fromVersion($logger, $version); |
71
|
|
|
} catch (Throwable) { |
72
|
|
|
return Command::FAILURE; |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
$data = file_get_contents($jsonPath); |
76
|
|
|
if (!$this->validateData($data)) { |
77
|
|
|
$logger->critical('Invalid JSON file provided'); |
78
|
|
|
return Command::INVALID; |
79
|
|
|
} |
80
|
|
|
$logger->info('Using JSON schema: ' . $jsonPath); |
81
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
82
|
|
|
$generator = TgScraper::fromJson($logger, $data); |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$data = file_get_contents($yamlPath); |
86
|
|
|
if (!$this->validateData($data)) { |
87
|
|
|
$logger->critical('Invalid YAML file provided'); |
88
|
|
|
return Command::INVALID; |
89
|
|
|
} |
90
|
|
|
$logger->info('Using YAML schema: ' . $yamlPath); |
91
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
92
|
|
|
$generator = TgScraper::fromYaml($logger, $data); |
93
|
|
|
} |
94
|
|
|
try { |
95
|
|
|
$output->writeln('Creating stubs...'); |
96
|
|
|
$generator->toStubs($input->getArgument('destination'), $input->getOption('namespace-prefix')); |
97
|
|
|
} catch (Exception) { |
98
|
|
|
$logger->critical('Could not create stubs.'); |
99
|
|
|
return Command::FAILURE; |
100
|
|
|
} |
101
|
|
|
$output->writeln('Done!'); |
102
|
|
|
return Command::SUCCESS; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |