|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Command\Utils; |
| 6 | + |
| 7 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 14 | + |
| 15 | +#[AsCommand( |
| 16 | + name: 'app:utils:convert-config-json-to-env', |
| 17 | + description: 'Converts a config json file (admin/client) from 2.x to .env variables used in 3.x', |
| 18 | +)] |
| 19 | +class ConvertConfigJsonToEnvCommand extends Command |
| 20 | +{ |
| 21 | + protected function configure(): void |
| 22 | + { |
| 23 | + $this->addArgument('filepath', InputArgument::REQUIRED, 'Path to the file or URL to convert'); |
| 24 | + $this->addOption('type', 't', InputOption::VALUE_REQUIRED, 'Type of the config (admin or client).', null, ['admin', 'client']); |
| 25 | + } |
| 26 | + |
| 27 | + final protected function execute(InputInterface $input, OutputInterface $output): int |
| 28 | + { |
| 29 | + $io = new SymfonyStyle($input, $output); |
| 30 | + |
| 31 | + $type = $input->getOption('type'); |
| 32 | + |
| 33 | + if (!in_array($type, ['admin', 'client'])) { |
| 34 | + $io->error('Invalid type'); |
| 35 | + |
| 36 | + return Command::INVALID; |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + $content = file_get_contents($input->getArgument('filepath')); |
| 41 | + |
| 42 | + if (false === $content) { |
| 43 | + throw new \Exception('Error reading file'); |
| 44 | + } |
| 45 | + |
| 46 | + $config = json_decode($content, true, 512, JSON_THROW_ON_ERROR); |
| 47 | + } catch (\Exception|\JsonException $e) { |
| 48 | + $io->error($e->getMessage()); |
| 49 | + |
| 50 | + return Command::INVALID; |
| 51 | + } |
| 52 | + |
| 53 | + if ('admin' === $type) { |
| 54 | + $io->success('Insert the following lines in .env.local'); |
| 55 | + |
| 56 | + $rejseplanenApiKey = $config['rejseplanenApiKey'] ?? null; |
| 57 | + $showScreenStatus = $config['showScreenStatus'] ?? null; |
| 58 | + $touchButtonRegions = $config['touchButtonRegions'] ?? null; |
| 59 | + $loginMethods = $config['loginMethods'] ?? null; |
| 60 | + |
| 61 | + if (null !== $loginMethods) { |
| 62 | + // Remove enabled field since this is unused in v3. |
| 63 | + foreach ($loginMethods as &$method) { |
| 64 | + unset($method['enabled']); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + $io->writeln('###> Admin configuration ###'); |
| 69 | + null !== $rejseplanenApiKey && $io->writeln('ADMIN_REJSEPLANEN_APIKEY="'.$rejseplanenApiKey.'"'); |
| 70 | + null !== $showScreenStatus && $io->writeln('ADMIN_SHOW_SCREEN_STATUS='.var_export($showScreenStatus, true)); |
| 71 | + null !== $touchButtonRegions && $io->writeln('ADMIN_TOUCH_BUTTON_REGIONS='.var_export($touchButtonRegions, true)); |
| 72 | + null !== $loginMethods && $io->writeln("ADMIN_LOGIN_METHODS='".json_encode($loginMethods)."'"); |
| 73 | + // This is a conversion from an url to boolean value. If the url is not empty, it is interpreted as true. |
| 74 | + !empty($config['previewClient']) && $io->writeln('ADMIN_ENHANCED_PREVIEW=true'); |
| 75 | + $io->writeln('###< Admin configuration ###'); |
| 76 | + } elseif ('client' === $type) { |
| 77 | + $io->success('Insert the following lines in .env.local'); |
| 78 | + |
| 79 | + $loginCheckTimeout = $config['loginCheckTimeout'] ?? null; |
| 80 | + $refreshTokenTimeout = $config['refreshTokenTimeout'] ?? null; |
| 81 | + $releaseTimestampIntervalTimeout = $config['releaseTimestampIntervalTimeout'] ?? null; |
| 82 | + $schedulingInterval = $config['schedulingInterval'] ?? null; |
| 83 | + $pullStrategyInterval = $config['dataStrategy']['config']['interval'] ?? null; |
| 84 | + $debug = $config['debug'] ?? null; |
| 85 | + |
| 86 | + $colorScheme = $config['colorScheme'] ?? null; |
| 87 | + $colorSchemeValue = null !== $colorScheme ? "'".json_encode($colorScheme)."'" : null; |
| 88 | + |
| 89 | + $io->writeln('###> Client configuration ###'); |
| 90 | + null !== $loginCheckTimeout && $io->writeln('CLIENT_LOGIN_CHECK_TIMEOUT='.$loginCheckTimeout); |
| 91 | + null !== $refreshTokenTimeout && $io->writeln('CLIENT_REFRESH_TOKEN_TIMEOUT='.$refreshTokenTimeout); |
| 92 | + null !== $releaseTimestampIntervalTimeout && $io->writeln('CLIENT_RELEASE_TIMESTAMP_INTERVAL_TIMEOUT='.$releaseTimestampIntervalTimeout); |
| 93 | + null !== $schedulingInterval && $io->writeln('CLIENT_SCHEDULING_INTERVAL='.$schedulingInterval); |
| 94 | + null !== $pullStrategyInterval && $io->writeln('CLIENT_PULL_STRATEGY_INTERVAL='.$pullStrategyInterval); |
| 95 | + null !== $colorSchemeValue && $io->writeln('CLIENT_COLOR_SCHEME='.$colorSchemeValue); |
| 96 | + null !== $debug && $io->writeln('CLIENT_DEBUG=true'); |
| 97 | + $io->writeln('###< Client configuration ###'); |
| 98 | + } |
| 99 | + |
| 100 | + return Command::SUCCESS; |
| 101 | + } |
| 102 | +} |
0 commit comments