helpers.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. use Illuminate\Contracts\Support\DeferringDisplayableValue;
  3. use Illuminate\Contracts\Support\Htmlable;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Env;
  6. use Illuminate\Support\HigherOrderTapProxy;
  7. use Illuminate\Support\Optional;
  8. use Illuminate\Support\Sleep;
  9. use Illuminate\Support\Str;
  10. if (! function_exists('append_config')) {
  11. /**
  12. * Assign high numeric IDs to a config item to force appending.
  13. *
  14. * @param array $array
  15. * @return array
  16. */
  17. function append_config(array $array)
  18. {
  19. $start = 9999;
  20. foreach ($array as $key => $value) {
  21. if (is_numeric($key)) {
  22. $start++;
  23. $array[$start] = Arr::pull($array, $key);
  24. }
  25. }
  26. return $array;
  27. }
  28. }
  29. if (! function_exists('blank')) {
  30. /**
  31. * Determine if the given value is "blank".
  32. *
  33. * @param mixed $value
  34. * @return bool
  35. */
  36. function blank($value)
  37. {
  38. if (is_null($value)) {
  39. return true;
  40. }
  41. if (is_string($value)) {
  42. return trim($value) === '';
  43. }
  44. if (is_numeric($value) || is_bool($value)) {
  45. return false;
  46. }
  47. if ($value instanceof Countable) {
  48. return count($value) === 0;
  49. }
  50. return empty($value);
  51. }
  52. }
  53. if (! function_exists('class_basename')) {
  54. /**
  55. * Get the class "basename" of the given object / class.
  56. *
  57. * @param string|object $class
  58. * @return string
  59. */
  60. function class_basename($class)
  61. {
  62. $class = is_object($class) ? get_class($class) : $class;
  63. return basename(str_replace('\\', '/', $class));
  64. }
  65. }
  66. if (! function_exists('class_uses_recursive')) {
  67. /**
  68. * Returns all traits used by a class, its parent classes and trait of their traits.
  69. *
  70. * @param object|string $class
  71. * @return array
  72. */
  73. function class_uses_recursive($class)
  74. {
  75. if (is_object($class)) {
  76. $class = get_class($class);
  77. }
  78. $results = [];
  79. foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) {
  80. $results += trait_uses_recursive($class);
  81. }
  82. return array_unique($results);
  83. }
  84. }
  85. if (! function_exists('e')) {
  86. /**
  87. * Encode HTML special characters in a string.
  88. *
  89. * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|null $value
  90. * @param bool $doubleEncode
  91. * @return string
  92. */
  93. function e($value, $doubleEncode = true)
  94. {
  95. if ($value instanceof DeferringDisplayableValue) {
  96. $value = $value->resolveDisplayableValue();
  97. }
  98. if ($value instanceof Htmlable) {
  99. return $value->toHtml();
  100. }
  101. if ($value instanceof BackedEnum) {
  102. $value = $value->value;
  103. }
  104. return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode);
  105. }
  106. }
  107. if (! function_exists('env')) {
  108. /**
  109. * Gets the value of an environment variable.
  110. *
  111. * @param string $key
  112. * @param mixed $default
  113. * @return mixed
  114. */
  115. function env($key, $default = null)
  116. {
  117. return Env::get($key, $default);
  118. }
  119. }
  120. if (! function_exists('filled')) {
  121. /**
  122. * Determine if a value is "filled".
  123. *
  124. * @param mixed $value
  125. * @return bool
  126. */
  127. function filled($value)
  128. {
  129. return ! blank($value);
  130. }
  131. }
  132. if (! function_exists('laravel_cloud')) {
  133. /**
  134. * Determine if the application is running on Laravel Cloud.
  135. *
  136. * @return bool
  137. */
  138. function laravel_cloud()
  139. {
  140. return ($_ENV['LARAVEL_CLOUD'] ?? false) === '1' ||
  141. ($_SERVER['LARAVEL_CLOUD'] ?? false) === '1';
  142. }
  143. }
  144. if (! function_exists('object_get')) {
  145. /**
  146. * Get an item from an object using "dot" notation.
  147. *
  148. * @param object $object
  149. * @param string|null $key
  150. * @param mixed $default
  151. * @return mixed
  152. */
  153. function object_get($object, $key, $default = null)
  154. {
  155. if (is_null($key) || trim($key) === '') {
  156. return $object;
  157. }
  158. foreach (explode('.', $key) as $segment) {
  159. if (! is_object($object) || ! isset($object->{$segment})) {
  160. return value($default);
  161. }
  162. $object = $object->{$segment};
  163. }
  164. return $object;
  165. }
  166. }
  167. if (! function_exists('optional')) {
  168. /**
  169. * Provide access to optional objects.
  170. *
  171. * @param mixed $value
  172. * @param callable|null $callback
  173. * @return mixed
  174. */
  175. function optional($value = null, ?callable $callback = null)
  176. {
  177. if (is_null($callback)) {
  178. return new Optional($value);
  179. } elseif (! is_null($value)) {
  180. return $callback($value);
  181. }
  182. }
  183. }
  184. if (! function_exists('preg_replace_array')) {
  185. /**
  186. * Replace a given pattern with each value in the array in sequentially.
  187. *
  188. * @param string $pattern
  189. * @param array $replacements
  190. * @param string $subject
  191. * @return string
  192. */
  193. function preg_replace_array($pattern, array $replacements, $subject)
  194. {
  195. return preg_replace_callback($pattern, function () use (&$replacements) {
  196. foreach ($replacements as $value) {
  197. return array_shift($replacements);
  198. }
  199. }, $subject);
  200. }
  201. }
  202. if (! function_exists('retry')) {
  203. /**
  204. * Retry an operation a given number of times.
  205. *
  206. * @param int|array $times
  207. * @param callable $callback
  208. * @param int|\Closure $sleepMilliseconds
  209. * @param callable|null $when
  210. * @return mixed
  211. *
  212. * @throws \Exception
  213. */
  214. function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
  215. {
  216. $attempts = 0;
  217. $backoff = [];
  218. if (is_array($times)) {
  219. $backoff = $times;
  220. $times = count($times) + 1;
  221. }
  222. beginning:
  223. $attempts++;
  224. $times--;
  225. try {
  226. return $callback($attempts);
  227. } catch (Exception $e) {
  228. if ($times < 1 || ($when && ! $when($e))) {
  229. throw $e;
  230. }
  231. $sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;
  232. if ($sleepMilliseconds) {
  233. Sleep::usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
  234. }
  235. goto beginning;
  236. }
  237. }
  238. }
  239. if (! function_exists('str')) {
  240. /**
  241. * Get a new stringable object from the given string.
  242. *
  243. * @param string|null $string
  244. * @return \Illuminate\Support\Stringable|mixed
  245. */
  246. function str($string = null)
  247. {
  248. if (func_num_args() === 0) {
  249. return new class
  250. {
  251. public function __call($method, $parameters)
  252. {
  253. return Str::$method(...$parameters);
  254. }
  255. public function __toString()
  256. {
  257. return '';
  258. }
  259. };
  260. }
  261. return Str::of($string);
  262. }
  263. }
  264. if (! function_exists('tap')) {
  265. /**
  266. * Call the given Closure with the given value then return the value.
  267. *
  268. * @param mixed $value
  269. * @param callable|null $callback
  270. * @return mixed
  271. */
  272. function tap($value, $callback = null)
  273. {
  274. if (is_null($callback)) {
  275. return new HigherOrderTapProxy($value);
  276. }
  277. $callback($value);
  278. return $value;
  279. }
  280. }
  281. if (! function_exists('throw_if')) {
  282. /**
  283. * Throw the given exception if the given condition is true.
  284. *
  285. * @template TException of \Throwable
  286. *
  287. * @param mixed $condition
  288. * @param TException|class-string<TException>|string $exception
  289. * @param mixed ...$parameters
  290. * @return mixed
  291. *
  292. * @throws TException
  293. */
  294. function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
  295. {
  296. if ($condition) {
  297. if (is_string($exception) && class_exists($exception)) {
  298. $exception = new $exception(...$parameters);
  299. }
  300. throw is_string($exception) ? new RuntimeException($exception) : $exception;
  301. }
  302. return $condition;
  303. }
  304. }
  305. if (! function_exists('throw_unless')) {
  306. /**
  307. * Throw the given exception unless the given condition is true.
  308. *
  309. * @template TException of \Throwable
  310. *
  311. * @param mixed $condition
  312. * @param TException|class-string<TException>|string $exception
  313. * @param mixed ...$parameters
  314. * @return mixed
  315. *
  316. * @throws TException
  317. */
  318. function throw_unless($condition, $exception = 'RuntimeException', ...$parameters)
  319. {
  320. throw_if(! $condition, $exception, ...$parameters);
  321. return $condition;
  322. }
  323. }
  324. if (! function_exists('trait_uses_recursive')) {
  325. /**
  326. * Returns all traits used by a trait and its traits.
  327. *
  328. * @param object|string $trait
  329. * @return array
  330. */
  331. function trait_uses_recursive($trait)
  332. {
  333. $traits = class_uses($trait) ?: [];
  334. foreach ($traits as $trait) {
  335. $traits += trait_uses_recursive($trait);
  336. }
  337. return $traits;
  338. }
  339. }
  340. if (! function_exists('transform')) {
  341. /**
  342. * Transform the given value if it is present.
  343. *
  344. * @template TValue of mixed
  345. * @template TReturn of mixed
  346. * @template TDefault of mixed
  347. *
  348. * @param TValue $value
  349. * @param callable(TValue): TReturn $callback
  350. * @param TDefault|callable(TValue): TDefault|null $default
  351. * @return ($value is empty ? ($default is null ? null : TDefault) : TReturn)
  352. */
  353. function transform($value, callable $callback, $default = null)
  354. {
  355. if (filled($value)) {
  356. return $callback($value);
  357. }
  358. if (is_callable($default)) {
  359. return $default($value);
  360. }
  361. return $default;
  362. }
  363. }
  364. if (! function_exists('windows_os')) {
  365. /**
  366. * Determine whether the current environment is Windows based.
  367. *
  368. * @return bool
  369. */
  370. function windows_os()
  371. {
  372. return PHP_OS_FAMILY === 'Windows';
  373. }
  374. }
  375. if (! function_exists('with')) {
  376. /**
  377. * Return the given value, optionally passed through the given callback.
  378. *
  379. * @template TValue
  380. * @template TReturn
  381. *
  382. * @param TValue $value
  383. * @param (callable(TValue): (TReturn))|null $callback
  384. * @return ($callback is null ? TValue : TReturn)
  385. */
  386. function with($value, ?callable $callback = null)
  387. {
  388. return is_null($callback) ? $value : $callback($value);
  389. }
  390. }