Service.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. use Closure;
  14. use think\event\RouteLoaded;
  15. /**
  16. * 系统服务基础类
  17. * @method void register()
  18. * @method void boot()
  19. */
  20. abstract class Service
  21. {
  22. protected $app;
  23. public function __construct(App $app)
  24. {
  25. $this->app = $app;
  26. }
  27. /**
  28. * 加载路由
  29. * @access protected
  30. * @param string $path 路由路径
  31. */
  32. protected function loadRoutesFrom($path)
  33. {
  34. $this->registerRoutes(function () use ($path) {
  35. include $path;
  36. });
  37. }
  38. /**
  39. * 注册路由
  40. * @param Closure $closure
  41. */
  42. protected function registerRoutes(Closure $closure)
  43. {
  44. $this->app->event->listen(RouteLoaded::class, $closure);
  45. }
  46. /**
  47. * 添加指令
  48. * @access protected
  49. * @param array|string $commands 指令
  50. */
  51. protected function commands($commands)
  52. {
  53. $commands = is_array($commands) ? $commands : func_get_args();
  54. Console::starting(function (Console $console) use ($commands) {
  55. $console->addCommands($commands);
  56. });
  57. }
  58. }