|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of friendsofhyperf/components. |
| 6 | + * |
| 7 | + * @link https://github.com/friendsofhyperf/components |
| 8 | + * @document https://github.com/friendsofhyperf/components/blob/main/README.md |
| 9 | + * @contact huangdijia@gmail.com |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FriendsOfHyperf\ValidatedDTO\Command; |
| 13 | + |
| 14 | +use Exception; |
| 15 | +use FriendsOfHyperf\ValidatedDTO\Exporter\ExporterInterface; |
| 16 | +use FriendsOfHyperf\ValidatedDTO\Exporter\TypeScriptExporter; |
| 17 | +use InvalidArgumentException; |
| 18 | +use Psr\Container\ContainerInterface; |
| 19 | +use RuntimeException; |
| 20 | +use Symfony\Component\Console\Command\Command as SymfonyCommand; |
| 21 | +use Symfony\Component\Console\Input\InputArgument; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Input\InputOption; |
| 24 | +use Symfony\Component\Console\Output\OutputInterface; |
| 25 | + |
| 26 | +class ExportDTOCommand extends SymfonyCommand |
| 27 | +{ |
| 28 | + public const DEFAULT_LANGUAGE = 'typescript'; |
| 29 | + |
| 30 | + public const SUPPORTED_LANGUAGES = ['typescript', 'ts']; |
| 31 | + |
| 32 | + public const FILE_PERMISSIONS = 0755; |
| 33 | + |
| 34 | + protected InputInterface $input; |
| 35 | + |
| 36 | + protected OutputInterface $output; |
| 37 | + |
| 38 | + public function __construct(protected ContainerInterface $container) |
| 39 | + { |
| 40 | + parent::__construct('dto:export'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Execute the console command. |
| 45 | + */ |
| 46 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 47 | + { |
| 48 | + $this->input = $input; |
| 49 | + $this->output = $output; |
| 50 | + |
| 51 | + $class = $input->getArgument('class'); |
| 52 | + $outputPath = $input->getOption('output'); |
| 53 | + $lang = $input->getOption('lang'); |
| 54 | + |
| 55 | + try { |
| 56 | + $exporter = $this->getExporter($lang); |
| 57 | + $exported = $exporter->export($class); |
| 58 | + |
| 59 | + if ($outputPath) { |
| 60 | + $this->writeToFile($outputPath, $exported); |
| 61 | + $output->writeln(sprintf('<info>DTO exported to %s</info>', $outputPath)); |
| 62 | + } else { |
| 63 | + $output->writeln($exported); |
| 64 | + } |
| 65 | + |
| 66 | + return 0; |
| 67 | + } catch (InvalidArgumentException $e) { |
| 68 | + $output->writeln(sprintf('<fg=red>%s</>', $e->getMessage())); |
| 69 | + return 1; |
| 70 | + } catch (Exception $e) { |
| 71 | + $output->writeln(sprintf('<error>Error: %s</error>', $e->getMessage())); |
| 72 | + return 1; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + protected function configure(): void |
| 77 | + { |
| 78 | + foreach ($this->getArguments() as $argument) { |
| 79 | + $this->addArgument(...$argument); |
| 80 | + } |
| 81 | + |
| 82 | + foreach ($this->getOptions() as $option) { |
| 83 | + $this->addOption(...$option); |
| 84 | + } |
| 85 | + |
| 86 | + $this->setDescription('Export DTO classes to various formats.'); |
| 87 | + $this->setAliases([]); |
| 88 | + } |
| 89 | + |
| 90 | + protected function getExporter(string $lang): ExporterInterface |
| 91 | + { |
| 92 | + return match ($lang) { |
| 93 | + 'typescript', 'ts' => $this->container->get(TypeScriptExporter::class), |
| 94 | + default => throw new InvalidArgumentException("Unsupported language: {$lang}"), |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + protected function writeToFile(string $path, string $content): void |
| 99 | + { |
| 100 | + // Validate path to prevent directory traversal attacks |
| 101 | + if (str_contains($path, '..') || str_contains($path, '~')) { |
| 102 | + throw new InvalidArgumentException('Invalid file path provided.'); |
| 103 | + } |
| 104 | + |
| 105 | + $directory = dirname($path); |
| 106 | + if (! is_dir($directory)) { |
| 107 | + mkdir($directory, self::FILE_PERMISSIONS, true); |
| 108 | + } |
| 109 | + |
| 110 | + if (file_exists($path) && ! $this->input->getOption('force')) { |
| 111 | + throw new RuntimeException(sprintf('File %s already exists! Use --force to overwrite.', $path)); |
| 112 | + } |
| 113 | + |
| 114 | + file_put_contents($path, $content); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Get the console command arguments. |
| 119 | + */ |
| 120 | + protected function getArguments(): array |
| 121 | + { |
| 122 | + return [ |
| 123 | + ['class', InputArgument::REQUIRED, 'The fully qualified class name of the DTO'], |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Get the console command options. |
| 129 | + */ |
| 130 | + protected function getOptions(): array |
| 131 | + { |
| 132 | + return [ |
| 133 | + ['output', 'o', InputOption::VALUE_OPTIONAL, 'Output file path', null], |
| 134 | + ['force', 'f', InputOption::VALUE_NONE, 'Overwrite existing files'], |
| 135 | + ['lang', 'l', InputOption::VALUE_OPTIONAL, 'Export language (typescript|ts)', self::DEFAULT_LANGUAGE], |
| 136 | + ]; |
| 137 | + } |
| 138 | +} |
0 commit comments