Skip to content

Commit

Permalink
Merge pull request #7 from irontec/php-8
Browse files Browse the repository at this point in the history
Update for php8
  • Loading branch information
ddniel16 authored Nov 22, 2024
2 parents 6f1668b + 2d650b2 commit f451039
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 340 deletions.
10 changes: 10 additions & 0 deletions Attribute/TypeScriptMe.php
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
{
}
63 changes: 33 additions & 30 deletions Command/GenerateAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand All @@ -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;
}
}
86 changes: 44 additions & 42 deletions Command/GenerateInterfaceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand All @@ -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);
}
}
Loading

0 comments on commit f451039

Please sign in to comment.