Event.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(\Illuminate\Events\QueuedClosure|callable|array|string $events, \Illuminate\Events\QueuedClosure|callable|array|string|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 mixed defer(callable $callback, array|null $events = null)
  22. * @method static array getRawListeners()
  23. * @method static void macro(string $name, object|callable $macro)
  24. * @method static void mixin(object $mixin, bool $replace = true)
  25. * @method static bool hasMacro(string $name)
  26. * @method static void flushMacros()
  27. * @method static \Illuminate\Support\Testing\Fakes\EventFake except(array|string $eventsToDispatch)
  28. * @method static void assertListening(string $expectedEvent, string|array $expectedListener)
  29. * @method static void assertDispatched(string|\Closure $event, callable|int|null $callback = null)
  30. * @method static void assertDispatchedOnce(string $event, int $times = null)
  31. * @method static void assertDispatchedTimes(string $event, int $times = 1)
  32. * @method static void assertNotDispatched(string|\Closure $event, callable|null $callback = null)
  33. * @method static void assertNothingDispatched()
  34. * @method static \Illuminate\Support\Collection dispatched(string $event, callable|null $callback = null)
  35. * @method static bool hasDispatched(string $event)
  36. * @method static array dispatchedEvents()
  37. *
  38. * @see \Illuminate\Events\Dispatcher
  39. * @see \Illuminate\Support\Testing\Fakes\EventFake
  40. */
  41. class Event extends Facade
  42. {
  43. /**
  44. * Replace the bound instance with a fake.
  45. *
  46. * @param array|string $eventsToFake
  47. * @return \Illuminate\Support\Testing\Fakes\EventFake
  48. */
  49. public static function fake($eventsToFake = [])
  50. {
  51. $actualDispatcher = static::isFake()
  52. ? static::getFacadeRoot()->dispatcher
  53. : static::getFacadeRoot();
  54. return tap(new EventFake($actualDispatcher, $eventsToFake), function ($fake) {
  55. static::swap($fake);
  56. Model::setEventDispatcher($fake);
  57. Cache::refreshEventDispatcher();
  58. });
  59. }
  60. /**
  61. * Replace the bound instance with a fake that fakes all events except the given events.
  62. *
  63. * @param string[]|string $eventsToAllow
  64. * @return \Illuminate\Support\Testing\Fakes\EventFake
  65. */
  66. public static function fakeExcept($eventsToAllow)
  67. {
  68. return static::fake([
  69. function ($eventName) use ($eventsToAllow) {
  70. return ! in_array($eventName, (array) $eventsToAllow);
  71. },
  72. ]);
  73. }
  74. /**
  75. * Replace the bound instance with a fake during the given callable's execution.
  76. *
  77. * @param callable $callable
  78. * @param array $eventsToFake
  79. * @return mixed
  80. */
  81. public static function fakeFor(callable $callable, array $eventsToFake = [])
  82. {
  83. $originalDispatcher = static::getFacadeRoot();
  84. static::fake($eventsToFake);
  85. try {
  86. return $callable();
  87. } finally {
  88. static::swap($originalDispatcher);
  89. Model::setEventDispatcher($originalDispatcher);
  90. Cache::refreshEventDispatcher();
  91. }
  92. }
  93. /**
  94. * Replace the bound instance with a fake during the given callable's execution.
  95. *
  96. * @param callable $callable
  97. * @param array $eventsToAllow
  98. * @return mixed
  99. */
  100. public static function fakeExceptFor(callable $callable, array $eventsToAllow = [])
  101. {
  102. $originalDispatcher = static::getFacadeRoot();
  103. static::fakeExcept($eventsToAllow);
  104. try {
  105. return $callable();
  106. } finally {
  107. static::swap($originalDispatcher);
  108. Model::setEventDispatcher($originalDispatcher);
  109. Cache::refreshEventDispatcher();
  110. }
  111. }
  112. /**
  113. * Get the registered name of the component.
  114. *
  115. * @return string
  116. */
  117. protected static function getFacadeAccessor()
  118. {
  119. return 'events';
  120. }
  121. }