|
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,43 @@ 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 | + * @return TValue |
| 210 | + */ |
| 211 | + function lazy($class, $callback = 0, $options = 0) |
| 212 | + { |
| 213 | + static $reflector = new class |
| 214 | + { |
| 215 | + use ReflectsClosures; |
| 216 | + |
| 217 | + public function get($callback) |
| 218 | + { |
| 219 | + return $this->firstClosureParameterType($callback); |
| 220 | + } |
| 221 | + }; |
| 222 | + |
| 223 | + [$class, $callback, $options] = is_string($class) |
| 224 | + ? [$class, $callback, $options] |
| 225 | + : [$reflector->get($class), $class, $callback ?: $options]; |
| 226 | + |
| 227 | + return (new ReflectionClass($class))->newLazyGhost(function ($instance) use ($callback) { |
| 228 | + $result = $callback($instance); |
| 229 | + |
| 230 | + if (is_array($result)) { |
| 231 | + $instance->__construct(...$result); |
| 232 | + } |
| 233 | + }, $options); |
| 234 | + } |
| 235 | +} |
| 236 | + |
199 | 237 | if (! function_exists('object_get')) { |
200 | 238 | /** |
201 | 239 | * Get an item from an object using "dot" notation. |
@@ -295,6 +333,37 @@ function preg_replace_array($pattern, array $replacements, $subject): string |
295 | 333 | } |
296 | 334 | } |
297 | 335 |
|
| 336 | +if (! function_exists('proxy')) { |
| 337 | + /** |
| 338 | + * Create a lazy proxy instance. |
| 339 | + * |
| 340 | + * @template TValue of object |
| 341 | + * |
| 342 | + * @param class-string<TValue>|(\Closure(TValue): TValue) $class |
| 343 | + * @param (\Closure(TValue): TValue)|int $callback |
| 344 | + * @param int $options |
| 345 | + * @return TValue |
| 346 | + */ |
| 347 | + function proxy($class, $callback = 0, $options = 0) |
| 348 | + { |
| 349 | + static $reflector = new class |
| 350 | + { |
| 351 | + use ReflectsClosures; |
| 352 | + |
| 353 | + public function get($callback) |
| 354 | + { |
| 355 | + return $this->firstClosureParameterType($callback); |
| 356 | + } |
| 357 | + }; |
| 358 | + |
| 359 | + [$class, $callback, $options] = is_string($class) |
| 360 | + ? [$class, $callback, $options] |
| 361 | + : [$reflector->get($class), $class, $callback ?: $options]; |
| 362 | + |
| 363 | + return (new ReflectionClass($class))->newLazyProxy($callback, $options); |
| 364 | + } |
| 365 | +} |
| 366 | + |
298 | 367 | if (! function_exists('retry')) { |
299 | 368 | /** |
300 | 369 | * Retry an operation a given number of times. |
|
0 commit comments