|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MagentoCloud\Command; |
| 9 | + |
| 10 | +use Magento\MagentoCloud\Filesystem\ConfigFileList; |
| 11 | +use Magento\MagentoCloud\Config\Environment\ReaderInterface; |
| 12 | +use Magento\MagentoCloud\Filesystem\Driver\File; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Input\InputArgument; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | +use Symfony\Component\Yaml\Yaml; |
| 18 | + |
| 19 | +/** |
| 20 | + * Updates .magento.env.yaml |
| 21 | + */ |
| 22 | +class ConfigUpdate extends Command |
| 23 | +{ |
| 24 | + const NAME = 'cloud:config:update'; |
| 25 | + |
| 26 | + const ARG_CONFIGURATION = 'configuration'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ConfigFileList |
| 30 | + */ |
| 31 | + private $configFileList; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var File |
| 35 | + */ |
| 36 | + private $file; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ReaderInterface |
| 40 | + */ |
| 41 | + private $reader; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param ConfigFileList $configFileList |
| 45 | + * @param File $file |
| 46 | + * @param ReaderInterface $reader |
| 47 | + */ |
| 48 | + public function __construct(ConfigFileList $configFileList, File $file, ReaderInterface $reader) |
| 49 | + { |
| 50 | + $this->configFileList = $configFileList; |
| 51 | + $this->file = $file; |
| 52 | + $this->reader = $reader; |
| 53 | + |
| 54 | + parent::__construct(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritdoc |
| 59 | + */ |
| 60 | + protected function configure() |
| 61 | + { |
| 62 | + $this->setName(static::NAME) |
| 63 | + ->setDescription( |
| 64 | + 'Updates the existing `.magento.env.yaml` file with the specified configuration. ' . |
| 65 | + 'Creates `.magento.env.yaml` file if it does not exist.' |
| 66 | + ) |
| 67 | + ->addArgument( |
| 68 | + self::ARG_CONFIGURATION, |
| 69 | + InputArgument::REQUIRED, |
| 70 | + 'Configuration in JSON format' |
| 71 | + ); |
| 72 | + |
| 73 | + parent::configure(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @inheritDoc |
| 78 | + */ |
| 79 | + public function execute(InputInterface $input, OutputInterface $output) |
| 80 | + { |
| 81 | + $configuration = $input->getArgument(self::ARG_CONFIGURATION); |
| 82 | + |
| 83 | + $decodedConfig = json_decode($configuration, true); |
| 84 | + |
| 85 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 86 | + throw new \Exception('Wrong JSON format: ' . json_last_error_msg()); |
| 87 | + } |
| 88 | + |
| 89 | + $config = array_replace_recursive($this->reader->read(), $decodedConfig); |
| 90 | + |
| 91 | + $yaml = Yaml::dump($config, 10, 2); |
| 92 | + $filePath = $this->configFileList->getEnvConfig(); |
| 93 | + |
| 94 | + $this->file->filePutContents($filePath, $yaml); |
| 95 | + |
| 96 | + $output->writeln(sprintf("Config file %s was updated", $filePath)); |
| 97 | + } |
| 98 | +} |
0 commit comments