| 1 |
<?php |
| 2 |
|
| 3 |
use Carbon\CarbonInterval; |
| 4 |
use Illuminate\Contracts\Support\DeferringDisplayableValue; |
| 5 |
use Illuminate\Contracts\Support\Htmlable; |
| 6 |
use Illuminate\Database\Eloquent\Model; |
| 7 |
use Illuminate\Support\Arr; |
| 8 |
use Illuminate\Support\Env; |
| 9 |
use Illuminate\Support\Fluent; |
| 10 |
use Illuminate\Support\HigherOrderTapProxy; |
| 11 |
use Illuminate\Support\Once; |
| 12 |
use Illuminate\Support\Onceable; |
| 13 |
use Illuminate\Support\Optional; |
| 14 |
use Illuminate\Support\Sleep; |
| 15 |
use Illuminate\Support\Str; |
| 16 |
use Illuminate\Support\Stringable as SupportStringable; |
| 17 |
|
| 18 |
if (! function_exists('append_config')) { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
function append_config(array $array): array |
| 25 |
{ |
| 26 |
$start = 9999; |
| 27 |
|
| 28 |
foreach ($array as $key => $value) { |
| 29 |
if (is_numeric($key)) { |
| 30 |
$start++; |
| 31 |
|
| 32 |
$array[$start] = Arr::pull($array, $key); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return $array; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
if (! function_exists('blank')) { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
function blank($value): bool |
| 51 |
{ |
| 52 |
if (is_null($value)) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
if (is_string($value)) { |
| 57 |
return trim($value) === ''; |
| 58 |
} |
| 59 |
|
| 60 |
if (is_numeric($value) || is_bool($value)) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
if ($value instanceof Model) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
if ($value instanceof Countable) { |
| 69 |
return count($value) === 0; |
| 70 |
} |
| 71 |
|
| 72 |
if ($value instanceof Stringable) { |
| 73 |
return trim((string) $value) === ''; |
| 74 |
} |
| 75 |
|
| 76 |
return empty($value); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (! function_exists('class_basename')) { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
function class_basename($class): string |
| 87 |
{ |
| 88 |
$class = is_object($class) ? get_class($class) : $class; |
| 89 |
|
| 90 |
return basename(str_replace('\\', '/', $class)); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (! function_exists('class_uses_recursive')) { |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
function class_uses_recursive($class): array |
| 102 |
{ |
| 103 |
if (is_object($class)) { |
| 104 |
$class = get_class($class); |
| 105 |
} |
| 106 |
|
| 107 |
$results = []; |
| 108 |
|
| 109 |
foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) { |
| 110 |
$results += trait_uses_recursive($class); |
| 111 |
} |
| 112 |
|
| 113 |
return array_unique($results); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if (! function_exists('e')) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
function e($value, $doubleEncode = true): string |
| 125 |
{ |
| 126 |
if ($value instanceof DeferringDisplayableValue) { |
| 127 |
$value = $value->resolveDisplayableValue(); |
| 128 |
} |
| 129 |
|
| 130 |
if ($value instanceof Htmlable) { |
| 131 |
return $value->toHtml() ?? ''; |
| 132 |
} |
| 133 |
|
| 134 |
if ($value instanceof BackedEnum) { |
| 135 |
$value = $value->value; |
| 136 |
} |
| 137 |
|
| 138 |
return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (! function_exists('env')) { |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
function env($key, $default = null) |
| 151 |
{ |
| 152 |
return Env::get($key, $default); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if (! function_exists('filled')) { |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
function filled($value): bool |
| 167 |
{ |
| 168 |
return ! blank($value); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (! function_exists('fluent')) { |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
function fluent($value = null): Fluent |
| 179 |
{ |
| 180 |
return new Fluent($value ?? []); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (! function_exists('literal')) { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
function literal(...$arguments) |
| 191 |
{ |
| 192 |
if (count($arguments) === 1 && array_is_list($arguments)) { |
| 193 |
return $arguments[0]; |
| 194 |
} |
| 195 |
|
| 196 |
return (object) $arguments; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if (! function_exists('object_get')) { |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
function object_get($object, $key, $default = null) |
| 212 |
{ |
| 213 |
if (is_null($key) || trim($key) === '') { |
| 214 |
return $object; |
| 215 |
} |
| 216 |
|
| 217 |
foreach (explode('.', $key) as $segment) { |
| 218 |
if (! is_object($object) || ! isset($object->{$segment})) { |
| 219 |
return value($default); |
| 220 |
} |
| 221 |
|
| 222 |
$object = $object->{$segment}; |
| 223 |
} |
| 224 |
|
| 225 |
return $object; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
if (! function_exists('laravel_cloud')) { |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
function laravel_cloud(): bool |
| 234 |
{ |
| 235 |
return ($_ENV['LARAVEL_CLOUD'] ?? false) === '1' || |
| 236 |
($_SERVER['LARAVEL_CLOUD'] ?? false) === '1'; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if (! function_exists('once')) { |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
function once(callable $callback) |
| 250 |
{ |
| 251 |
$onceable = Onceable::tryFromTrace( |
| 252 |
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2), |
| 253 |
$callback, |
| 254 |
); |
| 255 |
|
| 256 |
return $onceable ? Once::instance()->value($onceable) : call_user_func($callback); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if (! function_exists('optional')) { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
function optional($value = null, ?callable $callback = null) |
| 272 |
{ |
| 273 |
if (is_null($callback)) { |
| 274 |
return new Optional($value); |
| 275 |
} elseif (! is_null($value)) { |
| 276 |
return $callback($value); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
if (! function_exists('preg_replace_array')) { |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
function preg_replace_array($pattern, array $replacements, $subject): string |
| 290 |
{ |
| 291 |
return preg_replace_callback($pattern, function () use (&$replacements) { |
| 292 |
return array_shift($replacements); |
| 293 |
}, $subject); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if (! function_exists('retry')) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null) |
| 312 |
{ |
| 313 |
$attempts = 0; |
| 314 |
|
| 315 |
$backoff = []; |
| 316 |
|
| 317 |
if (is_array($times)) { |
| 318 |
$backoff = $times; |
| 319 |
|
| 320 |
$times = count($times) + 1; |
| 321 |
} |
| 322 |
|
| 323 |
beginning: |
| 324 |
$attempts++; |
| 325 |
$times--; |
| 326 |
|
| 327 |
try { |
| 328 |
return $callback($attempts); |
| 329 |
} catch (Throwable $e) { |
| 330 |
if ($times < 1 || ($when && ! $when($e))) { |
| 331 |
throw $e; |
| 332 |
} |
| 333 |
|
| 334 |
$sleepMilliseconds = $backoff[$attempts - 1] ?? $sleepMilliseconds; |
| 335 |
|
| 336 |
if ($sleepMilliseconds) { |
| 337 |
$duration = value($sleepMilliseconds, $attempts, $e); |
| 338 |
|
| 339 |
$duration instanceof CarbonInterval |
| 340 |
? Sleep::usleep($duration->totalMicroseconds) |
| 341 |
: Sleep::usleep($duration * 1000); |
| 342 |
} |
| 343 |
|
| 344 |
goto beginning; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if (! function_exists('str')) { |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
function str($string = null) |
| 357 |
{ |
| 358 |
if (func_num_args() === 0) { |
| 359 |
return new class |
| 360 |
{ |
| 361 |
public function __call($method, $parameters) |
| 362 |
{ |
| 363 |
return Str::$method(...$parameters); |
| 364 |
} |
| 365 |
|
| 366 |
public function __toString() |
| 367 |
{ |
| 368 |
return ''; |
| 369 |
} |
| 370 |
}; |
| 371 |
} |
| 372 |
|
| 373 |
return new SupportStringable($string); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if (! function_exists('tap')) { |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
function tap($value, $callback = null) |
| 388 |
{ |
| 389 |
if (is_null($callback)) { |
| 390 |
return new HigherOrderTapProxy($value); |
| 391 |
} |
| 392 |
|
| 393 |
$callback($value); |
| 394 |
|
| 395 |
return $value; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
if (! function_exists('throw_if')) { |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
function throw_if($condition, $exception = 'RuntimeException', ...$parameters) |
| 416 |
{ |
| 417 |
if ($condition) { |
| 418 |
if ($exception instanceof Closure) { |
| 419 |
$exception = $exception(...$parameters); |
| 420 |
} |
| 421 |
|
| 422 |
if (is_string($exception) && class_exists($exception)) { |
| 423 |
$exception = new $exception(...$parameters); |
| 424 |
} |
| 425 |
|
| 426 |
throw is_string($exception) ? new RuntimeException($exception) : $exception; |
| 427 |
} |
| 428 |
|
| 429 |
return $condition; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
if (! function_exists('throw_unless')) { |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
function throw_unless($condition, $exception = 'RuntimeException', ...$parameters) |
| 450 |
{ |
| 451 |
throw_if(! $condition, $exception, ...$parameters); |
| 452 |
|
| 453 |
return $condition; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
if (! function_exists('trait_uses_recursive')) { |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
function trait_uses_recursive($trait): array |
| 465 |
{ |
| 466 |
$traits = class_uses($trait) ?: []; |
| 467 |
|
| 468 |
foreach ($traits as $trait) { |
| 469 |
$traits += trait_uses_recursive($trait); |
| 470 |
} |
| 471 |
|
| 472 |
return $traits; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
if (! function_exists('transform')) { |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
function transform($value, callable $callback, $default = null) |
| 490 |
{ |
| 491 |
if (filled($value)) { |
| 492 |
return $callback($value); |
| 493 |
} |
| 494 |
|
| 495 |
if (is_callable($default)) { |
| 496 |
return $default($value); |
| 497 |
} |
| 498 |
|
| 499 |
return $default; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
if (! function_exists('windows_os')) { |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
function windows_os(): bool |
| 508 |
{ |
| 509 |
return PHP_OS_FAMILY === 'Windows'; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
if (! function_exists('with')) { |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
function with($value, ?callable $callback = null) |
| 525 |
{ |
| 526 |
return is_null($callback) ? $value : $callback($value); |
| 527 |
} |
| 528 |
} |