|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of skillshare/formatphp |
| 5 | + * |
| 6 | + * skillshare/formatphp is open source software: you can distribute |
| 7 | + * it and/or modify it under the terms of the MIT License |
| 8 | + * (the "License"). You may not use this file except in |
| 9 | + * compliance with the License. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | + * implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + * |
| 17 | + * @copyright Copyright (c) Skillshare, Inc. <https://www.skillshare.com> |
| 18 | + * @license https://opensource.org/licenses/MIT MIT License |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace FormatPHP\Console\Command; |
| 24 | + |
| 25 | +use FormatPHP\Exception; |
| 26 | +use FormatPHP\PseudoLocale\Converter; |
| 27 | +use FormatPHP\PseudoLocale\ConverterOptions; |
| 28 | +use FormatPHP\PseudoLocale\PseudoLocale; |
| 29 | +use FormatPHP\Util\FileSystemHelper; |
| 30 | +use FormatPHP\Util\FormatHelper; |
| 31 | +use Symfony\Component\Console\Exception\InvalidArgumentException as SymfonyInvalidArgumentException; |
| 32 | +use Symfony\Component\Console\Input\InputArgument; |
| 33 | +use Symfony\Component\Console\Input\InputInterface; |
| 34 | +use Symfony\Component\Console\Input\InputOption; |
| 35 | +use Symfony\Component\Console\Output\OutputInterface; |
| 36 | + |
| 37 | +use function implode; |
| 38 | + |
| 39 | +use const PHP_EOL; |
| 40 | + |
| 41 | +/** |
| 42 | + * Provides the `formatphp pseudo-locale` command |
| 43 | + */ |
| 44 | +class PseudoLocaleCommand extends AbstractCommand |
| 45 | +{ |
| 46 | + /** |
| 47 | + * @throws SymfonyInvalidArgumentException |
| 48 | + */ |
| 49 | + protected function configure(): void |
| 50 | + { |
| 51 | + $this |
| 52 | + ->setName('pseudo-locale') |
| 53 | + ->setDescription('Convert a locale file into a pseudo locale, for testing') |
| 54 | + ->addArgument( |
| 55 | + 'file', |
| 56 | + InputArgument::REQUIRED, |
| 57 | + 'The locale file to convert.', |
| 58 | + ) |
| 59 | + ->addArgument( |
| 60 | + 'pseudo-locale', |
| 61 | + InputArgument::REQUIRED, |
| 62 | + 'The pseudo-locale tag.' . PHP_EOL |
| 63 | + . 'One of: ' . implode(', ', PseudoLocale::LOCALES), |
| 64 | + ) |
| 65 | + ->addOption( |
| 66 | + 'in-format', |
| 67 | + null, |
| 68 | + InputOption::VALUE_REQUIRED, |
| 69 | + 'Formatter name or path to a formatter script that' . PHP_EOL |
| 70 | + . 'controls the shape of the JSON read from `file`.' . PHP_EOL |
| 71 | + . 'Defaults to "formatphp".', |
| 72 | + ) |
| 73 | + ->addOption( |
| 74 | + 'out-format', |
| 75 | + null, |
| 76 | + InputOption::VALUE_REQUIRED, |
| 77 | + 'Formatter name or path to a formatter script that' . PHP_EOL |
| 78 | + . 'controls the shape of the JSON produced as output.', |
| 79 | + ) |
| 80 | + ->addOption( |
| 81 | + 'out-file', |
| 82 | + null, |
| 83 | + InputOption::VALUE_REQUIRED, |
| 84 | + 'Target file path to save the JSON output.', |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @throws Exception\InvalidArgumentException |
| 90 | + * @throws Exception\UnableToWriteFileException |
| 91 | + * @throws Exception\UnableToProcessFileException |
| 92 | + * @throws Exception\ImproperContextException |
| 93 | + * @throws SymfonyInvalidArgumentException |
| 94 | + */ |
| 95 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 96 | + { |
| 97 | + /** @var string $file */ |
| 98 | + $file = $input->getArgument('file'); |
| 99 | + |
| 100 | + /** @var string $pseudoLocale */ |
| 101 | + $pseudoLocale = $input->getArgument('pseudo-locale'); |
| 102 | + |
| 103 | + $options = $this->buildOptions($input); |
| 104 | + $fileSystemHelper = new FileSystemHelper(); |
| 105 | + |
| 106 | + $converter = new Converter( |
| 107 | + $options, |
| 108 | + $fileSystemHelper, |
| 109 | + new FormatHelper($fileSystemHelper), |
| 110 | + $this->getConsoleLogger($output), |
| 111 | + ); |
| 112 | + |
| 113 | + $converter->convert($file, $pseudoLocale); |
| 114 | + |
| 115 | + return self::SUCCESS; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @throws SymfonyInvalidArgumentException |
| 120 | + */ |
| 121 | + private function buildOptions(InputInterface $input): ConverterOptions |
| 122 | + { |
| 123 | + $options = new ConverterOptions(); |
| 124 | + |
| 125 | + /** @var string | null $inFormat */ |
| 126 | + $inFormat = $input->getOption('in-format'); |
| 127 | + $options->inFormat = $inFormat; |
| 128 | + |
| 129 | + /** @var string | null $outFormat */ |
| 130 | + $outFormat = $input->getOption('out-format'); |
| 131 | + $options->outFormat = $outFormat; |
| 132 | + |
| 133 | + /** @var string | null $outFile */ |
| 134 | + $outFile = $input->getOption('out-file'); |
| 135 | + $options->outFile = $outFile; |
| 136 | + |
| 137 | + return $options; |
| 138 | + } |
| 139 | +} |
0 commit comments