Dispatcher.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Events;
  3. use ModulesGarden\ProxmoxAddon\App\Events\MyTestEvent;
  4. use ModulesGarden\ProxmoxAddon\Core\DependencyInjection;
  5. use ModulesGarden\ProxmoxAddon\Core\DependencyInjection\Container;
  6. use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  7. use ModulesGarden\ProxmoxAddon\Core\Queue\DatabaseQueue;
  8. use ModulesGarden\ProxmoxAddon\Core\Helper\WhmcsVersionComparator;
  9. use Illuminate\Contracts\Broadcasting\ShouldBroadcast as ShouldBroadcast;
  10. class Dispatcher extends \Illuminate\Events\Dispatcher
  11. {
  12. public function __construct(Container $container)
  13. {
  14. $this->container = $container;
  15. $this->initialize();
  16. }
  17. /**
  18. * Fire an event and call the listeners.
  19. *
  20. * @param string|object $event
  21. * @param mixed $payload
  22. * @param bool $halt
  23. * @return array|null
  24. */
  25. public function fire($event, $payload = [], $halt = false)
  26. {
  27. /* This function executes a different code, depending on the version of the container - WHMCS 8 has a much newer version */
  28. $version8OrHigher = (new WhmcsVersionComparator)->isWVersionHigherOrEqual('8.0.0');
  29. if($version8OrHigher)
  30. {
  31. return $this->fireForWhmcs8OrNewer($event, $payload, $halt);
  32. }
  33. /* ----------------------------- OLDER WHMCS -------------------------------------------- */
  34. if (is_object($event)) {
  35. list($payload, $event) = [[$event], get_class($event)];
  36. }
  37. $responses = [];
  38. if (! is_array($payload)) {
  39. $payload = [$payload];
  40. }
  41. $this->firing[] = $event;
  42. if (isset($payload[0]) && $payload[0] instanceof ShouldBroadcast) {
  43. $this->broadcastEvent($payload[0]);
  44. }
  45. foreach ($this->getListeners($event) as $listener) {
  46. $response = call_user_func_array($listener, $payload);
  47. if (! is_null($response) && $halt) {
  48. array_pop($this->firing);
  49. return $response;
  50. }
  51. if ($response === false) {
  52. break;
  53. }
  54. $responses[] = $response;
  55. }
  56. array_pop($this->firing);
  57. return $halt ? null : $responses;
  58. }
  59. private function fireForWhmcs8OrNewer($event, $payload, $halt)
  60. {
  61. [$event, $payload] = $this->parseEventAndPayload(
  62. $event, $payload
  63. );
  64. if ($this->shouldBroadcast($payload)) {
  65. $this->broadcastEvent($payload[0]);
  66. }
  67. $responses = [];
  68. foreach ($this->getListeners($event) as $listener) {
  69. $response = $listener($event, $payload);
  70. if ($halt && ! is_null($response)) {
  71. return $response;
  72. }
  73. if ($response === false) {
  74. break;
  75. }
  76. $responses[] = $response;
  77. }
  78. return $halt ? null : $responses;
  79. }
  80. /**
  81. *
  82. */
  83. protected function initialize()
  84. {
  85. /**
  86. * Load available events
  87. */
  88. $path = ModuleConstants::getFullPath('app', 'Config', 'events.php');
  89. $config = include($path);
  90. foreach ($config as $event => $listeners)
  91. {
  92. foreach ($listeners as $listener)
  93. {
  94. $this->listen($event, $listener);
  95. }
  96. }
  97. /**
  98. * Set queue resolver
  99. */
  100. $this->setQueueResolver(function()
  101. {
  102. return DependencyInjection::create(DatabaseQueue::class);
  103. });
  104. }
  105. /**
  106. * @param $class
  107. * @param $arguments
  108. */
  109. public function queue($class, $arguments, $parentId = null, $relType = null, $relId = null, $customId = null)
  110. {
  111. $class = implode('@', $this->parseClassCallable($class));
  112. return $this->resolveQueue()->push("$class", serialize($arguments), $parentId, $relType, $relId, $customId);
  113. }
  114. /**
  115. * @param $class
  116. * @param $method
  117. * @override
  118. * @return \Closure
  119. */
  120. protected function createQueuedHandlerCallable($class, $method)
  121. {
  122. return function () use ($class, $method)
  123. {
  124. $arguments = $this->cloneArgumentsForQueueing(func_get_args());
  125. if (method_exists($class, 'queue'))
  126. {
  127. $this->callQueueMethodOnHandler($class, $method, $arguments);
  128. }
  129. else
  130. {
  131. $this->resolveQueue()->push("{$class}@{$method}", serialize($arguments));
  132. }
  133. };
  134. }
  135. }