PreInstall.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\libs;
  3. use Illuminate\Support\Env;
  4. use Illuminate\Support\Facades\Config;
  5. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  6. /**
  7. * @desc 预设文件加载
  8. */
  9. class PreInstall
  10. {
  11. private static $data = [];
  12. /**
  13. * @desc 获得配置
  14. *
  15. * @param string $type 扩展名称
  16. *
  17. * @return array|mixed
  18. */
  19. public static function config(string $type)
  20. {
  21. $id = Env::get($type . '.id', Config::get('lc.' . $type . '.id'));
  22. if (!$id) {
  23. return [];
  24. }
  25. return self::get($type, $id);
  26. }
  27. /**
  28. * @desc 获取指定配置
  29. *
  30. * @param string $type 扩展名称
  31. * @param string|null $id 指定id
  32. *
  33. * @return array|mixed
  34. */
  35. public static function get(string $type, string $id = null)
  36. {
  37. if (!isset($data[$type])) {
  38. self::load($type);
  39. }
  40. if (!isset(self::$data[$type])) {
  41. return [];
  42. }
  43. $data = self::$data[$type];
  44. if ($id) {
  45. return $data[$id] ?? [];
  46. }
  47. return $data;
  48. }
  49. private static function load($type): void
  50. {
  51. $file = __DIR__ . '/preinstall/' . $type . '.php';
  52. if (!file_exists($file)) {
  53. throw new FileException('SWDZ预设文件不存在:' . $file);
  54. }
  55. self::$data[$type] = include $file;
  56. }
  57. }