ExtensionAbstract.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Core;
  4. use Composer\Json\JsonFile;
  5. use Exception;
  6. use SixShop\Core\Contracts\ExtensionInterface;
  7. use think\helper\Macroable;
  8. /**
  9. * @method bool available() 扩展是否可用
  10. */
  11. abstract class ExtensionAbstract implements ExtensionInterface
  12. {
  13. use Macroable;
  14. protected array $info;
  15. protected bool $isBooted = false;
  16. /**
  17. * @throws Exception
  18. */
  19. public function getInfo(): array
  20. {
  21. if (empty($this->info)) {
  22. if (file_exists($this->getBaseDir() . '/info.php')) {
  23. $this->info = require $this->getBaseDir() . '/info.php';
  24. } else {
  25. $localConfig = $this->getBaseDir() . '/composer.json';
  26. $file = new JsonFile($localConfig);
  27. $localConfig = $file->read();
  28. $this->info = [
  29. 'id' => static::EXTENSION_ID,
  30. 'name' => $localConfig['name'],
  31. 'description' => $localConfig['description'],
  32. ];
  33. }
  34. }
  35. return $this->info;
  36. }
  37. abstract protected function getBaseDir(): string;
  38. public function getConfig(): array
  39. {
  40. if (!file_exists($this->getBaseDir() . '/config.php')) {
  41. return [];
  42. }
  43. return require $this->getBaseDir() . '/config.php';
  44. }
  45. public function install(): void
  46. {
  47. }
  48. public function uninstall(): void
  49. {
  50. }
  51. public function getCommands(): array
  52. {
  53. if (!file_exists($this->getBaseDir() . '/command.php')) {
  54. return [];
  55. }
  56. return require $this->getBaseDir() . '/command.php';
  57. }
  58. public function getHooks(): array
  59. {
  60. return [];
  61. }
  62. /**
  63. * 获取路由
  64. * @return array<string, string>
  65. */
  66. public function getRoutes(): array
  67. {
  68. $adminRoute = $this->getBaseDir() . '/route/admin.php';
  69. $apiRoute = $this->getBaseDir() . '/route/api.php';
  70. $routes = [];
  71. if (file_exists($adminRoute)) {
  72. $routes['admin'] = $adminRoute;
  73. }
  74. if (file_exists($apiRoute)) {
  75. $routes['api'] = $apiRoute;
  76. }
  77. return $routes;
  78. }
  79. public function getCronJobs(): array
  80. {
  81. return [];
  82. }
  83. public function boot(): void
  84. {
  85. $this->isBooted = true;
  86. }
  87. }