Events.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Yansongda\Pay;
  3. use Exception;
  4. use Symfony\Component\EventDispatcher\EventDispatcher;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Contracts\EventDispatcher\Event;
  7. /**
  8. * @author yansongda <me@yansongda.cn>
  9. *
  10. * @method static Event dispatch(Event $event) Dispatches an event to all registered listeners
  11. * @method static array getListeners($eventName = null) Gets the listeners of a specific event or all listeners sorted by descending priority.
  12. * @method static int|void getListenerPriority($eventName, $listener) Gets the listener priority for a specific event.
  13. * @method static bool hasListeners($eventName = null) Checks whether an event has any registered listeners.
  14. * @method static void addListener($eventName, $listener, $priority = 0) Adds an event listener that listens on the specified events.
  15. * @method static removeListener($eventName, $listener) Removes an event listener from the specified events.
  16. * @method static void addSubscriber(EventSubscriberInterface $subscriber) Adds an event subscriber.
  17. * @method static void removeSubscriber(EventSubscriberInterface $subscriber)
  18. */
  19. class Events
  20. {
  21. /**
  22. * dispatcher.
  23. *
  24. * @var EventDispatcher
  25. */
  26. protected static $dispatcher;
  27. /**
  28. * Forward call.
  29. *
  30. * @author yansongda <me@yansongda.cn>
  31. *
  32. * @param string $method
  33. * @param array $args
  34. *
  35. * @throws Exception
  36. *
  37. * @return mixed
  38. */
  39. public static function __callStatic($method, $args)
  40. {
  41. return call_user_func_array([self::getDispatcher(), $method], $args);
  42. }
  43. /**
  44. * Forward call.
  45. *
  46. * @author yansongda <me@yansongda.cn>
  47. *
  48. * @param string $method
  49. * @param array $args
  50. *
  51. * @throws Exception
  52. *
  53. * @return mixed
  54. */
  55. public function __call($method, $args)
  56. {
  57. return call_user_func_array([self::getDispatcher(), $method], $args);
  58. }
  59. /**
  60. * setDispatcher.
  61. *
  62. * @author yansongda <me@yansongda.cn>
  63. */
  64. public static function setDispatcher(EventDispatcher $dispatcher)
  65. {
  66. self::$dispatcher = $dispatcher;
  67. }
  68. /**
  69. * getDispatcher.
  70. *
  71. * @author yansongda <me@yansongda.cn>
  72. */
  73. public static function getDispatcher(): EventDispatcher
  74. {
  75. if (self::$dispatcher) {
  76. return self::$dispatcher;
  77. }
  78. return self::$dispatcher = self::createDispatcher();
  79. }
  80. /**
  81. * createDispatcher.
  82. *
  83. * @author yansongda <me@yansongda.cn>
  84. */
  85. public static function createDispatcher(): EventDispatcher
  86. {
  87. return new EventDispatcher();
  88. }
  89. }