1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AurimasNiekis\TdLibSchema\Generator; |
6
|
|
|
|
7
|
|
|
use AurimasNiekis\TdLibSchema\Generator\Parser\SchemaParser; |
8
|
|
|
use Monolog\Logger; |
9
|
|
|
use Symfony\Bridge\Monolog\Handler\ConsoleHandler; |
10
|
|
|
use Symfony\Component\Console\Application; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Aurimas Niekis <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ConsoleApplication extends Command |
20
|
|
|
{ |
21
|
|
|
private string $version = '1.0'; |
22
|
|
|
private bool $running = false; |
23
|
|
|
|
24
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null): int |
25
|
|
|
{ |
26
|
|
|
if ($this->running) { |
27
|
|
|
return parent::run($input, $output); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// We use the command name as the application name |
31
|
|
|
$application = new Application($this->getName() ?: 'UNKNOWN', $this->version); |
32
|
|
|
|
33
|
|
|
// Fix the usage of the command displayed with "--help" |
34
|
|
|
$this->setName($_SERVER['argv'][0]); |
35
|
|
|
$application->add($this); |
36
|
|
|
$application->setDefaultCommand($this->getName(), true); |
37
|
|
|
|
38
|
|
|
$this->running = true; |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
$ret = $application->run($input, $output); |
42
|
|
|
} finally { |
43
|
|
|
$this->running = false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $ret ?? 1; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function configure(): void |
50
|
|
|
{ |
51
|
|
|
$this->addArgument( |
52
|
|
|
'tl_file', |
53
|
|
|
InputArgument::OPTIONAL, |
54
|
|
|
'Telegram TL file to generate classes from' |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
$logger = new Logger('generator'); |
61
|
|
|
$logger->pushHandler(new ConsoleHandler($output)); |
62
|
|
|
|
63
|
|
|
$parser = new SchemaParser($logger, $input->getArgument('tl_file')); |
|
|
|
|
64
|
|
|
$classes = $parser->parse(); |
65
|
|
|
|
66
|
|
|
$generator = new CodeGenerator('AurimasNiekis\TdLibSchema', __DIR__ . '/../src'); |
67
|
|
|
$generator->generate($classes); |
68
|
|
|
|
69
|
|
|
return 0; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|