DeferredCallback.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Illuminate\Support\Defer;
  3. use Illuminate\Support\Str;
  4. class DeferredCallback
  5. {
  6. /**
  7. * Create a new deferred callback instance.
  8. *
  9. * @param callable $callback
  10. */
  11. public function __construct(public $callback, public ?string $name = null, public bool $always = false)
  12. {
  13. $this->name = $name ?? (string) Str::uuid();
  14. }
  15. /**
  16. * Specify the name of the deferred callback so it can be cancelled later.
  17. *
  18. * @param string $name
  19. * @return $this
  20. */
  21. public function name(string $name): static
  22. {
  23. $this->name = $name;
  24. return $this;
  25. }
  26. /**
  27. * Indicate that the deferred callback should run even on unsuccessful requests and jobs.
  28. *
  29. * @param bool $always
  30. * @return $this
  31. */
  32. public function always(bool $always = true): static
  33. {
  34. $this->always = $always;
  35. return $this;
  36. }
  37. /**
  38. * Invoke the deferred callback.
  39. *
  40. * @return void
  41. */
  42. public function __invoke(): void
  43. {
  44. call_user_func($this->callback);
  45. }
  46. }