[--force]'; } public function run(): int { $path = trim($this->arg(0)); if ($path === '') { $this->fail('Укажите файл: php bin/proton settings:import var/export/settings.json'); return 1; } $full = str_starts_with($path, '/') || preg_match('/^[A-Za-z]:/', $path) === 1 ? $path : APP_ROOT . '/' . $path; if (!is_file($full)) { $this->fail('Файл не найден: ' . $path); return 1; } $data = json_decode((string) file_get_contents($full), true); if (!is_array($data)) { $this->fail('Не разобрал JSON: ' . $path); return 1; } /** @var array $settings */ $settings = (array) ($data['settings'] ?? []); /** @var array> $roles */ $roles = (array) ($data['roles'] ?? []); $this->line('В файле: настроек — ' . count($settings) . ', ролей — ' . count($roles)); if (!$this->confirm('Применить на этой базе?')) { $this->line('Отменено'); return 1; } $appliedSettings = 0; $unknownSettings = []; foreach ($settings as $key => $value) { $key = (string) $key; if (!Settings::known($key)) { $unknownSettings[] = $key; continue; } Settings::put($key, $value); $appliedSettings++; } $createdRoles = 0; $updatedRoles = 0; $skippedRoles = []; foreach ($roles as $row) { $name = trim((string) ($row['name'] ?? '')); if ($name === '') { continue; } $description = (string) ($row['description'] ?? ''); $permissions = Permission::filter((array) ($row['permissions'] ?? [])); /** @var Role|null $role */ $role = Role::query()->where('name', $name)->first(); if ($role === null) { Role::create([ 'name' => $name, 'description' => $description, 'permissions' => $permissions, 'is_system' => 0, ]); $createdRoles++; continue; } if ($role->isSystem()) { $skippedRoles[] = $name; continue; } $role->forceFill(['description' => $description, 'permissions' => $permissions])->save(); $updatedRoles++; } Audit::action( 'settings', 0, 'импорт из файла: настроек — ' . $appliedSettings . ', ролей заведено — ' . $createdRoles . ', обновлено — ' . $updatedRoles ); $this->ok('Настроек применено: ' . $appliedSettings . ', ролей заведено: ' . $createdRoles . ', обновлено: ' . $updatedRoles); if ($unknownSettings !== []) { $this->line(' Неизвестные настройки пропущены: ' . implode(', ', $unknownSettings)); } if ($skippedRoles !== []) { $this->line(' Встроенную роль не трогаем: ' . implode(', ', $skippedRoles)); } return 0; } }