Extension.php 1.9 KB

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