CoreService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Core\Service;
  4. use SixShop\Core\Exception\ExceptionHandle;
  5. use SixShop\Core\Request;
  6. use think\event\HttpRun;
  7. use think\exception\Handle;
  8. use think\Service;
  9. class CoreService extends Service
  10. {
  11. public static string $extensionPath;
  12. public static array $extensionNameList = [];
  13. public function register(): void
  14. {
  15. $this->app->bind(Handle::class, ExceptionHandle::class);
  16. $this->app->bind('think\Request', Request::class);
  17. $this->app->bind('classLoader',
  18. require $this->app->getRootPath() . 'vendor/autoload.php');
  19. self::$extensionPath = $this->app->getRootPath() . 'extension' . DIRECTORY_SEPARATOR;
  20. $this->initExtensionList();
  21. }
  22. public function boot(): void
  23. {
  24. $this->app->make(AutoloadService::class)->init($this->app);
  25. $this->app->make(HookAttributeService::class)->init($this->app);
  26. $this->app->event->listen(HttpRun::class,function (){
  27. $this->registerRoutes($this->app->make(RegisterRouteService::class)->init($this->app));
  28. });
  29. $this->app->make(CommandService::class)->init($this->app, function ($commands) {
  30. $this->commands($commands);
  31. });
  32. }
  33. public function initExtensionList(): void
  34. {
  35. if (empty(self::$extensionNameList)) {
  36. $coreFile = $this->app->getRootPath() . 'runtime/module_name_list_core.php';
  37. if (file_exists($coreFile)) {
  38. self::$extensionNameList = require $coreFile;
  39. } else {
  40. $extensionInfoList = [];
  41. $extensionDirs = array_diff(scandir(self::extensionPath), ['.', '..']);
  42. foreach ($extensionDirs as $item) {
  43. if (!is_dir(self::extensionPath . $item)) {
  44. continue;
  45. }
  46. $infoFile = self::extensionPath . $item . '/info.php';
  47. if (is_file($infoFile)) {
  48. $info = require $infoFile;
  49. $info['weight'] = $info['weight'] ?? 10000;
  50. if ($info['is_core'] ?? false) {
  51. self::$extensionNameList[] = $info['id'];
  52. $extensionInfoList[] = $info;
  53. }
  54. }
  55. }
  56. usort($extensionInfoList, function ($a, $b) {
  57. return $a['weight'] <=> $b['weight'];
  58. });
  59. $header = '// This file is automatically generated at:' . date('Y-m-d H:i:s') . PHP_EOL . 'declare (strict_types = 1);' . PHP_EOL;
  60. $content = '<?php ' . PHP_EOL . $header . "return " . var_export(array_column($extensionInfoList, 'id'), true) . ';';
  61. file_put_contents($coreFile, $content);
  62. }
  63. $normalFile = $this->app->getRootPath() . 'runtime/module_name_list_normal.php';
  64. if (file_exists($normalFile)) {
  65. self::$extensionNameList = array_merge(self::$extensionNameList, require $normalFile);
  66. }
  67. }
  68. }
  69. public function loadEnv(string $envName = ''): void
  70. {
  71. $home = getenv('HOME');
  72. $envFile = $envName ? $home . '/.env.' . $envName : $home . '/.env';
  73. if (is_file($envFile)) {
  74. $this->app->env->load($envFile);
  75. return;
  76. }
  77. $this->app->loadEnv($envName);
  78. }
  79. }