Extension.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\System;
  4. use SixShop\Core\ExtensionAbstract;
  5. use SixShop\Core\Service\AutoloadService;
  6. use SixShop\System\Enum\ExtensionStatusEnum;
  7. use SixShop\System\Hook\CronJobHook;
  8. use SixShop\System\Hook\DebugHook;
  9. use SixShop\System\Hook\ExtensionStatusHook;
  10. use SixShop\System\Hook\GatheringCrontabEventHook;
  11. use SixShop\System\Hook\QueueJobHook;
  12. use think\db\exception\PDOException;
  13. use function SixShop\Core\extension_name_list;
  14. class Extension extends ExtensionAbstract
  15. {
  16. public function __construct(private readonly AutoloadService $autoloadService)
  17. {
  18. }
  19. public function getHooks(): array
  20. {
  21. $hooks = [
  22. CronJobHook::class,
  23. ExtensionStatusHook::class,
  24. GatheringCrontabEventHook::class,
  25. QueueJobHook::class,
  26. ];
  27. // Debug 模式下添加 DebugHook
  28. if (app()->isDebug()) {
  29. $hooks[] = DebugHook::class;
  30. }
  31. return $hooks;
  32. }
  33. public function boot(): void
  34. {
  35. if ($this->isBooted) {
  36. return;
  37. }
  38. foreach (extension_name_list() as $extensionID) {
  39. /** @var ExtensionAbstract $extension */
  40. $extension = $this->autoloadService->getExtension($extensionID);
  41. $className = get_class($extension);
  42. if (method_exists($extension, 'available')
  43. || $className::hasMacro('available')
  44. ) {
  45. continue;
  46. }
  47. $className::macro('available', function () {
  48. $extensionID = $this->getInfo()['id'];
  49. try {
  50. $info = app()->make(ExtensionManager::class)->getInfo($extensionID);
  51. } catch (PDOException) {
  52. // 新装系统的时候system扩展还未安装
  53. return false;
  54. }
  55. return $info->status === ExtensionStatusEnum::ENABLED;
  56. });
  57. }
  58. parent::boot();
  59. }
  60. public function available(): true
  61. {
  62. return true;
  63. }
  64. protected function getBaseDir(): string
  65. {
  66. return dirname(__DIR__);
  67. }
  68. }