|
2 | 2 |
|
3 | 3 | namespace Hackathon\EAVCleaner\Console\Command;
|
4 | 4 |
|
| 5 | +use Magento\Framework\App\Config\Initial\Reader; |
5 | 6 | use Magento\Framework\App\ResourceConnection;
|
6 | 7 | use Symfony\Component\Console\Command\Command;
|
7 | 8 | use Symfony\Component\Console\Input\InputInterface;
|
|
10 | 11 |
|
11 | 12 | class RestoreUseDefaultConfigValueCommand extends Command
|
12 | 13 | {
|
| 14 | + /** @var Reader */ |
| 15 | + private $configReader; |
| 16 | + |
13 | 17 | /**
|
14 | 18 | * @var ResourceConnection
|
15 | 19 | */
|
16 | 20 | private $resourceConnection;
|
17 | 21 |
|
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 | + ) { |
20 | 30 | parent::__construct($name);
|
| 31 | + |
| 32 | + $this->configReader = $configReader; |
21 | 33 | $this->resourceConnection = $resourceConnection;
|
22 | 34 | }
|
23 | 35 |
|
@@ -63,7 +75,50 @@ public function execute(InputInterface $input, OutputInterface $output)
|
63 | 75 | }
|
64 | 76 | $removedConfigValues += ($count - 1);
|
65 | 77 | }
|
| 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 | + } |
66 | 98 | }
|
| 99 | + |
67 | 100 | $output->writeln('Removed ' . $removedConfigValues . ' values from core_config_data table.');
|
68 | 101 | }
|
| 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 | + } |
69 | 124 | }
|
0 commit comments