instance.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Event Class
  15. *
  16. * @package Fuel
  17. * @category Core
  18. * @author Eric Barnes
  19. * @author Harro "WanWizard" Verton
  20. */
  21. class Event_Instance
  22. {
  23. /**
  24. * @var array An array of listeners
  25. */
  26. protected $_events = array();
  27. // --------------------------------------------------------------------
  28. /**
  29. * Constructor, sets all initial events.
  30. *
  31. * @param array $events events array
  32. */
  33. public function __construct(array $events = array())
  34. {
  35. foreach($events as $event => $callback)
  36. {
  37. $this->register($event, $callback);
  38. }
  39. }
  40. // --------------------------------------------------------------------
  41. /**
  42. * Register
  43. *
  44. * Registers a Callback for a given event
  45. *
  46. * @access public
  47. * @param string The name of the event
  48. * @param mixed callback information
  49. * @return void
  50. */
  51. public function register()
  52. {
  53. // get any arguments passed
  54. $callback = func_get_args();
  55. // if the arguments are valid, register the event
  56. if (isset($callback[0]) and is_string($callback[0]) and isset($callback[1]) and is_callable($callback[1]))
  57. {
  58. // make sure we have an array for this event
  59. isset($this->_events[$callback[0]]) or $this->_events[$callback[0]] = array();
  60. // store the callback on the call stack
  61. if (empty($callback[2]))
  62. {
  63. array_unshift($this->_events[$callback[0]], $callback);
  64. }
  65. else
  66. {
  67. $this->_events[$callback[0]][] = $callback;
  68. }
  69. // and report success
  70. return true;
  71. }
  72. else
  73. {
  74. // can't register the event
  75. return false;
  76. }
  77. }
  78. // --------------------------------------------------------------------
  79. /**
  80. * Unregister/remove one or all callbacks from event
  81. *
  82. * @param string $event event to remove from
  83. * @param mixed $callback callback to remove [optional, null for all]
  84. * @return boolean wether one or all callbacks have been removed
  85. */
  86. public function unregister($event, $callback = null)
  87. {
  88. if (isset($this->_events[$event]))
  89. {
  90. if ($callback === true)
  91. {
  92. $this->_events = array();
  93. return true;
  94. }
  95. foreach ($this->_events[$event] as $i => $arguments)
  96. {
  97. if($callback === $arguments[1])
  98. {
  99. unset($this->_events[$event][$i]);
  100. return true;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * Trigger
  109. *
  110. * Triggers an event and returns the results. The results can be returned
  111. * in the following formats:
  112. *
  113. * 'array'
  114. * 'json'
  115. * 'serialized'
  116. * 'string'
  117. *
  118. * @access public
  119. * @param string The name of the event
  120. * @param mixed Any data that is to be passed to the listener
  121. * @param string The return type
  122. * @param boolean Wether to fire events ordered LIFO instead of FIFO
  123. * @return mixed The return of the listeners, in the return type
  124. */
  125. public function trigger($event, $data = '', $return_type = 'string', $reversed = false)
  126. {
  127. $calls = array();
  128. // check if we have events registered
  129. if ($this->has_events($event))
  130. {
  131. $events = $reversed ? array_reverse($this->_events[$event], true) : $this->_events[$event];
  132. // process them
  133. foreach ($events as $arguments)
  134. {
  135. // get rid of the event name
  136. array_shift($arguments);
  137. // get the callback method
  138. $callback = array_shift($arguments);
  139. // call the callback event
  140. if (is_callable($callback))
  141. {
  142. $calls[] = call_user_func($callback, $data, $arguments);
  143. }
  144. }
  145. }
  146. return $this->_format_return($calls, $return_type);
  147. }
  148. // --------------------------------------------------------------------
  149. /**
  150. * Has Listeners
  151. *
  152. * Checks if the event has listeners
  153. *
  154. * @access public
  155. * @param string The name of the event
  156. * @return bool Whether the event has listeners
  157. */
  158. public function has_events($event)
  159. {
  160. if (isset($this->_events[$event]) and count($this->_events[$event]) > 0)
  161. {
  162. return true;
  163. }
  164. return false;
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Format Return
  169. *
  170. * Formats the return in the given type
  171. *
  172. * @access protected
  173. * @param array The array of returns
  174. * @param string The return type
  175. * @return mixed The formatted return
  176. */
  177. protected function _format_return(array $calls, $return_type)
  178. {
  179. switch ($return_type)
  180. {
  181. case 'array':
  182. return $calls;
  183. break;
  184. case 'json':
  185. return json_encode($calls);
  186. break;
  187. case 'none':
  188. return;
  189. case 'serialized':
  190. return serialize($calls);
  191. break;
  192. case 'string':
  193. $str = '';
  194. foreach ($calls as $call)
  195. {
  196. $str .= $call;
  197. }
  198. return $str;
  199. break;
  200. default:
  201. return $calls;
  202. break;
  203. }
  204. return false;
  205. }
  206. }