Event.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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(string | array $events, $listener)
  7. * @method static bool hasListeners(string $eventName)
  8. * @method static void subscribe(object | string $subscriber)
  9. * @method static array|null until(string | object $event, $payload = [])
  10. * @method static array|null dispatch(string | object $event, $payload = [], bool $halt = false)
  11. * @method static void push(string $event, array $payload = [])
  12. * @method static void flush(string $event)
  13. * @method static void forget(string $event)
  14. * @method static void forgetPushed()
  15. *
  16. * @see \Illuminate\Events\Dispatcher
  17. */
  18. class Event extends Facade
  19. {
  20. /**
  21. * Replace the bound instance with a fake.
  22. *
  23. * @param array|string $eventsToFake
  24. * @return void
  25. */
  26. public static function fake($eventsToFake = [])
  27. {
  28. static::swap($fake = new EventFake(static::getFacadeRoot(), $eventsToFake));
  29. Model::setEventDispatcher($fake);
  30. }
  31. /**
  32. * Replace the bound instance with a fake during the given callable's execution.
  33. *
  34. * @param callable $callable
  35. * @param array|string $eventsToFake
  36. * @return callable
  37. */
  38. public static function fakeFor(callable $callable, array $eventsToFake = [])
  39. {
  40. $originalDispatcher = static::getFacadeRoot();
  41. static::fake($eventsToFake);
  42. return tap($callable(), function () use ($originalDispatcher) {
  43. static::swap($originalDispatcher);
  44. Model::setEventDispatcher($originalDispatcher);
  45. });
  46. }
  47. /**
  48. * Get the registered name of the component.
  49. *
  50. * @return string
  51. */
  52. protected static function getFacadeAccessor()
  53. {
  54. return 'events';
  55. }
  56. }