Event.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Illuminate\Support\Facades;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Testing\Fakes\EventFake;
  5. /**
  6. * @method static \Closure createClassListener(string $listener, bool $wildcard = false)
  7. * @method static \Closure makeListener(\Closure|string $listener, bool $wildcard = false)
  8. * @method static \Illuminate\Events\Dispatcher setQueueResolver(callable $resolver)
  9. * @method static array getListeners(string $eventName)
  10. * @method static array|null dispatch(string|object $event, mixed $payload = [], bool $halt = false)
  11. * @method static array|null until(string|object $event, mixed $payload = [])
  12. * @method static bool hasListeners(string $eventName)
  13. * @method static void assertDispatched(string|\Closure $event, callable|int $callback = null)
  14. * @method static void assertDispatchedTimes(string $event, int $times = 1)
  15. * @method static void assertNotDispatched(string|\Closure $event, callable|int $callback = null)
  16. * @method static void assertNothingDispatched()
  17. * @method static void assertListening(string $expectedEvent, string $expectedListener)
  18. * @method static void flush(string $event)
  19. * @method static void forget(string $event)
  20. * @method static void forgetPushed()
  21. * @method static void listen(\Closure|string|array $events, \Closure|string|array $listener = null)
  22. * @method static void push(string $event, array $payload = [])
  23. * @method static void subscribe(object|string $subscriber)
  24. *
  25. * @see \Illuminate\Events\Dispatcher
  26. */
  27. class Event extends Facade
  28. {
  29. /**
  30. * Replace the bound instance with a fake.
  31. *
  32. * @param array|string $eventsToFake
  33. * @return \Illuminate\Support\Testing\Fakes\EventFake
  34. */
  35. public static function fake($eventsToFake = [])
  36. {
  37. static::swap($fake = new EventFake(static::getFacadeRoot(), $eventsToFake));
  38. Model::setEventDispatcher($fake);
  39. Cache::refreshEventDispatcher();
  40. return $fake;
  41. }
  42. /**
  43. * Replace the bound instance with a fake that fakes all events except the given events.
  44. *
  45. * @param string[]|string $eventsToAllow
  46. * @return \Illuminate\Support\Testing\Fakes\EventFake
  47. */
  48. public static function fakeExcept($eventsToAllow)
  49. {
  50. return static::fake([
  51. function ($eventName) use ($eventsToAllow) {
  52. return ! in_array($eventName, (array) $eventsToAllow);
  53. },
  54. ]);
  55. }
  56. /**
  57. * Replace the bound instance with a fake during the given callable's execution.
  58. *
  59. * @param callable $callable
  60. * @param array $eventsToFake
  61. * @return mixed
  62. */
  63. public static function fakeFor(callable $callable, array $eventsToFake = [])
  64. {
  65. $originalDispatcher = static::getFacadeRoot();
  66. static::fake($eventsToFake);
  67. return tap($callable(), function () use ($originalDispatcher) {
  68. static::swap($originalDispatcher);
  69. Model::setEventDispatcher($originalDispatcher);
  70. Cache::refreshEventDispatcher();
  71. });
  72. }
  73. /**
  74. * Replace the bound instance with a fake during the given callable's execution.
  75. *
  76. * @param callable $callable
  77. * @param array $eventsToAllow
  78. * @return mixed
  79. */
  80. public static function fakeExceptFor(callable $callable, array $eventsToAllow = [])
  81. {
  82. $originalDispatcher = static::getFacadeRoot();
  83. static::fakeExcept($eventsToAllow);
  84. return tap($callable(), function () use ($originalDispatcher) {
  85. static::swap($originalDispatcher);
  86. Model::setEventDispatcher($originalDispatcher);
  87. Cache::refreshEventDispatcher();
  88. });
  89. }
  90. /**
  91. * Get the registered name of the component.
  92. *
  93. * @return string
  94. */
  95. protected static function getFacadeAccessor()
  96. {
  97. return 'events';
  98. }
  99. }