Skip to content

Commit 8b4286d

Browse files
author
Gregor Pollak
committed
Initial code deployment
0 parents  commit 8b4286d

File tree

9 files changed

+453
-0
lines changed

9 files changed

+453
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
.idea/
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

Handler/Config.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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\Handler;
19+
20+
use Magento\Framework\App\Filesystem\DirectoryList;
21+
22+
/**
23+
* Reads the existing file content from var/config/config.xml and parses it into an XML structure. If file doesn't
24+
* exist, it creates an empty template.
25+
*/
26+
class Config
27+
{
28+
private const CONFIG_PATH = '/config/config.xml';
29+
30+
private \Magento\Framework\Filesystem $filesystem;
31+
32+
public function __construct(
33+
\Magento\Framework\Filesystem $filesystem
34+
) {
35+
$this->filesystem = $filesystem;
36+
}
37+
38+
public function get(): \SimpleXMLElement
39+
{
40+
$varFolder = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
41+
42+
$content = '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
43+
. ' xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"></config>';
44+
if ($varFolder->isExist(self::CONFIG_PATH)) {
45+
$content = $varFolder->readFile(self::CONFIG_PATH);
46+
}
47+
48+
return new \SimpleXmlElement($content);
49+
}
50+
51+
/**
52+
* @throws \Magento\Framework\Exception\FileSystemException
53+
*/
54+
public function set(\SimpleXMLElement $xml): void
55+
{
56+
$varFolder = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
57+
58+
$content = $xml->asXML();
59+
$content = $this->getCleanContent($content);
60+
61+
$varFolder->writeFile(self::CONFIG_PATH, $content);
62+
}
63+
64+
private function getCleanContent($content): string
65+
{
66+
$dom = new \DOMDocument('1.0');
67+
$dom->preserveWhiteSpace = false;
68+
$dom->formatOutput = true;
69+
$dom->loadXML($content);
70+
$content = $dom->saveXML();
71+
$content = explode("\n", $content);
72+
$contentSize = count($content);
73+
for ($i = 0; $i < $contentSize; $i++) {
74+
$pos = strpos($content[$i], '<');
75+
if ($pos === false) {
76+
continue;
77+
}
78+
$spaces = substr($content[$i], 0, $pos);
79+
$content[$i] = str_replace($spaces, str_replace(' ', ' ', $spaces), $content[$i]);
80+
}
81+
82+
return implode("\n", $content);
83+
}
84+
}

Provider/Fetch.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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\Provider;
19+
20+
use Magento\Framework\Exception\InvalidArgumentException;
21+
22+
class Fetch
23+
{
24+
private array $configCache = [];
25+
26+
private \Magento\Framework\App\Config $config;
27+
28+
public function __construct(
29+
\Magento\Framework\App\Config $config
30+
) {
31+
$this->config = $config;
32+
}
33+
34+
/**
35+
* @throws InvalidArgumentException
36+
*/
37+
public function values(string $path, array $scopes): array
38+
{
39+
$levels = $this->getPathLevels($path);
40+
41+
if (!$this->configCache) {
42+
$this->configCache = $this->config->get('system');
43+
}
44+
45+
$values = array_fill_keys($scopes, []);
46+
$defaultScopeValues = $this->getScopeValues($this->configCache['default'], $levels);
47+
48+
foreach ($scopes as $scope) {
49+
if (!isset($this->configCache[$scope])) {
50+
throw new InvalidArgumentException(__('Invalid scope key: ' . $scope));
51+
}
52+
if ($scope === 'default') {
53+
$values[$scope] = $defaultScopeValues;
54+
continue;
55+
}
56+
57+
foreach ($this->configCache[$scope] as $scopeKey => $scopeData) {
58+
if (is_numeric($scopeKey) || in_array($scopeKey, ['admin', 'default'])) {
59+
continue;
60+
}
61+
62+
$scopeValues = $this->getScopeValues($scopeData, $levels);
63+
$scopeValues = $this->cleanScopeValues($scopeValues, $defaultScopeValues);
64+
65+
if (count($scopeValues) > 0) {
66+
$values[$scope][$scopeKey] = $scopeValues;
67+
}
68+
}
69+
}
70+
71+
foreach ($values as $scope => $scopeData) {
72+
if (count($scopeData) == 0) {
73+
unset($values[$scope]);
74+
}
75+
}
76+
77+
return $values;
78+
}
79+
80+
protected function cleanScopeValues(array $scopeValues, array $defaultValues): array
81+
{
82+
foreach ($scopeValues as $level1 => $l1scopeData) {
83+
foreach ($l1scopeData as $level2 => $l2scopeData) {
84+
foreach ($l2scopeData as $level3 => $value) {
85+
$defaultValue = $defaultValues[$level1][$level2][$level3] ?? null;
86+
if ($value === $defaultValue) {
87+
unset($scopeValues[$level1][$level2][$level3]);
88+
}
89+
}
90+
91+
if (count($scopeValues[$level1][$level2]) === 0) {
92+
unset($scopeValues[$level1][$level2]);
93+
}
94+
}
95+
96+
if (count($scopeValues[$level1]) === 0) {
97+
unset($scopeValues[$level1]);
98+
}
99+
}
100+
101+
return $scopeValues;
102+
}
103+
104+
protected function getScopeValues(array $scopeData, array $levels): array
105+
{
106+
[$level1, $level2, $level3] = $levels;
107+
108+
if (!isset($scopeData[$level1])) {
109+
return [];
110+
}
111+
112+
$scopeData = $scopeData[$level1];
113+
if ($level2 === '*' && $level3 === '*') {
114+
return [$level1 => $scopeData];
115+
}
116+
117+
if ($level2 !== '*' && $level3 === '*') {
118+
return [$level1 => [$level2 => $scopeData[$level2]]];
119+
}
120+
121+
if ($level2 === '*' && $level3 !== '*') {
122+
$values = [$level1 => []];
123+
foreach ($scopeData as $level2 => $l3scopeData) {
124+
foreach ($l3scopeData as $key => $value) {
125+
if ($key === $level3) {
126+
$values[$level1][$level2] = $values[$level1][$level2] ?? [];
127+
$values[$level1][$level2][$level3] = $value;
128+
}
129+
}
130+
}
131+
return $values;
132+
}
133+
134+
if (isset($scopeData[$level2]) && isset($scopeData[$level2][$level3])) {
135+
return [$level1 => [$level2 => [$level3 => $scopeData[$level2][$level3]]]];
136+
}
137+
138+
return [];
139+
}
140+
141+
/**
142+
* @throws InvalidArgumentException
143+
*/
144+
protected function getPathLevels(string $path): array
145+
{
146+
$levels = explode('/', $path);
147+
$levels = array_map(function ($level) {
148+
$level = trim($level);
149+
if ($level === '') {
150+
$level = '*';
151+
}
152+
return $level;
153+
}, $levels);
154+
155+
if ($levels[0] === '*') {
156+
throw new InvalidArgumentException(__("Path can't start with asterisk!"));
157+
}
158+
159+
return $levels;
160+
}
161+
}

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# RocketWeb Config (Export)
2+
3+
This module is a simple tool to export specific sections of configuration into config.xml file to make it VCS
4+
transferable without locking them by using app/etc/config.php or app/etc/env.php
5+
6+
## Usage
7+
Running the command will generate a file config.xml inside of var/config/ folder. If file doesn't exist, it will
8+
create it, otherwise it will append/modify values to the XML structure.
9+
10+
11+
12+
> bin/magento config:data:export _scopes_ _paths_

0 commit comments

Comments
 (0)