|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TechDivision\Import\Cli\Command\ImportConfigDiffCommand |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7 |
|
6
|
|
|
* |
|
7
|
|
|
* @author [email protected] |
|
8
|
|
|
* @copyright 2023 TechDivision GmbH <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/MIT |
|
10
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
11
|
|
|
* @link http://www.techdivision.com |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace TechDivision\Import\Cli\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
use TechDivision\Import\Configuration\ConfigurationInterface; |
|
19
|
|
|
use TechDivision\Import\Utils\CommandNames; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The import command implementation. |
|
23
|
|
|
* |
|
24
|
|
|
* @author [email protected] |
|
25
|
|
|
* @copyright 2023 TechDivision GmbH <[email protected]> |
|
26
|
|
|
* @license https://opensource.org/licenses/MIT |
|
27
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
|
28
|
|
|
* @link http://www.techdivision.com |
|
29
|
|
|
*/ |
|
30
|
|
|
class ImportConfigDiffCommand extends AbstractSimpleImportCommand |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* file where default values are saved |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
public const DEFAULT_FILE = __DIR__ . '/../../config.json.default'; |
|
38
|
|
|
|
|
39
|
|
|
/** @var string */ |
|
40
|
|
|
public const ADD_KEY = 'added'; |
|
41
|
|
|
|
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
public const DELETE_KEY = 'deleted'; |
|
44
|
|
|
|
|
45
|
|
|
/** @var string */ |
|
46
|
|
|
public const CHANGED_KEY = 'changed'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Configures the current command. |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function configure(): void |
|
55
|
|
|
{ |
|
56
|
|
|
// initialize the command with the required/optional options |
|
57
|
|
|
$this->setName(CommandNames::IMPORT_CONFIG_DIFF) |
|
|
|
|
|
|
58
|
|
|
->setDescription('Shows Diffs between default configuration values and project config'); |
|
59
|
|
|
|
|
60
|
|
|
// invoke the parent method |
|
61
|
|
|
parent::configure(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Finally executes the simple command. |
|
66
|
|
|
* |
|
67
|
|
|
* @param ConfigurationInterface $configuration The configuration instance |
|
68
|
|
|
* @param InputInterface $input An InputInterface instance |
|
69
|
|
|
* @param OutputInterface $output An OutputInterface instance |
|
70
|
|
|
* |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function executeSimpleCommand( |
|
74
|
|
|
ConfigurationInterface $configuration, |
|
75
|
|
|
InputInterface $input, |
|
76
|
|
|
OutputInterface $output |
|
77
|
|
|
): int { |
|
78
|
|
|
$serializer = $this->createSerializer(); |
|
79
|
|
|
$projectValues = $serializer->serialize($configuration, 'json'); |
|
80
|
|
|
$defaultValues = file_get_contents(self::DEFAULT_FILE); |
|
81
|
|
|
|
|
82
|
|
|
// compare project and default Values |
|
83
|
|
|
if ($defaultValues !== $projectValues) { |
|
84
|
|
|
$projectJsonValues = json_decode($projectValues); |
|
85
|
|
|
$defaultValuesJson = json_decode($defaultValues); |
|
86
|
|
|
$paths = $this->getDataAsFlatArray($projectJsonValues); |
|
87
|
|
|
$defaultPaths = $this->getDataAsFlatArray($defaultValuesJson); |
|
88
|
|
|
$diff = $this->getAllDiffs($defaultPaths, $paths); |
|
89
|
|
|
$this->writeDiffs($defaultPaths, $paths, $diff, $output); |
|
90
|
|
|
return 0; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$output->writeln('[*] no changes found'); |
|
94
|
|
|
return 0; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param array $defaultPaths Default values as flat array |
|
99
|
|
|
* @param array $paths Project values as flat array |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getAllDiffs(array $defaultPaths, array $paths): array |
|
103
|
|
|
{ |
|
104
|
|
|
$addedKeys = $this->getAddedKeys($defaultPaths, $paths); |
|
105
|
|
|
$manipulatedKeys = array_diff_assoc($defaultPaths, $paths); |
|
106
|
|
|
$deletedKeys = $this->getDeletedKeys($defaultPaths, $paths); |
|
107
|
|
|
return [ |
|
108
|
|
|
self::ADD_KEY => $addedKeys, |
|
109
|
|
|
self::DELETE_KEY => $deletedKeys, |
|
110
|
|
|
self::CHANGED_KEY => $manipulatedKeys, |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $defaultConfig Default values as flat array |
|
116
|
|
|
* @param array $projectConfig Project values as flat array |
|
117
|
|
|
* @param array $diff Array with all diffs |
|
118
|
|
|
* @param OutputInterface $output OutputInterface instance |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
private function writeDiffs( |
|
122
|
|
|
array $defaultConfig, |
|
123
|
|
|
array $projectConfig, |
|
124
|
|
|
array $diff, |
|
125
|
|
|
OutputInterface $output |
|
126
|
|
|
): void { |
|
127
|
|
|
$output->writeln('Original | Override'); |
|
128
|
|
|
foreach ($diff[self::DELETE_KEY] as $deletedKey) { |
|
129
|
|
|
$output->writeln($deletedKey . ':' . |
|
130
|
|
|
'"' . $defaultConfig[$deletedKey] . '"' . ' | --- key was deleted ---'); |
|
131
|
|
|
} |
|
132
|
|
|
foreach ($diff[self::ADD_KEY] as $addedKey) { |
|
133
|
|
|
$output->writeln('--- key was added --- | ' . $addedKey . ':' . |
|
134
|
|
|
'"' . $projectConfig[$addedKey] . '"'); |
|
135
|
|
|
} |
|
136
|
|
|
foreach ($diff[self::CHANGED_KEY] as $key => $value) { |
|
137
|
|
|
$output->writeln($key. ':' . |
|
138
|
|
|
'"'.$defaultConfig[$key].'"' . ' | ' . $key . ':'. '"'.$projectConfig[$key].'"'); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param array $default Default values as flat array |
|
144
|
|
|
* @param array $project Project values as flat array |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
private function getDeletedKeys(array $default, array $project): array |
|
148
|
|
|
{ |
|
149
|
|
|
$deletedKeys = []; |
|
150
|
|
|
foreach ($default as $key => $value) { |
|
151
|
|
|
if (!array_key_exists($key, $project)) { |
|
152
|
|
|
$deletedKeys[] = $key; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
return $deletedKeys; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param array $default Default values as flat array |
|
160
|
|
|
* @param array $project Project values as flat array |
|
161
|
|
|
* @return array |
|
162
|
|
|
*/ |
|
163
|
|
|
private function getAddedKeys(array $default, array $project): array |
|
164
|
|
|
{ |
|
165
|
|
|
$addedKeys = []; |
|
166
|
|
|
foreach ($project as $key => $value) { |
|
167
|
|
|
if (!array_key_exists($key, $default)) { |
|
168
|
|
|
$addedKeys[] = $key; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
return $addedKeys; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param mixd $data Data to be converted to flat array |
|
|
|
|
|
|
176
|
|
|
* @param string $key Key of the data |
|
177
|
|
|
* @param string $path Path of the data |
|
178
|
|
|
* @param array $arr Array to be filled with data |
|
179
|
|
|
* @return array |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getDataAsFlatArray($data, string $key = '', string $path = '', array $arr = []): array |
|
182
|
|
|
{ |
|
183
|
|
|
if ($path != '') { |
|
184
|
|
|
$path = $path . '/'; |
|
185
|
|
|
} |
|
186
|
|
|
$path = $path . $key; |
|
187
|
|
|
if (is_object($data) || is_array($data)) { |
|
188
|
|
|
foreach ($data as $key => $value) { |
|
189
|
|
|
$arr = $this->getDataAsFlatArray($value, $key, $path, $arr); |
|
190
|
|
|
} |
|
191
|
|
|
} else { |
|
192
|
|
|
$arr[$path] = $data; |
|
193
|
|
|
} |
|
194
|
|
|
return $arr; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|