|
5 | 5 |
|
6 | 6 | namespace Irontec\TypeScriptGeneratorBundle\Command; |
7 | 7 |
|
8 | | -use \Symfony\Component\Console\Command\Command; |
9 | | -use \Symfony\Component\Console\Input\{InputArgument, InputInterface}; |
10 | | -use \Symfony\Component\Console\Output\OutputInterface; |
11 | | -use \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
12 | | -use \Symfony\Component\Filesystem\Filesystem; |
13 | | -use \Symfony\Component\Finder\Finder; |
14 | | - |
15 | | -use \Irontec\TypeScriptGeneratorBundle\ParseTypeScript\Parser as ParseTypeScript; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\{InputArgument, InputInterface}; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
| 12 | +use Symfony\Component\Filesystem\Filesystem; |
| 13 | +use Symfony\Component\Finder\Finder; |
| 14 | +use Irontec\TypeScriptGeneratorBundle\ParseTypeScript\Parser as ParseTypeScript; |
16 | 15 |
|
17 | 16 | /** |
18 | 17 | * @author Irontec <info@irontec.com> |
|
21 | 20 | */ |
22 | 21 | class GenerateInterfaceCommand extends Command |
23 | 22 | { |
| 23 | + private string $projectDir; |
24 | 24 |
|
25 | | - protected static $defaultName = 'typescript:generate:interfaces'; |
26 | | - |
27 | | - /** |
28 | | - * @var ParameterBagInterface |
29 | | - */ |
30 | | - private ParameterBagInterface $params; |
31 | | - |
32 | | - public function __construct(ParameterBagInterface $params) |
| 25 | + public function __construct(private ParameterBagInterface $params) |
33 | 26 | { |
34 | | - $this->params = $params; |
35 | | - parent::__construct(self::$defaultName); |
| 27 | + /** @var string $projectDir */ |
| 28 | + $this->projectDir = $this->params->get('kernel.project_dir'); |
| 29 | + |
| 30 | + parent::__construct(); |
36 | 31 | } |
37 | 32 |
|
38 | 33 | protected function configure() |
39 | 34 | { |
40 | | - |
| 35 | + $this->setName('typescript:generate:interfaces'); |
41 | 36 | $this->setDescription('Generate TypeScript interfaces from Doctrine Entities'); |
42 | 37 | $this->setHelp('bin/console typescript:generate:interfaces interfaces src/Entity'); |
43 | 38 |
|
44 | 39 | $this->addArgument('output', InputArgument::REQUIRED, 'Where to generate the interfaces?'); |
45 | | - $this->addArgument('entities-dir', InputArgument::OPTIONAL, 'Where are the entities?', $this->params->get('kernel.project_dir') . '/src/Entity/'); |
46 | | - |
| 40 | + $this->addArgument('entities-dir', InputArgument::OPTIONAL, 'Where are the entities?', "{$this->projectDir}/src/Entity/"); |
47 | 41 | } |
48 | 42 |
|
49 | 43 | protected function execute(InputInterface $input, OutputInterface $output): int |
50 | 44 | { |
51 | | - |
| 45 | + /** @var string $dirOutput */ |
52 | 46 | $dirOutput = $input->getArgument('output'); |
| 47 | + |
| 48 | + /** @var string $dirEntity */ |
53 | 49 | $dirEntity = $input->getArgument('entities-dir'); |
54 | 50 |
|
55 | | - $fs = new Filesystem(); |
56 | 51 | $finder = new Finder(); |
57 | | - $finder->files('*.php')->in($dirEntity); |
58 | | - |
59 | | - $models = array(); |
| 52 | + $finder->in($dirEntity)->name('*.php'); |
60 | 53 |
|
61 | 54 | foreach ($finder as $file) { |
62 | 55 | $parser = new ParseTypeScript($file->getPathName()); |
63 | | - |
64 | 56 | $parserOutput = $parser->getOutput(); |
65 | | - if (empty($parserOutput) === false) { |
66 | | - |
67 | | - $targetFile = $dirOutput . '/' . str_replace( '.php','.d.ts', $file->getFilename()); |
68 | | - $fs->dumpFile($targetFile, $parserOutput); |
69 | | - $output->writeln('Created interface ' . $targetFile); |
70 | | - $models[] = $parser->getCurrentInterface()->name; |
71 | 57 |
|
| 58 | + if (empty($parserOutput)) { |
| 59 | + continue; |
72 | 60 | } |
| 61 | + |
| 62 | + $targetFile = "{$this->projectDir}/{$dirOutput}/" . str_replace('.php', '.d.ts', $file->getFilename()); |
| 63 | + $this->writeToFile($targetFile, $parserOutput); |
| 64 | + $output->writeln(sprintf('Created interface %s', $targetFile)); |
| 65 | + |
| 66 | + $models[] = $parser->getCurrentInterface()->name; |
73 | 67 | } |
74 | 68 |
|
75 | | - if (empty($models) === false) { |
76 | | - $tmp = ''; |
77 | | - foreach ($models as $model) { |
78 | | - $tmp .= "export * from './" . $model . "';" . PHP_EOL; |
79 | | - } |
| 69 | + if (!isset($models)) { |
| 70 | + return Command::SUCCESS; |
| 71 | + } |
80 | 72 |
|
81 | | - $targetFile = $dirOutput . '/models.d.ts'; |
82 | | - $fs->dumpFile($targetFile, $tmp . PHP_EOL); |
83 | | - $output->writeln('Created ' . $targetFile); |
| 73 | + $content = array_reduce($models, fn ($content, $model) => sprintf("%sexport * from './%s';%s", $content, $model, PHP_EOL)); |
| 74 | + |
| 75 | + if (!is_string($content)) { |
| 76 | + return Command::SUCCESS; |
84 | 77 | } |
85 | 78 |
|
86 | | - return 0; |
| 79 | + $targetFile = $dirOutput . '/models.d.ts'; |
| 80 | + $this->writeToFile($targetFile, $content); |
| 81 | + $output->writeln(sprintf('Created %s', $targetFile)); |
87 | 82 |
|
| 83 | + return Command::SUCCESS; |
88 | 84 | } |
89 | 85 |
|
| 86 | + private function writeToFile(string $filename, string $content): void |
| 87 | + { |
| 88 | + $fs = new Filesystem(); |
| 89 | + |
| 90 | + $fs->dumpFile($filename, $content); |
| 91 | + } |
90 | 92 | } |
0 commit comments