Skip to content

Commit f451039

Browse files
authored
Merge pull request #7 from irontec/php-8
Update for php8
2 parents 6f1668b + 2d650b2 commit f451039

13 files changed

+369
-340
lines changed

Attribute/TypeScriptMe.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Irontec\TypeScriptGeneratorBundle\Attribute;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_CLASS)]
8+
class TypeScriptMe
9+
{
10+
}

Command/GenerateAllCommand.php

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
namespace Irontec\TypeScriptGeneratorBundle\Command;
77

8-
use \Symfony\Component\Console\Command\Command;
9-
use \Symfony\Component\Console\Input\{ArrayInput, InputArgument, InputInterface};
10-
use \Symfony\Component\Console\Output\OutputInterface;
11-
use \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\{ArrayInput, InputArgument, InputInterface};
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
1212

1313
/**
1414
* @author Irontec <info@irontec.com>
@@ -17,60 +17,63 @@
1717
*/
1818
class GenerateAllCommand extends Command
1919
{
20-
21-
protected static $defaultName = 'typescript:generate:all';
22-
23-
/**
24-
* @var ParameterBagInterface
25-
*/
26-
private ParameterBagInterface $params;
27-
28-
public function __construct(ParameterBagInterface $params)
20+
public function __construct(private ParameterBagInterface $params)
2921
{
30-
$this->params = $params;
31-
parent::__construct(self::$defaultName);
22+
parent::__construct();
3223
}
3324

34-
protected function configure()
25+
protected function configure(): void
3526
{
27+
/** @var string @projectDir */
28+
$projectDir = $this->params->get('kernel.project_dir');
3629

30+
$this->setName('typescript:generate:all');
3731
$this->setDescription('Execute all commands');
3832
$this->setHelp('bin/console typescript:generate:all interfaces src/Entity');
3933

4034
$this->addArgument('output', InputArgument::REQUIRED, 'Where to generate the interfaces?');
41-
$this->addArgument('entities-dir', InputArgument::OPTIONAL, 'Where are the entities?', $this->params->get('kernel.project_dir') . '/src/Entity/');
42-
$this->addArgument('package-name', InputArgument::OPTIONAL, 'what is the name of the package?');
43-
$this->addArgument('version', InputArgument::OPTIONAL, 'manual version?');
44-
35+
$this->addArgument('entities-dir', InputArgument::OPTIONAL, 'Where are the entities?', "{$projectDir}/src/Entity/");
36+
$this->addArgument('package-name', InputArgument::OPTIONAL, 'What is the name of the package?');
37+
$this->addArgument('version', InputArgument::OPTIONAL, 'Manual version?');
4538
}
4639

4740
protected function execute(InputInterface $input, OutputInterface $output): int
4841
{
42+
$commandInterface = $this->getApplication()?->find('typescript:generate:interfaces');
43+
$commandPackage = $this->getApplication()?->find('typescript:generate:package');
4944

50-
$commandInterface = $this->getApplication()->find('typescript:generate:interfaces');
51-
$commandPackage = $this->getApplication()->find('typescript:generate:package');
45+
if (null === $commandInterface || null === $commandPackage) {
46+
return Command::FAILURE;
47+
}
5248

5349
$dirOutput = $input->getArgument('output');
5450
$dirEntity = $input->getArgument('entities-dir');
5551
$packageName = $input->getArgument('package-name');
5652
$version = $input->getArgument('version');
5753

58-
$argumentsInterface = array(
54+
$argumentsInterface = [
5955
'output' => $dirOutput,
6056
'entities-dir' => $dirEntity
61-
);
57+
];
6258

63-
$argumentsPackage = array(
59+
$argumentsPackage = [
6460
'output' => $dirOutput,
6561
'package-name' => $packageName,
6662
'version' => $version
67-
);
63+
];
6864

69-
$commandInterface->run(new ArrayInput($argumentsInterface), $output);
70-
$commandPackage->run(new ArrayInput($argumentsPackage), $output);
65+
$status = $commandPackage->run(new ArrayInput($argumentsPackage), $output);
7166

72-
return 0;
67+
if (Command::SUCCESS !== $status) {
68+
return Command::INVALID;
69+
}
7370

74-
}
71+
$status = $commandInterface->run(new ArrayInput($argumentsInterface), $output);
72+
73+
if (Command::SUCCESS !== $status) {
74+
return Command::INVALID;
75+
}
7576

77+
return Command::SUCCESS;
78+
}
7679
}

Command/GenerateInterfaceCommand.php

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55

66
namespace Irontec\TypeScriptGeneratorBundle\Command;
77

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;
1615

1716
/**
1817
* @author Irontec <info@irontec.com>
@@ -21,70 +20,73 @@
2120
*/
2221
class GenerateInterfaceCommand extends Command
2322
{
23+
private string $projectDir;
2424

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)
3326
{
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();
3631
}
3732

3833
protected function configure()
3934
{
40-
35+
$this->setName('typescript:generate:interfaces');
4136
$this->setDescription('Generate TypeScript interfaces from Doctrine Entities');
4237
$this->setHelp('bin/console typescript:generate:interfaces interfaces src/Entity');
4338

4439
$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/");
4741
}
4842

4943
protected function execute(InputInterface $input, OutputInterface $output): int
5044
{
51-
45+
/** @var string $dirOutput */
5246
$dirOutput = $input->getArgument('output');
47+
48+
/** @var string $dirEntity */
5349
$dirEntity = $input->getArgument('entities-dir');
5450

55-
$fs = new Filesystem();
5651
$finder = new Finder();
57-
$finder->files('*.php')->in($dirEntity);
58-
59-
$models = array();
52+
$finder->in($dirEntity)->name('*.php');
6053

6154
foreach ($finder as $file) {
6255
$parser = new ParseTypeScript($file->getPathName());
63-
6456
$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;
7157

58+
if (empty($parserOutput)) {
59+
continue;
7260
}
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;
7367
}
7468

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+
}
8072

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;
8477
}
8578

86-
return 0;
79+
$targetFile = $dirOutput . '/models.d.ts';
80+
$this->writeToFile($targetFile, $content);
81+
$output->writeln(sprintf('Created %s', $targetFile));
8782

83+
return Command::SUCCESS;
8884
}
8985

86+
private function writeToFile(string $filename, string $content): void
87+
{
88+
$fs = new Filesystem();
89+
90+
$fs->dumpFile($filename, $content);
91+
}
9092
}

0 commit comments

Comments
 (0)