EmitterInterface.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\Event;
  4. /**
  5. * Event Emitter Interface.
  6. *
  7. * Anything that accepts listeners and emits events should implement this
  8. * interface.
  9. *
  10. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  11. * @author Evert Pot (http://evertpot.com/)
  12. * @license http://sabre.io/license/ Modified BSD License
  13. */
  14. interface EmitterInterface
  15. {
  16. /**
  17. * Subscribe to an event.
  18. */
  19. public function on(string $eventName, callable $callBack, int $priority = 100);
  20. /**
  21. * Subscribe to an event exactly once.
  22. */
  23. public function once(string $eventName, callable $callBack, int $priority = 100);
  24. /**
  25. * Emits an event.
  26. *
  27. * This method will return true if 0 or more listeners were successfully
  28. * handled. false is returned if one of the events broke the event chain.
  29. *
  30. * If the continueCallBack is specified, this callback will be called every
  31. * time before the next event handler is called.
  32. *
  33. * If the continueCallback returns false, event propagation stops. This
  34. * allows you to use the eventEmitter as a means for listeners to implement
  35. * functionality in your application, and break the event loop as soon as
  36. * some condition is fulfilled.
  37. *
  38. * Note that returning false from an event subscriber breaks propagation
  39. * and returns false, but if the continue-callback stops propagation, this
  40. * is still considered a 'successful' operation and returns true.
  41. *
  42. * Lastly, if there are 5 event handlers for an event. The continueCallback
  43. * will be called at most 4 times.
  44. */
  45. public function emit(string $eventName, array $arguments = [], ?callable $continueCallBack = null): bool;
  46. /**
  47. * Returns the list of listeners for an event.
  48. *
  49. * The list is returned as an array, and the list of events are sorted by
  50. * their priority.
  51. *
  52. * @return callable[]
  53. */
  54. public function listeners(string $eventName): array;
  55. /**
  56. * Removes a specific listener from an event.
  57. *
  58. * If the listener could not be found, this method will return false. If it
  59. * was removed it will return true.
  60. */
  61. public function removeListener(string $eventName, callable $listener): bool;
  62. /**
  63. * Removes all listeners.
  64. *
  65. * If the eventName argument is specified, all listeners for that event are
  66. * removed. If it is not specified, every listener for every event is
  67. * removed.
  68. */
  69. public function removeAllListeners(?string $eventName = null);
  70. }