PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/heart/reborn/src/Reborn/Cores/Event.php

https://bitbucket.org/yelinaung/reborn
PHP | 185 lines | 97 code | 27 blank | 61 comment | 13 complexity | 2732c611ffb7f20f227eb52d7aba85af MD5 | raw file
  1. <?php
  2. namespace Reborn\Cores;
  3. use Reborn\Cores\Exception\EventException as EventException;
  4. /**
  5. * Event Class for Reborn CMS
  6. *
  7. * @package Cores
  8. * @author Reborn CMS Development Team
  9. **/
  10. class Event
  11. {
  12. /**
  13. * Variable for events list
  14. *
  15. * @var array
  16. */
  17. protected static $callbackEvents = array();
  18. /**
  19. * Variable for callback method is not callable message
  20. *
  21. * @var string
  22. */
  23. protected static $msg = 'Not callbable method { %s } for event name { %s } !';
  24. /**
  25. * Variable for Log object
  26. *
  27. * @var object
  28. */
  29. protected static $log;
  30. /**
  31. * Initialize method for Event Class
  32. *
  33. * @param array $events Events array list
  34. */
  35. public static function initialize($events = array())
  36. {
  37. static::$log = new Log;
  38. require CORES.'Event'.DS.'register'.EXT;
  39. //require CORES.'Config'.DS.'events'.EXT;
  40. if(! empty($events))
  41. {
  42. foreach($events as $k => $e)
  43. {
  44. static::add($e['name'], $e['callback']);
  45. }
  46. }
  47. }
  48. /**
  49. * Add(Register) the Event.
  50. * Don't use (.)dot operator at event name.
  51. * If callback function is not callable, you can check log file.
  52. * If you are developement process.
  53. * You should set true for event.callback_throw in app config file.
  54. * event.callback_throw is true, throw error msg when callback is not callable.
  55. *
  56. * @param string $name Event name (eg: blog_post_create)
  57. * @param string $callback Callback function name.
  58. */
  59. public static function add($name, $callback)
  60. {
  61. if(strpos($callback, '::'))
  62. {
  63. list($ns, $method) = explode('::', $callback);
  64. if(! is_callable(array($ns, $method)))
  65. {
  66. $msg = sprintf(static::$msg, $callback, $name);
  67. static::$log->error($msg);
  68. if(Config::get('app.event.callback_throw'))
  69. {
  70. throw new EventException($msg);
  71. }
  72. }
  73. }
  74. else
  75. {
  76. if(! is_callable($callback))
  77. {
  78. $msg = sprintf(static::$msg, $callback, $name);
  79. static::$log->error($msg);
  80. if(Config::get('app.event.callback_throw'))
  81. {
  82. throw new EventException($msg);
  83. }
  84. }
  85. }
  86. $eventName = strtolower($name);
  87. static::$callbackEvents[$eventName][] = $callback;
  88. }
  89. /**
  90. * Call(Trigger) the event.
  91. *
  92. * @param string $name Name of event
  93. * @param array $data Data array for callback event (optional)
  94. * @return void
  95. */
  96. public static function call($name, $data = array())
  97. {
  98. $eventName = strtolower($name);
  99. if(isset(static::$callbackEvents[$eventName]))
  100. {
  101. foreach(static::$callbackEvents[$eventName] as $callback)
  102. {
  103. call_user_func_array($callback, array($data));
  104. }
  105. }
  106. }
  107. /**
  108. * Check the given event name is have or not
  109. *
  110. * @param string $name Event name
  111. * @return boolean
  112. */
  113. public static function has($name)
  114. {
  115. $eventName = strtolower($name);
  116. return isset(static::$callbackEvents[$eventName]);
  117. }
  118. /**
  119. * Remove(UnRegister) the given event name.
  120. * Each event is an array.
  121. * If you give name only, remove the all method from event name.
  122. * If you want to remove event's some method from event name. use(.)
  123. * eg ( eventname.method)
  124. *
  125. * @param string $name Name of the event
  126. * @return void
  127. */
  128. public static function remove($name)
  129. {
  130. $method = '';
  131. if(strpos($name, '.'))
  132. {
  133. list($name, $method) = explode('.', $name);
  134. }
  135. $eventName = strtolower($name);
  136. if(static::has($name))
  137. {
  138. if($method == '')
  139. {
  140. unset(static::$callbackEvents[$eventName]);
  141. }
  142. else
  143. {
  144. foreach(static::$callbackEvents[$eventName] as $k => $n)
  145. {
  146. if($method == $n)
  147. {
  148. unset(static::$callbackEvents[$eventName][$k]);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. public static function getAll()
  155. {
  156. return static::$callbackEvents;
  157. }
  158. } // END class Event