Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.89% |
34 / 37 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ApieMakeTranslationFileCommand | |
91.89% |
34 / 37 |
|
83.33% |
5 / 6 |
13.09 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| configure | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
85.71% |
18 / 21 |
|
0.00% |
0 / 1 |
8.19 | |||
| renderPhpFile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| renderJsonFile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| renderYamlFile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Command; |
| 3 | |
| 4 | use Apie\Common\Translator\TranslationCollector; |
| 5 | use Apie\Core\Other\FileWriterInterface; |
| 6 | use Composer\Console\Input\InputArgument; |
| 7 | use Composer\Console\Input\InputOption; |
| 8 | use Symfony\Component\Console\Command\Command; |
| 9 | use Symfony\Component\Console\Input\InputInterface; |
| 10 | use Symfony\Component\Console\Output\NullOutput; |
| 11 | use Symfony\Component\Console\Output\OutputInterface; |
| 12 | use Symfony\Component\Yaml\Yaml; |
| 13 | |
| 14 | class ApieMakeTranslationFileCommand extends Command |
| 15 | { |
| 16 | public function __construct( |
| 17 | protected readonly TranslationCollector $collector, |
| 18 | protected readonly FileWriterInterface $fileWriter, |
| 19 | ) { |
| 20 | parent::__construct('apie:make-translation-file'); |
| 21 | } |
| 22 | protected function configure(): void |
| 23 | { |
| 24 | $this->setDescription('This command will create a translation file for Apie.'); |
| 25 | $this->addArgument('filename', InputArgument::OPTIONAL, 'filename to write', 'php://stdout'); |
| 26 | $this->addOption('specifity-filter', 's', InputOption::VALUE_OPTIONAL, 'Only create text with a certain specifity to reduce number of texts', 99); |
| 27 | } |
| 28 | |
| 29 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 30 | { |
| 31 | $filename = $input->getArgument('filename'); |
| 32 | $method = 'renderPhpFile'; |
| 33 | if ($filename === 'php://stdout') { |
| 34 | $output = new NullOutput(); |
| 35 | } else { |
| 36 | $ext = pathinfo($filename, PATHINFO_EXTENSION); |
| 37 | if ($ext) { |
| 38 | $method = match($ext) { |
| 39 | 'php' => 'renderPhpFile', |
| 40 | 'json' => 'renderJsonFile', |
| 41 | 'yml' => 'renderYamlFile', |
| 42 | 'yaml' => 'renderYamlFile', |
| 43 | default => throw new \LogicException("Unknown extension " . $ext), |
| 44 | }; |
| 45 | } else { |
| 46 | $filename .= '.php'; |
| 47 | } |
| 48 | } |
| 49 | $output->writeln('Collecting all translations...'); |
| 50 | $translationList = $this->collector->createList(); |
| 51 | $output->writeln('Done'); |
| 52 | $output->writeln('Creating file....'); |
| 53 | $this->$method($filename, $translationList->toNestedArray()); |
| 54 | $output->writeln('Created ' . $filename . ' successfully'); |
| 55 | return Command::SUCCESS; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param array<array-key, mixed> $input |
| 60 | */ |
| 61 | private function renderPhpFile(string $filename, array $input): void |
| 62 | { |
| 63 | $this->fileWriter->writeFile( |
| 64 | $filename, |
| 65 | "<?php" . PHP_EOL . "return " . var_export($input, true) . ';' |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param array<array-key, mixed> $input |
| 71 | */ |
| 72 | private function renderJsonFile(string $filename, array $input): void |
| 73 | { |
| 74 | $this->fileWriter->writeFile( |
| 75 | $filename, |
| 76 | json_encode($input, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param array<array-key, mixed> $input |
| 82 | */ |
| 83 | private function renderYamlFile(string $filename, array $input): void |
| 84 | { |
| 85 | $this->fileWriter->writeFile( |
| 86 | $filename, |
| 87 | Yaml::dump($input, inline: 9) |
| 88 | ); |
| 89 | } |
| 90 | } |