Event.php 4.8 KB

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