InteractWithHandlers.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Libs\TikTok\Kernel\Traits;
  4. use function array_reverse;
  5. use function array_unshift;
  6. use function call_user_func;
  7. use Closure;
  8. use App\Libs\TikTok\Kernel\Exceptions\InvalidArgumentException;
  9. use function func_get_args;
  10. use function gettype;
  11. use function is_array;
  12. use function is_callable;
  13. use function is_string;
  14. use function method_exists;
  15. use function spl_object_hash;
  16. trait InteractWithHandlers
  17. {
  18. /**
  19. * @var array<int, array{hash: string, handler: callable}>
  20. */
  21. protected array $handlers = [];
  22. /**
  23. * @return array<int, array{hash: string, handler: callable}>
  24. */
  25. public function getHandlers(): array
  26. {
  27. return $this->handlers;
  28. }
  29. /**
  30. * @param callable|string $handler
  31. * @return InteractWithHandlers
  32. * @throws InvalidArgumentException
  33. */
  34. public function with(callable|string $handler): static
  35. {
  36. return $this->withHandler($handler);
  37. }
  38. /**
  39. * @param callable|string $handler
  40. * @return InteractWithHandlers
  41. * @throws InvalidArgumentException
  42. */
  43. public function withHandler(callable|string $handler): static
  44. {
  45. $this->handlers[] = $this->createHandlerItem($handler);
  46. return $this;
  47. }
  48. /**
  49. * @param callable|string $handler
  50. * @return array{hash: string, handler: callable}
  51. *
  52. * @throws InvalidArgumentException
  53. */
  54. public function createHandlerItem(callable|string $handler): array
  55. {
  56. return [
  57. 'hash' => $this->getHandlerHash($handler),
  58. 'handler' => $this->makeClosure($handler),
  59. ];
  60. }
  61. /**
  62. * @param callable|string $handler
  63. * @return string
  64. * @throws InvalidArgumentException
  65. */
  66. protected function getHandlerHash(callable|string $handler): string
  67. {
  68. switch (true) {
  69. case is_string($handler):
  70. return $handler;
  71. case is_array($handler):
  72. return is_string($handler[0]) ? $handler[0].'::'.$handler[1] : get_class($handler[0]).$handler[1];
  73. case $handler instanceof Closure:
  74. return spl_object_hash($handler);
  75. default:
  76. throw new InvalidArgumentException('Invalid handler: '.gettype($handler));
  77. }
  78. }
  79. /**
  80. * @param callable|string $handler
  81. * @return callable
  82. * @throws InvalidArgumentException
  83. */
  84. protected function makeClosure(callable|string $handler): callable
  85. {
  86. if (is_callable($handler)) {
  87. return $handler;
  88. }
  89. if (class_exists($handler) && method_exists($handler, '__invoke')) {
  90. /**
  91. * @psalm-suppress InvalidFunctionCall
  92. * @phpstan-ignore-next-line https://github.com/phpstan/phpstan/issues/5867
  93. */
  94. return fn () => (new $handler())(...func_get_args());
  95. }
  96. throw new InvalidArgumentException(sprintf('Invalid handler: %s.', $handler));
  97. }
  98. /**
  99. * @param callable|string $handler
  100. * @return InteractWithHandlers
  101. * @throws InvalidArgumentException
  102. */
  103. public function prepend(callable|string $handler): static
  104. {
  105. return $this->prependHandler($handler);
  106. }
  107. /**
  108. * @param callable|string $handler
  109. * @return InteractWithHandlers
  110. * @throws InvalidArgumentException
  111. */
  112. public function prependHandler(callable|string $handler): static
  113. {
  114. array_unshift($this->handlers, $this->createHandlerItem($handler));
  115. return $this;
  116. }
  117. /**
  118. * @param callable|string $handler
  119. * @return InteractWithHandlers
  120. * @throws InvalidArgumentException
  121. */
  122. public function without(callable|string $handler): static
  123. {
  124. return $this->withoutHandler($handler);
  125. }
  126. /**
  127. * @param callable|string $handler
  128. * @return InteractWithHandlers
  129. * @throws InvalidArgumentException
  130. */
  131. public function withoutHandler(callable|string $handler): static
  132. {
  133. $index = $this->indexOf($handler);
  134. if ($index > -1) {
  135. unset($this->handlers[$index]);
  136. }
  137. return $this;
  138. }
  139. /**
  140. * @param callable|string $handler
  141. * @return int
  142. * @throws InvalidArgumentException
  143. */
  144. public function indexOf(callable|string $handler): int
  145. {
  146. foreach ($this->handlers as $index => $item) {
  147. if ($item['hash'] === $this->getHandlerHash($handler)) {
  148. return $index;
  149. }
  150. }
  151. return -1;
  152. }
  153. /**
  154. * @param $value
  155. * @param callable|string $handler
  156. * @return InteractWithHandlers
  157. * @throws InvalidArgumentException
  158. */
  159. public function when($value, callable|string $handler): static
  160. {
  161. if (is_callable($value)) {
  162. $value = call_user_func($value, $this);
  163. }
  164. if ($value) {
  165. return $this->withHandler($handler);
  166. }
  167. return $this;
  168. }
  169. public function handle($result, $payload = null)
  170. {
  171. $next = $result = is_callable($result) ? $result : fn ($p) => $result;
  172. foreach (array_reverse($this->handlers) as $item) {
  173. $next = fn ($p) => $item['handler']($p, $next) ?? $result($p);
  174. }
  175. return $next($payload);
  176. }
  177. /**
  178. * @param callable|string $handler
  179. * @return bool
  180. * @throws InvalidArgumentException
  181. */
  182. public function has(callable|string $handler): bool
  183. {
  184. return $this->indexOf($handler) > -1;
  185. }
  186. }