Bus.php 5.1 KB

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