CoreService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Core\Service;
  4. use Composer\InstalledVersions;
  5. use Composer\Json\JsonFile;
  6. use SixShop\Core\Exception\ExceptionHandle;
  7. use SixShop\Core\Helper;
  8. use SixShop\Core\Plugin;
  9. use SixShop\Core\Request;
  10. use think\event\HttpRun;
  11. use think\exception\Handle;
  12. use think\facade\Event;
  13. use think\Service;
  14. class CoreService extends Service
  15. {
  16. public static string $extensionPath;
  17. /**
  18. * @var array<string, array{name:string}>
  19. */
  20. public static array $extensionComposerMap = [];
  21. /* @deprecated */
  22. public static array $extensionNameList = [];
  23. private static array $normalExtensionNameList = [];
  24. public function register(): void
  25. {
  26. $this->app->bind(Handle::class, ExceptionHandle::class);
  27. $this->app->bind('think\Request', Request::class);
  28. $this->app->bind('classLoader',
  29. require $this->app->getRootPath() . 'vendor/autoload.php');
  30. self::$extensionPath = $this->app->getRootPath() . 'extension' . DIRECTORY_SEPARATOR;
  31. $this->initExtensionList();
  32. $this->compatibleExtensionNameList();
  33. }
  34. public function boot(): void
  35. {
  36. $this->app->make(AutoloadService::class)->load(self::$extensionComposerMap,self::$normalExtensionNameList);
  37. $this->app->make(HookAttributeService::class)->init();
  38. $this->app->event->trigger('hook_init', $this->app);
  39. $this->app->event->listen(HttpRun::class, function () {
  40. $this->registerRoutes($this->app->make(RegisterRouteService::class)->init($this->app));
  41. });
  42. $this->app->make(CommandService::class)->init(function ($commands) {
  43. $this->commands($commands);
  44. });
  45. }
  46. private function initExtensionList(): void
  47. {
  48. if (!empty(self::$extensionComposerMap)) {
  49. return;
  50. }
  51. $reference = Plugin::getInstalledSixShopExtensions()['root']['reference'];
  52. $extensionComposerFile = $this->app->getRootPath() . 'runtime/extension_' .$reference.'.php';
  53. if (file_exists($extensionComposerFile)) {
  54. self::$extensionComposerMap = require $extensionComposerFile;
  55. return;
  56. }
  57. foreach (InstalledVersions::getInstalledPackagesByType(Plugin::EXTENSION_TYPE) as $item) {
  58. //$version = InstalledVersions::getInstallPath($item);
  59. $installPath = InstalledVersions::getInstallPath($item);
  60. $composerJson = new JsonFile($installPath . '/composer.json');
  61. $composer = $composerJson->read();
  62. $extensionID = $composer['extra']['sixshop']['id'];
  63. self::$extensionComposerMap[$extensionID] = $composer;
  64. };
  65. $header = '// This file is automatically generated at:' . date('Y-m-d H:i:s') . PHP_EOL . 'declare (strict_types = 1);' . PHP_EOL;
  66. $content = '<?php ' . PHP_EOL . $header . "return " . var_export(self::$extensionComposerMap, true) . ';';
  67. file_put_contents($extensionComposerFile, $content);
  68. }
  69. /* @deprecated */
  70. private function compatibleExtensionNameList(): void
  71. {
  72. if (empty(self::$extensionNameList)) {
  73. self::$extensionNameList = array_keys(self::$extensionComposerMap);
  74. $normalFile = $this->app->getRootPath() . 'runtime/module_name_list_normal.php';
  75. if (file_exists($normalFile)) {
  76. $normalExtensionList = require $normalFile;
  77. foreach ($normalExtensionList as $item) {
  78. if (array_key_exists($item, self::$extensionComposerMap)) {
  79. continue;
  80. }
  81. if (is_dir( Helper::extension_path($item).'src')) {
  82. self::$extensionNameList[] = $item;
  83. self::$normalExtensionNameList[] = $item;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }