sfEventDispatcher.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfEventDispatcher implements a dispatcher object.
  11. *
  12. * @see http://developer.apple.com/documentation/Cocoa/Conceptual/Notifications/index.html Apple's Cocoa framework
  13. *
  14. * @package symfony
  15. * @subpackage util
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @version SVN: $Id: sfEventDispatcher.class.php 17858 2009-05-01 21:22:50Z FabianLange $
  18. */
  19. class sfEventDispatcher
  20. {
  21. protected
  22. $listeners = array();
  23. /**
  24. * Connects a listener to a given event name.
  25. *
  26. * @param string $name An event name
  27. * @param mixed $listener A PHP callable
  28. */
  29. public function connect($name, $listener)
  30. {
  31. if (!isset($this->listeners[$name]))
  32. {
  33. $this->listeners[$name] = array();
  34. }
  35. $this->listeners[$name][] = $listener;
  36. }
  37. /**
  38. * Disconnects a listener for a given event name.
  39. *
  40. * @param string $name An event name
  41. * @param mixed $listener A PHP callable
  42. *
  43. * @return mixed false if listener does not exist, null otherwise
  44. */
  45. public function disconnect($name, $listener)
  46. {
  47. if (!isset($this->listeners[$name]))
  48. {
  49. return false;
  50. }
  51. foreach ($this->listeners[$name] as $i => $callable)
  52. {
  53. if ($listener === $callable)
  54. {
  55. unset($this->listeners[$name][$i]);
  56. }
  57. }
  58. }
  59. /**
  60. * Notifies all listeners of a given event.
  61. *
  62. * @param sfEvent $event A sfEvent instance
  63. *
  64. * @return sfEvent The sfEvent instance
  65. */
  66. public function notify(sfEvent $event)
  67. {
  68. foreach ($this->getListeners($event->getName()) as $listener)
  69. {
  70. call_user_func($listener, $event);
  71. }
  72. return $event;
  73. }
  74. /**
  75. * Notifies all listeners of a given event until one returns a non null value.
  76. *
  77. * @param sfEvent $event A sfEvent instance
  78. *
  79. * @return sfEvent The sfEvent instance
  80. */
  81. public function notifyUntil(sfEvent $event)
  82. {
  83. foreach ($this->getListeners($event->getName()) as $listener)
  84. {
  85. if (call_user_func($listener, $event))
  86. {
  87. $event->setProcessed(true);
  88. break;
  89. }
  90. }
  91. return $event;
  92. }
  93. /**
  94. * Filters a value by calling all listeners of a given event.
  95. *
  96. * @param sfEvent $event A sfEvent instance
  97. * @param mixed $value The value to be filtered
  98. *
  99. * @return sfEvent The sfEvent instance
  100. */
  101. public function filter(sfEvent $event, $value)
  102. {
  103. foreach ($this->getListeners($event->getName()) as $listener)
  104. {
  105. $value = call_user_func_array($listener, array($event, $value));
  106. }
  107. $event->setReturnValue($value);
  108. return $event;
  109. }
  110. /**
  111. * Returns true if the given event name has some listeners.
  112. *
  113. * @param string $name The event name
  114. *
  115. * @return Boolean true if some listeners are connected, false otherwise
  116. */
  117. public function hasListeners($name)
  118. {
  119. if (!isset($this->listeners[$name]))
  120. {
  121. $this->listeners[$name] = array();
  122. }
  123. return (boolean) count($this->listeners[$name]);
  124. }
  125. /**
  126. * Returns all listeners associated with a given event name.
  127. *
  128. * @param string $name The event name
  129. *
  130. * @return array An array of listeners
  131. */
  132. public function getListeners($name)
  133. {
  134. if (!isset($this->listeners[$name]))
  135. {
  136. return array();
  137. }
  138. return $this->listeners[$name];
  139. }
  140. }