InitAddon.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace app\event;
  3. use app\model\system\Addon;
  4. use think\facade\Lang;
  5. use think\facade\Event;
  6. use think\facade\Cache;
  7. class InitAddon
  8. {
  9. public function handle()
  10. {
  11. if (defined('BIND_MODULE') && BIND_MODULE === 'install') {
  12. return;
  13. }
  14. $this->initEvent();
  15. }
  16. private function InitEvent()
  17. {
  18. $cache = Cache::get("addon_event_list");
  19. if (empty($cache)) {
  20. $addon_model = new Addon();
  21. $addon_data = $addon_model->getAddonList([], 'name');
  22. $listen_array = [];
  23. foreach ($addon_data['data'] as $k => $v) {
  24. $addon_event = (require_once 'addon/' . $v['name'] . '/config/event.php');
  25. $listen = isset($addon_event['listen']) ? $addon_event['listen'] : [];
  26. if (!empty($listen)) {
  27. $listen_array[] = $listen;
  28. }
  29. }
  30. Cache::tag("addon")->set("addon_event_list", $listen_array);
  31. } else {
  32. $listen_array = $cache;
  33. }
  34. if (!empty($listen_array)) {
  35. foreach ($listen_array as $k => $listen) {
  36. if (!empty($listen)) {
  37. Event::listenEvents($listen);
  38. }
  39. }
  40. }
  41. }
  42. }