Skip to content

Commit 2b58c80

Browse files
committed
Merge branch '32-generate-config-keys' into 'master'
#32 Миграция конфигов See merge request components/laravel-entity-generator!74
2 parents 84245ea + d281f34 commit 2b58c80

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Facades\Config;
78
use RonasIT\Support\Events\SuccessCreateMessage;
89
use RonasIT\Support\Exceptions\ClassNotExistsException;
910
use RonasIT\Support\Exceptions\EntityCreateException;
@@ -163,6 +164,7 @@ public function __construct()
163164
public function handle()
164165
{
165166
$this->validateInput();
167+
$this->checkConfigs();
166168
$this->eventDispatcher->listen(SuccessCreateMessage::class, $this->getSuccessMessageCallback());
167169

168170
try {
@@ -172,6 +174,57 @@ public function handle()
172174
}
173175
}
174176

177+
protected function checkConfigs()
178+
{
179+
$packageConfigPath = __DIR__ . '/../../config/entity-generator.php';
180+
$packageConfigs = require $packageConfigPath;
181+
182+
$projectConfigs = config('entity-generator');
183+
184+
$newConfig = $this->outputNewConfig($packageConfigs, $projectConfigs);
185+
186+
if ($newConfig !== $projectConfigs) {
187+
$this->info('Config has been updated');
188+
Config::set('entity-generator', $newConfig);
189+
file_put_contents(config_path('entity-generator.php'), "<?php\n\nreturn" . $this->customVarExport($newConfig) . ';');
190+
}
191+
}
192+
193+
protected function outputNewConfig($packageConfigs, $projectConfigs)
194+
{
195+
$flattenedPackageConfigs = Arr::dot($packageConfigs);
196+
$flattenedProjectConfigs = Arr::dot($projectConfigs);
197+
198+
$newConfig = array_merge($flattenedPackageConfigs, $flattenedProjectConfigs);
199+
200+
$differences = array_diff_key($newConfig, $flattenedProjectConfigs);
201+
202+
foreach ($differences as $differenceKey => $differenceValue) {
203+
$this->info("Key '{$differenceKey}' was missing in your config, we added it with the value '{$differenceValue}'");
204+
}
205+
206+
return array_undot($newConfig);
207+
}
208+
209+
protected function customVarExport($expression)
210+
{
211+
$defaultExpression = var_export($expression, true);
212+
213+
$patterns = [
214+
'/array/' => '',
215+
'/\(/' => '[',
216+
'/\)/' => ']',
217+
'/=> \\n/' => '=>',
218+
'/=>.+\[/' => '=> [',
219+
'/^ {8}/m' => "\t\t\t\t",
220+
'/^ {6}/m' => "\t\t\t",
221+
'/^ {4}/m' => "\t\t",
222+
'/^ {2}/m' => "\t",
223+
];
224+
225+
return preg_replace(array_keys($patterns), array_values($patterns), $defaultExpression);
226+
}
227+
175228
protected function classExists($path, $name)
176229
{
177230
$paths = config('entity-generator.paths');

0 commit comments

Comments
 (0)