|
13 | 13 | use Illuminate\Support\Sleep; |
14 | 14 | use Illuminate\Support\Str; |
15 | 15 | use Illuminate\Support\Stringable as SupportStringable; |
| 16 | +use Illuminate\Support\Traits\ReflectsClosures; |
16 | 17 |
|
17 | 18 | if (! function_exists('append_config')) { |
18 | 19 | /** |
@@ -196,6 +197,60 @@ function literal(...$arguments) |
196 | 197 | } |
197 | 198 | } |
198 | 199 |
|
| 200 | +if (! function_exists('lazy')) { |
| 201 | + /** |
| 202 | + * Create a lazy instance. |
| 203 | + * |
| 204 | + * @template TValue of object |
| 205 | + * |
| 206 | + * @param class-string<TValue>|(\Closure(TValue): mixed) $class |
| 207 | + * @param (\Closure(TValue): mixed)|int $callback |
| 208 | + * @param int $options |
| 209 | + * @param array<string, mixed> $eager |
| 210 | + * @return TValue |
| 211 | + */ |
| 212 | + function lazy($class, $callback = 0, $options = 0, $eager = []) |
| 213 | + { |
| 214 | + static $supported = version_compare(phpversion(), '8.4.0', '>='); |
| 215 | + |
| 216 | + if (! $supported) { |
| 217 | + throw new RuntimeException('The `lazy` helper is only supported in PHP 8.4 or higher.'); |
| 218 | + } |
| 219 | + |
| 220 | + static $closureReflector = null; |
| 221 | + |
| 222 | + $closureReflector ??= new class |
| 223 | + { |
| 224 | + use ReflectsClosures; |
| 225 | + |
| 226 | + public function typeFromParameter($callback) |
| 227 | + { |
| 228 | + return $this->firstClosureParameterType($callback); |
| 229 | + } |
| 230 | + }; |
| 231 | + |
| 232 | + [$class, $callback, $options] = is_string($class) |
| 233 | + ? [$class, $callback, $options] |
| 234 | + : [$closureReflector->typeFromParameter($class), $class, $callback ?: $options]; |
| 235 | + |
| 236 | + $reflectionClass = new ReflectionClass($class); |
| 237 | + |
| 238 | + $instance = $reflectionClass->newLazyGhost(function ($instance) use ($callback) { |
| 239 | + $result = $callback($instance); |
| 240 | + |
| 241 | + if (is_array($result)) { |
| 242 | + $instance->__construct(...$result); |
| 243 | + } |
| 244 | + }, $options); |
| 245 | + |
| 246 | + foreach ($eager as $property => $value) { |
| 247 | + $reflectionClass->getProperty($property)->setRawValueWithoutLazyInitialization($instance, $value); |
| 248 | + } |
| 249 | + |
| 250 | + return $instance; |
| 251 | + } |
| 252 | +} |
| 253 | + |
199 | 254 | if (! function_exists('object_get')) { |
200 | 255 | /** |
201 | 256 | * Get an item from an object using "dot" notation. |
@@ -295,6 +350,45 @@ function preg_replace_array($pattern, array $replacements, $subject): string |
295 | 350 | } |
296 | 351 | } |
297 | 352 |
|
| 353 | +if (! function_exists('proxy')) { |
| 354 | + /** |
| 355 | + * Create a lazy proxy instance. |
| 356 | + * |
| 357 | + * @template TValue of object |
| 358 | + * |
| 359 | + * @param class-string<TValue>|(\Closure(TValue): TValue) $class |
| 360 | + * @param (\Closure(TValue): TValue)|int $callback |
| 361 | + * @param int $options |
| 362 | + * @return TValue |
| 363 | + */ |
| 364 | + function proxy($class, $callback = 0, $options = 0) |
| 365 | + { |
| 366 | + static $supported = version_compare(phpversion(), '8.4.0', '>='); |
| 367 | + |
| 368 | + if (! $supported) { |
| 369 | + throw new RuntimeException('The `lazy` helper is only supported in PHP 8.4 or higher.'); |
| 370 | + } |
| 371 | + |
| 372 | + static $clousreReflector = null; |
| 373 | + |
| 374 | + $clousreReflector ??= new class |
| 375 | + { |
| 376 | + use ReflectsClosures; |
| 377 | + |
| 378 | + public function typeFromParameter($callback) |
| 379 | + { |
| 380 | + return $this->firstClosureParameterType($callback); |
| 381 | + } |
| 382 | + }; |
| 383 | + |
| 384 | + [$class, $callback, $options] = is_string($class) |
| 385 | + ? [$class, $callback, $options] |
| 386 | + : [$clousreReflector->typeFromParameter($class), $class, $callback ?: $options]; |
| 387 | + |
| 388 | + return (new ReflectionClass($class))->newLazyProxy($callback, $options); |
| 389 | + } |
| 390 | +} |
| 391 | + |
298 | 392 | if (! function_exists('retry')) { |
299 | 393 | /** |
300 | 394 | * Retry an operation a given number of times. |
|
0 commit comments