Config.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\admin\controller\oauth;
  3. use app\common\controller\Backend;
  4. class Config extends Backend
  5. {
  6. protected static $configFile = 'oauth.php';
  7. public function initialize(): void
  8. {
  9. parent::initialize();
  10. }
  11. public function index(): void
  12. {
  13. // 获取所有配置项
  14. $config = config('oauth');
  15. $this->success('', [
  16. 'config' => $config,
  17. ]);
  18. }
  19. public function saveConfig(): void
  20. {
  21. $name = $this->request->get('name', '');
  22. $data = $this->request->post();
  23. if (!$name) {
  24. $this->error(__('Parameter error'));
  25. }
  26. $config = config('oauth');
  27. $configPath = config_path() . self::$configFile;
  28. $configContent = @file_get_contents($configPath);
  29. if (!isset($config[$name]) || !is_array($config[$name])) {
  30. $this->error(__('Parameter error'));
  31. }
  32. foreach ($config[$name] as $key => $item) {
  33. $item = str_replace('/', '\/', $item);
  34. $configContent = preg_replace("/'$key'(\s+)=>(\s+)'$item',#$name#/", "'$key'\$1=>\$2'{$data[$key]}',#$name#", $configContent);
  35. }
  36. $result = @file_put_contents($configPath, $configContent);
  37. if (!$result) {
  38. $this->error(__('Configuration write failed: %s', ['config/' . self::$configFile]));
  39. }
  40. $this->success();
  41. }
  42. }