-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from irontec/php-8
Update for php8
- Loading branch information
Showing
13 changed files
with
369 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Irontec\TypeScriptGeneratorBundle\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class TypeScriptMe | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ | |
|
||
namespace Irontec\TypeScriptGeneratorBundle\Command; | ||
|
||
use \Symfony\Component\Console\Command\Command; | ||
use \Symfony\Component\Console\Input\{ArrayInput, InputArgument, InputInterface}; | ||
use \Symfony\Component\Console\Output\OutputInterface; | ||
use \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\{ArrayInput, InputArgument, InputInterface}; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
|
||
/** | ||
* @author Irontec <[email protected]> | ||
|
@@ -17,60 +17,63 @@ | |
*/ | ||
class GenerateAllCommand extends Command | ||
{ | ||
|
||
protected static $defaultName = 'typescript:generate:all'; | ||
|
||
/** | ||
* @var ParameterBagInterface | ||
*/ | ||
private ParameterBagInterface $params; | ||
|
||
public function __construct(ParameterBagInterface $params) | ||
public function __construct(private ParameterBagInterface $params) | ||
{ | ||
$this->params = $params; | ||
parent::__construct(self::$defaultName); | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() | ||
protected function configure(): void | ||
{ | ||
/** @var string @projectDir */ | ||
$projectDir = $this->params->get('kernel.project_dir'); | ||
|
||
$this->setName('typescript:generate:all'); | ||
$this->setDescription('Execute all commands'); | ||
$this->setHelp('bin/console typescript:generate:all interfaces src/Entity'); | ||
|
||
$this->addArgument('output', InputArgument::REQUIRED, 'Where to generate the interfaces?'); | ||
$this->addArgument('entities-dir', InputArgument::OPTIONAL, 'Where are the entities?', $this->params->get('kernel.project_dir') . '/src/Entity/'); | ||
$this->addArgument('package-name', InputArgument::OPTIONAL, 'what is the name of the package?'); | ||
$this->addArgument('version', InputArgument::OPTIONAL, 'manual version?'); | ||
|
||
$this->addArgument('entities-dir', InputArgument::OPTIONAL, 'Where are the entities?', "{$projectDir}/src/Entity/"); | ||
$this->addArgument('package-name', InputArgument::OPTIONAL, 'What is the name of the package?'); | ||
$this->addArgument('version', InputArgument::OPTIONAL, 'Manual version?'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$commandInterface = $this->getApplication()?->find('typescript:generate:interfaces'); | ||
$commandPackage = $this->getApplication()?->find('typescript:generate:package'); | ||
|
||
$commandInterface = $this->getApplication()->find('typescript:generate:interfaces'); | ||
$commandPackage = $this->getApplication()->find('typescript:generate:package'); | ||
if (null === $commandInterface || null === $commandPackage) { | ||
return Command::FAILURE; | ||
} | ||
|
||
$dirOutput = $input->getArgument('output'); | ||
$dirEntity = $input->getArgument('entities-dir'); | ||
$packageName = $input->getArgument('package-name'); | ||
$version = $input->getArgument('version'); | ||
|
||
$argumentsInterface = array( | ||
$argumentsInterface = [ | ||
'output' => $dirOutput, | ||
'entities-dir' => $dirEntity | ||
); | ||
]; | ||
|
||
$argumentsPackage = array( | ||
$argumentsPackage = [ | ||
'output' => $dirOutput, | ||
'package-name' => $packageName, | ||
'version' => $version | ||
); | ||
]; | ||
|
||
$commandInterface->run(new ArrayInput($argumentsInterface), $output); | ||
$commandPackage->run(new ArrayInput($argumentsPackage), $output); | ||
$status = $commandPackage->run(new ArrayInput($argumentsPackage), $output); | ||
|
||
return 0; | ||
if (Command::SUCCESS !== $status) { | ||
return Command::INVALID; | ||
} | ||
|
||
} | ||
$status = $commandInterface->run(new ArrayInput($argumentsInterface), $output); | ||
|
||
if (Command::SUCCESS !== $status) { | ||
return Command::INVALID; | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,13 @@ | |
|
||
namespace Irontec\TypeScriptGeneratorBundle\Command; | ||
|
||
use \Symfony\Component\Console\Command\Command; | ||
use \Symfony\Component\Console\Input\{InputArgument, InputInterface}; | ||
use \Symfony\Component\Console\Output\OutputInterface; | ||
use \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use \Symfony\Component\Filesystem\Filesystem; | ||
use \Symfony\Component\Finder\Finder; | ||
|
||
use \Irontec\TypeScriptGeneratorBundle\ParseTypeScript\Parser as ParseTypeScript; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\{InputArgument, InputInterface}; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Finder\Finder; | ||
use Irontec\TypeScriptGeneratorBundle\ParseTypeScript\Parser as ParseTypeScript; | ||
|
||
/** | ||
* @author Irontec <[email protected]> | ||
|
@@ -21,70 +20,73 @@ | |
*/ | ||
class GenerateInterfaceCommand extends Command | ||
{ | ||
private string $projectDir; | ||
|
||
protected static $defaultName = 'typescript:generate:interfaces'; | ||
|
||
/** | ||
* @var ParameterBagInterface | ||
*/ | ||
private ParameterBagInterface $params; | ||
|
||
public function __construct(ParameterBagInterface $params) | ||
public function __construct(private ParameterBagInterface $params) | ||
{ | ||
$this->params = $params; | ||
parent::__construct(self::$defaultName); | ||
/** @var string $projectDir */ | ||
$this->projectDir = $this->params->get('kernel.project_dir'); | ||
|
||
parent::__construct(); | ||
} | ||
|
||
protected function configure() | ||
{ | ||
|
||
$this->setName('typescript:generate:interfaces'); | ||
$this->setDescription('Generate TypeScript interfaces from Doctrine Entities'); | ||
$this->setHelp('bin/console typescript:generate:interfaces interfaces src/Entity'); | ||
|
||
$this->addArgument('output', InputArgument::REQUIRED, 'Where to generate the interfaces?'); | ||
$this->addArgument('entities-dir', InputArgument::OPTIONAL, 'Where are the entities?', $this->params->get('kernel.project_dir') . '/src/Entity/'); | ||
|
||
$this->addArgument('entities-dir', InputArgument::OPTIONAL, 'Where are the entities?', "{$this->projectDir}/src/Entity/"); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
|
||
/** @var string $dirOutput */ | ||
$dirOutput = $input->getArgument('output'); | ||
|
||
/** @var string $dirEntity */ | ||
$dirEntity = $input->getArgument('entities-dir'); | ||
|
||
$fs = new Filesystem(); | ||
$finder = new Finder(); | ||
$finder->files('*.php')->in($dirEntity); | ||
|
||
$models = array(); | ||
$finder->in($dirEntity)->name('*.php'); | ||
|
||
foreach ($finder as $file) { | ||
$parser = new ParseTypeScript($file->getPathName()); | ||
|
||
$parserOutput = $parser->getOutput(); | ||
if (empty($parserOutput) === false) { | ||
|
||
$targetFile = $dirOutput . '/' . str_replace( '.php','.d.ts', $file->getFilename()); | ||
$fs->dumpFile($targetFile, $parserOutput); | ||
$output->writeln('Created interface ' . $targetFile); | ||
$models[] = $parser->getCurrentInterface()->name; | ||
|
||
if (empty($parserOutput)) { | ||
continue; | ||
} | ||
|
||
$targetFile = "{$this->projectDir}/{$dirOutput}/" . str_replace('.php', '.d.ts', $file->getFilename()); | ||
$this->writeToFile($targetFile, $parserOutput); | ||
$output->writeln(sprintf('Created interface %s', $targetFile)); | ||
|
||
$models[] = $parser->getCurrentInterface()->name; | ||
} | ||
|
||
if (empty($models) === false) { | ||
$tmp = ''; | ||
foreach ($models as $model) { | ||
$tmp .= "export * from './" . $model . "';" . PHP_EOL; | ||
} | ||
if (!isset($models)) { | ||
return Command::SUCCESS; | ||
} | ||
|
||
$targetFile = $dirOutput . '/models.d.ts'; | ||
$fs->dumpFile($targetFile, $tmp . PHP_EOL); | ||
$output->writeln('Created ' . $targetFile); | ||
$content = array_reduce($models, fn ($content, $model) => sprintf("%sexport * from './%s';%s", $content, $model, PHP_EOL)); | ||
|
||
if (!is_string($content)) { | ||
return Command::SUCCESS; | ||
} | ||
|
||
return 0; | ||
$targetFile = $dirOutput . '/models.d.ts'; | ||
$this->writeToFile($targetFile, $content); | ||
$output->writeln(sprintf('Created %s', $targetFile)); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function writeToFile(string $filename, string $content): void | ||
{ | ||
$fs = new Filesystem(); | ||
|
||
$fs->dumpFile($filename, $content); | ||
} | ||
} |
Oops, something went wrong.