Event.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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)
  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. *
  35. * @see \Illuminate\Events\Dispatcher
  36. * @see \Illuminate\Support\Testing\Fakes\EventFake
  37. */
  38. class Event extends Facade
  39. {
  40. /**
  41. * Replace the bound instance with a fake.
  42. *
  43. * @param array|string $eventsToFake
  44. * @return \Illuminate\Support\Testing\Fakes\EventFake
  45. */
  46. public static function fake($eventsToFake = [])
  47. {
  48. $actualDispatcher = static::isFake()
  49. ? static::getFacadeRoot()->dispatcher
  50. : static::getFacadeRoot();
  51. return tap(new EventFake($actualDispatcher, $eventsToFake), function ($fake) {
  52. static::swap($fake);
  53. Model::setEventDispatcher($fake);
  54. Cache::refreshEventDispatcher();
  55. });
  56. }
  57. /**
  58. * Replace the bound instance with a fake that fakes all events except the given events.
  59. *
  60. * @param string[]|string $eventsToAllow
  61. * @return \Illuminate\Support\Testing\Fakes\EventFake
  62. */
  63. public static function fakeExcept($eventsToAllow)
  64. {
  65. return static::fake([
  66. function ($eventName) use ($eventsToAllow) {
  67. return ! in_array($eventName, (array) $eventsToAllow);
  68. },
  69. ]);
  70. }
  71. /**
  72. * Replace the bound instance with a fake during the given callable's execution.
  73. *
  74. * @param callable $callable
  75. * @param array $eventsToFake
  76. * @return mixed
  77. */
  78. public static function fakeFor(callable $callable, array $eventsToFake = [])
  79. {
  80. $originalDispatcher = static::getFacadeRoot();
  81. static::fake($eventsToFake);
  82. return tap($callable(), function () use ($originalDispatcher) {
  83. static::swap($originalDispatcher);
  84. Model::setEventDispatcher($originalDispatcher);
  85. Cache::refreshEventDispatcher();
  86. });
  87. }
  88. /**
  89. * Replace the bound instance with a fake during the given callable's execution.
  90. *
  91. * @param callable $callable
  92. * @param array $eventsToAllow
  93. * @return mixed
  94. */
  95. public static function fakeExceptFor(callable $callable, array $eventsToAllow = [])
  96. {
  97. $originalDispatcher = static::getFacadeRoot();
  98. static::fakeExcept($eventsToAllow);
  99. return tap($callable(), function () use ($originalDispatcher) {
  100. static::swap($originalDispatcher);
  101. Model::setEventDispatcher($originalDispatcher);
  102. Cache::refreshEventDispatcher();
  103. });
  104. }
  105. /**
  106. * Get the registered name of the component.
  107. *
  108. * @return string
  109. */
  110. protected static function getFacadeAccessor()
  111. {
  112. return 'events';
  113. }
  114. }