GlobalEventManager.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_EventManager
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. require_once 'Zend/Stdlib/CallbackHandler.php';
  21. require_once 'Zend/Stdlib/PriorityQueue.php';
  22. /**
  23. * Event manager: notification system
  24. *
  25. * Use the EventManager when you want to create a per-instance notification
  26. * system for your objects.
  27. *
  28. * @category Zend
  29. * @package Zend_EventManager
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_EventManager_GlobalEventManager
  34. {
  35. /**
  36. * @var Zend_EventManager_EventCollection
  37. */
  38. protected static $events;
  39. /**
  40. * Set the event collection on which this will operate
  41. *
  42. * @param null|Zend_EventManager_EventCollection $events
  43. * @return void
  44. */
  45. public static function setEventCollection(Zend_EventManager_EventCollection $events = null)
  46. {
  47. self::$events = $events;
  48. }
  49. /**
  50. * Get event collection on which this operates
  51. *
  52. * @return Zend_EventManager_EventCollection
  53. */
  54. public static function getEventCollection()
  55. {
  56. if (null === self::$events) {
  57. self::setEventCollection(new Zend_EventManager_EventManager());
  58. }
  59. return self::$events;
  60. }
  61. /**
  62. * Trigger an event
  63. *
  64. * @param string $event
  65. * @param object|string $context
  66. * @param array|object $argv
  67. * @return Zend_EventManager_ResponseCollection
  68. */
  69. public static function trigger($event, $context, $argv = array())
  70. {
  71. return self::getEventCollection()->trigger($event, $context, $argv);
  72. }
  73. /**
  74. * Trigger listeenrs until return value of one causes a callback to evaluate
  75. * to true.
  76. *
  77. * @param string $event
  78. * @param string|object $context
  79. * @param array|object $argv
  80. * @param callback $callback
  81. * @return Zend_EventManager_ResponseCollection
  82. */
  83. public static function triggerUntil($event, $context, $argv, $callback)
  84. {
  85. return self::getEventCollection()->triggerUntil($event, $context, $argv, $callback);
  86. }
  87. /**
  88. * Attach a listener to an event
  89. *
  90. * @param string $event
  91. * @param callback $callback
  92. * @param int $priority
  93. * @return Zend_Stdlib_CallbackHandler
  94. */
  95. public static function attach($event, $callback, $priority = 1)
  96. {
  97. return self::getEventCollection()->attach($event, $callback, $priority);
  98. }
  99. /**
  100. * Detach a callback from a listener
  101. *
  102. * @param Zend_Stdlib_CallbackHandler $listener
  103. * @return bool
  104. */
  105. public static function detach(Zend_Stdlib_CallbackHandler $listener)
  106. {
  107. return self::getEventCollection()->detach($listener);
  108. }
  109. /**
  110. * Retrieve list of events this object manages
  111. *
  112. * @return array
  113. */
  114. public static function getEvents()
  115. {
  116. return self::getEventCollection()->getEvents();
  117. }
  118. /**
  119. * Retrieve all listeners for a given event
  120. *
  121. * @param string $event
  122. * @return Zend_Stdlib_PriorityQueue|array
  123. */
  124. public static function getListeners($event)
  125. {
  126. return self::getEventCollection()->getListeners($event);
  127. }
  128. /**
  129. * Clear all listeners for a given event
  130. *
  131. * @param string $event
  132. * @return void
  133. */
  134. public static function clearListeners($event)
  135. {
  136. return self::getEventCollection()->clearListeners($event);
  137. }
  138. }