Skip to content

Commit f7c3d75

Browse files
authored
Merge pull request #9 from fredden/use-system-value
Remove config values matching config.xml
2 parents 3176373 + f1a53e7 commit f7c3d75

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

Console/Command/RestoreUseDefaultConfigValueCommand.php

+57-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hackathon\EAVCleaner\Console\Command;
44

5+
use Magento\Framework\App\Config\Initial\Reader;
56
use Magento\Framework\App\ResourceConnection;
67
use Symfony\Component\Console\Command\Command;
78
use Symfony\Component\Console\Input\InputInterface;
@@ -10,14 +11,25 @@
1011

1112
class RestoreUseDefaultConfigValueCommand extends Command
1213
{
14+
/** @var Reader */
15+
private $configReader;
16+
1317
/**
1418
* @var ResourceConnection
1519
*/
1620
private $resourceConnection;
1721

18-
public function __construct(ResourceConnection $resourceConnection, string $name = null)
19-
{
22+
/** @var array */
23+
private $systemConfig;
24+
25+
public function __construct(
26+
Reader $configReader,
27+
ResourceConnection $resourceConnection,
28+
string $name = null
29+
) {
2030
parent::__construct($name);
31+
32+
$this->configReader = $configReader;
2133
$this->resourceConnection = $resourceConnection;
2234
}
2335

@@ -63,7 +75,50 @@ public function execute(InputInterface $input, OutputInterface $output)
6375
}
6476
$removedConfigValues += ($count - 1);
6577
}
78+
79+
if ($config['value'] === $this->getSystemValue($config['path'])) {
80+
$output->writeln("Config path {$config['path']} with value {$config['value']} matches system default; deleting value");
81+
82+
if (!$isDryRun) {
83+
if ($config['value'] === null) {
84+
$db->query(
85+
"DELETE FROM $tableName WHERE path = ? AND value IS NULL AND scope_id = 0",
86+
[$config['path']]
87+
);
88+
} else {
89+
$db->query(
90+
"DELETE FROM $tableName WHERE path = ? AND BINARY value = ? AND scope_id = 0",
91+
[$config['path'], $config['value']]
92+
);
93+
}
94+
}
95+
96+
$removedConfigValues++;
97+
}
6698
}
99+
67100
$output->writeln('Removed ' . $removedConfigValues . ' values from core_config_data table.');
68101
}
102+
103+
/**
104+
* Retrieve the system value for a given configuration path
105+
*
106+
* @param string $path
107+
* @return mixed
108+
*/
109+
private function getSystemValue(string $path)
110+
{
111+
if (!isset($this->systemConfig)) {
112+
$this->systemConfig = $this->configReader->read()['data']['default'];
113+
}
114+
115+
$pathParts = explode('/', $path);
116+
$value = $this->systemConfig;
117+
118+
while (!empty($pathParts)) {
119+
$value = $value[array_shift($pathParts)] ?? null;
120+
}
121+
122+
return $value;
123+
}
69124
}

0 commit comments

Comments
 (0)