Functions.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Support;
  12. use Carbon\Carbon;
  13. use Closure;
  14. use DateTimeZone;
  15. use Hyperf\Collection\Arr;
  16. use Hyperf\Context\ApplicationContext;
  17. use Hyperf\Di\Container;
  18. use Hyperf\Stringable\StrCache;
  19. use Hyperf\Support\Backoff\ArrayBackoff;
  20. use Throwable;
  21. /**
  22. * Return the default value of the given value.
  23. */
  24. function value(mixed $value, ...$args)
  25. {
  26. return $value instanceof Closure ? $value(...$args) : $value;
  27. }
  28. /**
  29. * Gets the value of an environment variable.
  30. *
  31. * @param string $key
  32. * @param null|mixed $default
  33. */
  34. function env($key, $default = null)
  35. {
  36. $value = getenv($key);
  37. if ($value === false) {
  38. return value($default);
  39. }
  40. switch (strtolower($value)) {
  41. case 'true':
  42. case '(true)':
  43. return true;
  44. case 'false':
  45. case '(false)':
  46. return false;
  47. case 'empty':
  48. case '(empty)':
  49. return '';
  50. case 'null':
  51. case '(null)':
  52. return null;
  53. }
  54. if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') {
  55. return substr($value, 1, -1);
  56. }
  57. return $value;
  58. }
  59. /**
  60. * Retry an operation a given number of times.
  61. *
  62. * @param float|int|int[] $times
  63. * @param int $sleep millisecond
  64. * @throws Throwable
  65. */
  66. function retry($times, callable $callback, int $sleep = 0)
  67. {
  68. $attempts = 0;
  69. if (is_array($times)) {
  70. $backoff = new ArrayBackoff($times);
  71. $times = count($times);
  72. } else {
  73. $backoff = new Backoff($sleep);
  74. }
  75. beginning:
  76. try {
  77. return $callback(++$attempts);
  78. } catch (Throwable $e) {
  79. if (--$times < 0) {
  80. throw $e;
  81. }
  82. $backoff->sleep();
  83. goto beginning;
  84. }
  85. }
  86. /**
  87. * Return the given value, optionally passed through the given callback.
  88. *
  89. * @param mixed $value
  90. */
  91. function with($value, ?callable $callback = null)
  92. {
  93. return is_null($callback) ? $value : $callback($value);
  94. }
  95. /**
  96. * Call a callback with the arguments.
  97. *
  98. * @param mixed $callback
  99. * @return null|mixed
  100. */
  101. function call($callback, array $args = [])
  102. {
  103. $result = null;
  104. if ($callback instanceof Closure) {
  105. $result = $callback(...$args);
  106. } elseif (is_object($callback) || (is_string($callback) && function_exists($callback))) {
  107. $result = $callback(...$args);
  108. } elseif (is_array($callback)) {
  109. [$object, $method] = $callback;
  110. $result = is_object($object) ? $object->{$method}(...$args) : $object::$method(...$args);
  111. } else {
  112. $result = call_user_func_array($callback, $args);
  113. }
  114. return $result;
  115. }
  116. /**
  117. * Get the class "basename" of the given object / class.
  118. *
  119. * @param object|string $class
  120. * @return string
  121. */
  122. function class_basename($class)
  123. {
  124. $class = is_object($class) ? get_class($class) : $class;
  125. return basename(str_replace('\\', '/', $class));
  126. }
  127. /**
  128. * Returns all traits used by a trait and its traits.
  129. *
  130. * @param object|string $trait
  131. * @return array
  132. */
  133. function trait_uses_recursive($trait)
  134. {
  135. $traits = class_uses($trait);
  136. foreach ($traits as $trait) {
  137. $traits += trait_uses_recursive($trait);
  138. }
  139. return $traits;
  140. }
  141. /**
  142. * Returns all traits used by a class, its parent classes and trait of their traits.
  143. *
  144. * @param object|string $class
  145. * @return array
  146. */
  147. function class_uses_recursive($class)
  148. {
  149. if (is_object($class)) {
  150. $class = get_class($class);
  151. }
  152. $results = [];
  153. /* @phpstan-ignore-next-line */
  154. foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) {
  155. $results += trait_uses_recursive($class);
  156. }
  157. return array_unique($results);
  158. }
  159. /**
  160. * Create a setter string.
  161. */
  162. function setter(string $property): string
  163. {
  164. return 'set' . StrCache::studly($property);
  165. }
  166. /**
  167. * Create a getter string.
  168. */
  169. function getter(string $property): string
  170. {
  171. return 'get' . StrCache::studly($property);
  172. }
  173. /**
  174. * Create an object instance, if the DI container exist in ApplicationContext,
  175. * then the object will be created by DI container via `make()` method, if not,
  176. * the object will create by `new` keyword.
  177. */
  178. function make(string $name, array $parameters = [])
  179. {
  180. if (ApplicationContext::hasContainer()) {
  181. /** @var Container $container */
  182. $container = ApplicationContext::getContainer();
  183. if (method_exists($container, 'make')) {
  184. return $container->make($name, $parameters);
  185. }
  186. }
  187. $parameters = array_values($parameters);
  188. return new $name(...$parameters);
  189. }
  190. /**
  191. * Return the default swoole hook flags, you can rewrite it by defining `SWOOLE_HOOK_FLAGS`.
  192. */
  193. function swoole_hook_flags(): int
  194. {
  195. return defined('SWOOLE_HOOK_FLAGS') ? SWOOLE_HOOK_FLAGS : SWOOLE_HOOK_ALL;
  196. }
  197. /**
  198. * Provide access to optional objects.
  199. *
  200. * @param mixed $value
  201. * @return mixed
  202. */
  203. function optional($value = null, ?callable $callback = null)
  204. {
  205. if (is_null($callback)) {
  206. return new Optional($value);
  207. }
  208. if (! is_null($value)) {
  209. return $callback($value);
  210. }
  211. return null;
  212. }
  213. /**
  214. * Build SQL contain bind.
  215. */
  216. function build_sql(string $sql, array $bindings = []): string
  217. {
  218. if (! Arr::isAssoc($bindings)) {
  219. $position = 0;
  220. foreach ($bindings as $value) {
  221. $position = strpos($sql, '?', $position);
  222. if ($position === false) {
  223. break;
  224. }
  225. $value = (string) match (gettype($value)) {
  226. 'integer', 'double' => $value,
  227. 'boolean' => (int) $value,
  228. default => sprintf("'%s'", $value),
  229. };
  230. $sql = substr_replace($sql, $value, $position, 1);
  231. $position += strlen($value);
  232. }
  233. }
  234. return $sql;
  235. }
  236. /**
  237. * Sleep milliseconds.
  238. */
  239. function msleep(int $milliSeconds): void
  240. {
  241. usleep($milliSeconds * 1000);
  242. }
  243. /**
  244. * Create a new Carbon instance for the current time.
  245. *
  246. * @param null|DateTimeZone|string $tz
  247. * @return Carbon
  248. */
  249. function now($tz = null)
  250. {
  251. return Carbon::now($tz);
  252. }
  253. /**
  254. * Create a new Carbon instance for the current date.
  255. *
  256. * @param null|DateTimeZone|string $tz
  257. * @return Carbon
  258. */
  259. function today($tz = null)
  260. {
  261. return Carbon::today($tz);
  262. }