|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * RocketWeb |
| 4 | + * |
| 5 | + * NOTICE OF LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the Open Software License (OSL 3.0) |
| 8 | + * that is bundled with this package in the file LICENSE.txt. |
| 9 | + * It is also available through the world-wide-web at this URL: |
| 10 | + * http://opensource.org/licenses/osl-3.0.php |
| 11 | + * |
| 12 | + * @category RocketWeb |
| 13 | + * @copyright Copyright (c) 2022 RocketWeb (http://rocketweb.com) |
| 14 | + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
| 15 | + * @author Rocket Web Inc. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace RocketWeb\ConfigExport\Console\Command; |
| 19 | + |
| 20 | +use Symfony\Component\Console\Command\Command; |
| 21 | +use Symfony\Component\Console\Input\InputArgument; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | + |
| 25 | +class ExportConfigsCommand extends Command |
| 26 | +{ |
| 27 | + private const ARG_SCOPES = 'scopes'; |
| 28 | + private const ARG_PATHS = 'paths'; |
| 29 | + private const ALLOWED_SCOPES = ['default', 'websites', 'stores']; |
| 30 | + |
| 31 | + private \RocketWeb\ConfigExport\Provider\Fetch $fetch; |
| 32 | + private \RocketWeb\ConfigExport\Handler\Config $configHandler; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + \RocketWeb\ConfigExport\Handler\Config $configHandler, |
| 36 | + \RocketWeb\ConfigExport\Provider\Fetch $fetch, |
| 37 | + string $name = null |
| 38 | + ) { |
| 39 | + parent::__construct($name); |
| 40 | + $this->fetch = $fetch; |
| 41 | + $this->configHandler = $configHandler; |
| 42 | + } |
| 43 | + |
| 44 | + protected function configure(): void |
| 45 | + { |
| 46 | + $this->setName('config:data:export'); |
| 47 | + $this->setDescription('Export specific configuration into config.xml to make it VCS transferable' |
| 48 | + . ' without locking them by using app/etc/config.php or app/etc/env.php'); |
| 49 | + |
| 50 | + $this->addArgument( |
| 51 | + self::ARG_SCOPES, |
| 52 | + InputArgument::REQUIRED, |
| 53 | + 'Scopes for which you want to export values for. CSV values are allowed. ' |
| 54 | + . 'Options: all|' . implode('|', self::ALLOWED_SCOPES) |
| 55 | + ); |
| 56 | + $this->addArgument( |
| 57 | + self::ARG_PATHS, |
| 58 | + InputArgument::REQUIRED, |
| 59 | + 'Path(s) that you want to export. Wildcard support as asterisk for second and third section' |
| 60 | + . ' of the path (*). Example: trans_email/*/email' |
| 61 | + ); |
| 62 | + |
| 63 | + parent::configure(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 68 | + * @throws \Magento\Framework\Exception\InvalidArgumentException |
| 69 | + */ |
| 70 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 71 | + { |
| 72 | + $paths = $input->getArgument(self::ARG_PATHS); |
| 73 | + $paths = array_filter(array_map('trim', explode(',', $paths))); |
| 74 | + |
| 75 | + if (count($paths) == 0) { |
| 76 | + $output->writeln('No valid paths provided, existing'); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + $scopes = trim($input->getArgument(self::ARG_SCOPES)); |
| 81 | + if ($scopes === 'all') { |
| 82 | + $scopes = implode(',', self::ALLOWED_SCOPES); |
| 83 | + } |
| 84 | + $scopes = array_filter(array_map('trim', explode(',', $scopes))); |
| 85 | + $scopes = array_map('strtolower', $scopes); |
| 86 | + foreach ($scopes as $scope) { |
| 87 | + if (!in_array($scope, self::ALLOWED_SCOPES)) { |
| 88 | + $output->writeln('Scope "' . $scope . '" is not valid. Accepted values: all|' |
| 89 | + . implode('|', self::ALLOWED_SCOPES)); |
| 90 | + return; |
| 91 | + } |
| 92 | + } |
| 93 | + $values = []; |
| 94 | + foreach ($paths as $path) { |
| 95 | + $values = array_merge_recursive($values, $this->fetch->values($path, $scopes)); |
| 96 | + } |
| 97 | + |
| 98 | + $xml = $this->configHandler->get(); |
| 99 | + $this->updateXml($xml, $values); |
| 100 | + $this->configHandler->set($xml); |
| 101 | + } |
| 102 | + |
| 103 | + protected function updateXml(\SimpleXMLElement $xml, array $values) |
| 104 | + { |
| 105 | + foreach ($values as $key => $subValue) { |
| 106 | + if (is_array($subValue)) { |
| 107 | + $childXml = $this->findXmlChild($xml, $key); |
| 108 | + $this->updateXml($childXml, $subValue); |
| 109 | + } else { |
| 110 | + $this->findXmlChild($xml, $key); |
| 111 | + $xml->{$key} = $subValue; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private function findXmlChild($xml, $key): \SimpleXMLElement |
| 117 | + { |
| 118 | + $children = $xml->children(); |
| 119 | + $childXml = null; |
| 120 | + foreach ($children as $child) { |
| 121 | + if ($child->getName() == $key) { |
| 122 | + $childXml = $child; |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + if (!$childXml) { |
| 127 | + $childXml = $xml->addChild($key, ''); |
| 128 | + } |
| 129 | + |
| 130 | + return $childXml; |
| 131 | + } |
| 132 | +} |
0 commit comments