ExtensionAbstract.php 2.0 KB

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