path = $path = public_path('theme/'); $this->themes = array_map(function ($item) use ($path) { return str_replace($path, '', $item); }, glob($path . '*')); } public function getThemes() { $themeConfigs = []; foreach ($this->themes as $theme) { $themeConfigFile = $this->path . "{$theme}/config.php"; if (!File::exists($themeConfigFile)) continue; $themeConfig = include($themeConfigFile); if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue; $themeConfigs[$this->themes] = $themeConfig; } return response([ 'data' => $themeConfigs ]); } public function getThemeConfig(Request $request) { return response([ 'data' => config('theme.v2board') ]); } public function saveThemeConfig(Request $request) { $payload = $request->validate([ 'name' => 'required|in:' . join(',', $this->themes), 'configs' => 'required|array' ]); $themeConfigFile = public_path("theme/{$payload['name']}/config.php"); if (!File::exists($themeConfigFile)) abort(500, '主题不存在'); $themeConfig = include($themeConfigFile); $validateFields = array_column($themeConfig['configs'], 'field_name'); $config = []; foreach ($validateFields as $validateField) { if (!isset($payload['configs'][$validateField])) continue; $config[$validateField] = $payload['configs'][$validateField]; } File::ensureDirectoryExists(base_path() . '/config/theme/'); $data = var_export($config, 1); if (!File::put(base_path() . "/config/theme/{$payload['name']}.php", " config('theme.v2board') ]); } }