Bus.php 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Illuminate\Support\Facades;
  3. use Illuminate\Bus\BatchRepository;
  4. use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract;
  5. use Illuminate\Foundation\Bus\PendingChain;
  6. use Illuminate\Support\Testing\Fakes\BusFake;
  7. /**
  8. * @method static mixed dispatch(mixed $command)
  9. * @method static mixed dispatchSync(mixed $command, mixed $handler = null)
  10. * @method static mixed dispatchNow(mixed $command, mixed $handler = null)
  11. * @method static \Illuminate\Bus\Batch|null findBatch(string $batchId)
  12. * @method static \Illuminate\Bus\PendingBatch batch(\Illuminate\Support\Collection|array|mixed $jobs)
  13. * @method static \Illuminate\Foundation\Bus\PendingChain chain(\Illuminate\Support\Collection|array $jobs)
  14. * @method static bool hasCommandHandler(mixed $command)
  15. * @method static bool|mixed getCommandHandler(mixed $command)
  16. * @method static mixed dispatchToQueue(mixed $command)
  17. * @method static void dispatchAfterResponse(mixed $command, mixed $handler = null)
  18. * @method static \Illuminate\Bus\Dispatcher pipeThrough(array $pipes)
  19. * @method static \Illuminate\Bus\Dispatcher map(array $map)
  20. * @method static \Illuminate\Support\Testing\Fakes\BusFake except(array|string $jobsToDispatch)
  21. * @method static void assertDispatched(string|\Closure $command, callable|int|null $callback = null)
  22. * @method static void assertDispatchedTimes(string|\Closure $command, int $times = 1)
  23. * @method static void assertNotDispatched(string|\Closure $command, callable|null $callback = null)
  24. * @method static void assertNothingDispatched()
  25. * @method static void assertDispatchedSync(string|\Closure $command, callable|int|null $callback = null)
  26. * @method static void assertDispatchedSyncTimes(string|\Closure $command, int $times = 1)
  27. * @method static void assertNotDispatchedSync(string|\Closure $command, callable|null $callback = null)
  28. * @method static void assertDispatchedAfterResponse(string|\Closure $command, callable|int|null $callback = null)
  29. * @method static void assertDispatchedAfterResponseTimes(string|\Closure $command, int $times = 1)
  30. * @method static void assertNotDispatchedAfterResponse(string|\Closure $command, callable|null $callback = null)
  31. * @method static void assertChained(array $expectedChain)
  32. * @method static void assertDispatchedWithoutChain(string|\Closure $command, callable|null $callback = null)
  33. * @method static \Illuminate\Support\Testing\Fakes\ChainedBatchTruthTest chainedBatch(\Closure $callback)
  34. * @method static void assertBatched(callable $callback)
  35. * @method static void assertBatchCount(int $count)
  36. * @method static void assertNothingBatched()
  37. * @method static \Illuminate\Support\Collection dispatched(string $command, callable|null $callback = null)
  38. * @method static \Illuminate\Support\Collection dispatchedSync(string $command, callable|null $callback = null)
  39. * @method static \Illuminate\Support\Collection dispatchedAfterResponse(string $command, callable|null $callback = null)
  40. * @method static \Illuminate\Support\Collection batched(callable $callback)
  41. * @method static bool hasDispatched(string $command)
  42. * @method static bool hasDispatchedSync(string $command)
  43. * @method static bool hasDispatchedAfterResponse(string $command)
  44. * @method static \Illuminate\Bus\Batch dispatchFakeBatch(string $name = '')
  45. * @method static \Illuminate\Bus\Batch recordPendingBatch(\Illuminate\Bus\PendingBatch $pendingBatch)
  46. * @method static \Illuminate\Support\Testing\Fakes\BusFake serializeAndRestore(bool $serializeAndRestore = true)
  47. *
  48. * @see \Illuminate\Bus\Dispatcher
  49. * @see \Illuminate\Support\Testing\Fakes\BusFake
  50. */
  51. class Bus extends Facade
  52. {
  53. /**
  54. * Replace the bound instance with a fake.
  55. *
  56. * @param array|string $jobsToFake
  57. * @param \Illuminate\Bus\BatchRepository|null $batchRepository
  58. * @return \Illuminate\Support\Testing\Fakes\BusFake
  59. */
  60. public static function fake($jobsToFake = [], ?BatchRepository $batchRepository = null)
  61. {
  62. $actualDispatcher = static::isFake()
  63. ? static::getFacadeRoot()->dispatcher
  64. : static::getFacadeRoot();
  65. return tap(new BusFake($actualDispatcher, $jobsToFake, $batchRepository), function ($fake) {
  66. static::swap($fake);
  67. });
  68. }
  69. /**
  70. * Dispatch the given chain of jobs.
  71. *
  72. * @param array|mixed $jobs
  73. * @return \Illuminate\Foundation\Bus\PendingDispatch
  74. */
  75. public static function dispatchChain($jobs)
  76. {
  77. $jobs = is_array($jobs) ? $jobs : func_get_args();
  78. return (new PendingChain(array_shift($jobs), $jobs))
  79. ->dispatch();
  80. }
  81. /**
  82. * Get the registered name of the component.
  83. *
  84. * @return string
  85. */
  86. protected static function getFacadeAccessor()
  87. {
  88. return BusDispatcherContract::class;
  89. }
  90. }