123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- use Illuminate\Contracts\Support\DeferringDisplayableValue;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Env;
- use Illuminate\Support\HigherOrderTapProxy;
- use Illuminate\Support\Optional;
- use Illuminate\Support\Sleep;
- use Illuminate\Support\Str;
- if (! function_exists('append_config')) {
- /**
- * Assign high numeric IDs to a config item to force appending.
- *
- * @param array $array
- * @return array
- */
- function append_config(array $array)
- {
- $start = 9999;
- foreach ($array as $key => $value) {
- if (is_numeric($key)) {
- $start++;
- $array[$start] = Arr::pull($array, $key);
- }
- }
- return $array;
- }
- }
- if (! function_exists('blank')) {
- /**
- * Determine if the given value is "blank".
- *
- * @param mixed $value
- * @return bool
- */
- function blank($value)
- {
- if (is_null($value)) {
- return true;
- }
- if (is_string($value)) {
- return trim($value) === '';
- }
- if (is_numeric($value) || is_bool($value)) {
- return false;
- }
- if ($value instanceof Countable) {
- return count($value) === 0;
- }
- return empty($value);
- }
- }
- if (! function_exists('class_basename')) {
- /**
- * Get the class "basename" of the given object / class.
- *
- * @param string|object $class
- * @return string
- */
- function class_basename($class)
- {
- $class = is_object($class) ? get_class($class) : $class;
- return basename(str_replace('\\', '/', $class));
- }
- }
- if (! function_exists('class_uses_recursive')) {
- /**
- * Returns all traits used by a class, its parent classes and trait of their traits.
- *
- * @param object|string $class
- * @return array
- */
- function class_uses_recursive($class)
- {
- if (is_object($class)) {
- $class = get_class($class);
- }
- $results = [];
- foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) {
- $results += trait_uses_recursive($class);
- }
- return array_unique($results);
- }
- }
- if (! function_exists('e')) {
- /**
- * Encode HTML special characters in a string.
- *
- * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|\BackedEnum|string|null $value
- * @param bool $doubleEncode
- * @return string
- */
- function e($value, $doubleEncode = true)
- {
- if ($value instanceof DeferringDisplayableValue) {
- $value = $value->resolveDisplayableValue();
- }
- if ($value instanceof Htmlable) {
- return $value->toHtml();
- }
- if ($value instanceof BackedEnum) {
- $value = $value->value;
- }
- return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode);
- }
- }
- if (! function_exists('env')) {
- /**
- * Gets the value of an environment variable.
- *
- * @param string $key
- * @param mixed $default
- * @return mixed
- */
- function env($key, $default = null)
- {
- return Env::get($key, $default);
- }
- }
- if (! function_exists('filled')) {
- /**
- * Determine if a value is "filled".
- *
- * @param mixed $value
- * @return bool
- */
- function filled($value)
- {
- return ! blank($value);
- }
- }
- if (! function_exists('laravel_cloud')) {
- /**
- * Determine if the application is running on Laravel Cloud.
- *
- * @return bool
- */
- function laravel_cloud()
- {
- return ($_ENV['LARAVEL_CLOUD'] ?? false) === '1' ||
- ($_SERVER['LARAVEL_CLOUD'] ?? false) === '1';
- }
- }
- if (! function_exists('object_get')) {
- /**
- * Get an item from an object using "dot" notation.
- *
- * @param object $object
- * @param string|null $key
- * @param mixed $default
- * @return mixed
- */
- function object_get($object, $key, $default = null)
- {
- if (is_null($key) || trim($key) === '') {
- return $object;
- }
- foreach (explode('.', $key) as $segment) {
- if (! is_object($object) || ! isset($object->{$segment})) {
- return value($default);
- }
- $object = $object->{$segment};
- }
- return $object;
- }
- }
- if (! function_exists('optional')) {
- /**
- * Provide access to optional objects.
- *
- * @param mixed $value
- * @param callable|null $callback
- * @return mixed
- */
- function optional($value = null, ?callable $callback = null)
- {
- if (is_null($callback)) {
- return new Optional($value);
- } elseif (! is_null($value)) {
- return $callback($value);
- }
- }
- }
- if (! function_exists('preg_replace_array')) {
- /**
- * Replace a given pattern with each value in the array in sequentially.
- *
- * @param string $pattern
- * @param array $replacements
- * @param string $subject
- * @return string
- */
- function preg_replace_array($pattern, array $replacements, $subject)
- {
- return preg_replace_callback($pattern, function () use (&$replacements) {
- foreach ($replacements as $value) {
- return array_shift($replacements);
- }
- }, $subject);
- }
- }
- if (! function_exists('retry')) {
- /**
- * Retry an operation a given number of times.
- *
- * @param int|array $times
- * @param callable $callback
- * @param int|\Closure $sleepMilliseconds
- * @param callable|null $when
- * @return mixed
- *
- * @throws \Exception
- */
- function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)
- {
- $attempts = 0;
- $backoff = [];
- if (is_array($times)) {
- $backoff = $times;
- $times = count($times) + 1;
- }
- beginning:
- $attempts++;
- $times--;
- try {
- return $callback($attempts);
- } catch (Exception $e) {
- if ($times < 1 || ($when && ! $when($e))) {
- throw $e;
- }
- $sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds;
- if ($sleepMilliseconds) {
- Sleep::usleep(value($sleepMilliseconds, $attempts, $e) * 1000);
- }
- goto beginning;
- }
- }
- }
- if (! function_exists('str')) {
- /**
- * Get a new stringable object from the given string.
- *
- * @param string|null $string
- * @return \Illuminate\Support\Stringable|mixed
- */
- function str($string = null)
- {
- if (func_num_args() === 0) {
- return new class
- {
- public function __call($method, $parameters)
- {
- return Str::$method(...$parameters);
- }
- public function __toString()
- {
- return '';
- }
- };
- }
- return Str::of($string);
- }
- }
- if (! function_exists('tap')) {
- /**
- * Call the given Closure with the given value then return the value.
- *
- * @param mixed $value
- * @param callable|null $callback
- * @return mixed
- */
- function tap($value, $callback = null)
- {
- if (is_null($callback)) {
- return new HigherOrderTapProxy($value);
- }
- $callback($value);
- return $value;
- }
- }
- if (! function_exists('throw_if')) {
- /**
- * Throw the given exception if the given condition is true.
- *
- * @template TException of \Throwable
- *
- * @param mixed $condition
- * @param TException|class-string<TException>|string $exception
- * @param mixed ...$parameters
- * @return mixed
- *
- * @throws TException
- */
- function throw_if($condition, $exception = 'RuntimeException', ...$parameters)
- {
- if ($condition) {
- if (is_string($exception) && class_exists($exception)) {
- $exception = new $exception(...$parameters);
- }
- throw is_string($exception) ? new RuntimeException($exception) : $exception;
- }
- return $condition;
- }
- }
- if (! function_exists('throw_unless')) {
- /**
- * Throw the given exception unless the given condition is true.
- *
- * @template TException of \Throwable
- *
- * @param mixed $condition
- * @param TException|class-string<TException>|string $exception
- * @param mixed ...$parameters
- * @return mixed
- *
- * @throws TException
- */
- function throw_unless($condition, $exception = 'RuntimeException', ...$parameters)
- {
- throw_if(! $condition, $exception, ...$parameters);
- return $condition;
- }
- }
- if (! function_exists('trait_uses_recursive')) {
- /**
- * Returns all traits used by a trait and its traits.
- *
- * @param object|string $trait
- * @return array
- */
- function trait_uses_recursive($trait)
- {
- $traits = class_uses($trait) ?: [];
- foreach ($traits as $trait) {
- $traits += trait_uses_recursive($trait);
- }
- return $traits;
- }
- }
- if (! function_exists('transform')) {
- /**
- * Transform the given value if it is present.
- *
- * @template TValue of mixed
- * @template TReturn of mixed
- * @template TDefault of mixed
- *
- * @param TValue $value
- * @param callable(TValue): TReturn $callback
- * @param TDefault|callable(TValue): TDefault|null $default
- * @return ($value is empty ? ($default is null ? null : TDefault) : TReturn)
- */
- function transform($value, callable $callback, $default = null)
- {
- if (filled($value)) {
- return $callback($value);
- }
- if (is_callable($default)) {
- return $default($value);
- }
- return $default;
- }
- }
- if (! function_exists('windows_os')) {
- /**
- * Determine whether the current environment is Windows based.
- *
- * @return bool
- */
- function windows_os()
- {
- return PHP_OS_FAMILY === 'Windows';
- }
- }
- if (! function_exists('with')) {
- /**
- * Return the given value, optionally passed through the given callback.
- *
- * @template TValue
- * @template TReturn
- *
- * @param TValue $value
- * @param (callable(TValue): (TReturn))|null $callback
- * @return ($callback is null ? TValue : TReturn)
- */
- function with($value, ?callable $callback = null)
- {
- return is_null($callback) ? $value : $callback($value);
- }
- }
|