events_dispatcher.class.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Class EventsDispatcher
  4. * Entry point for every event in the application.
  5. * Fires the functions linked to the events according to the event's conf.
  6. * Every function got its own filter, it's fired inside the functiones fired
  7. * by this class. The filter config is next to the event config, in conf/events.conf.php
  8. *
  9. */
  10. class EventsDispatcher
  11. {
  12. /**
  13. * @param string $event_name
  14. * @param array $event_data
  15. * @return bool
  16. */
  17. public static function events($event_name, $event_data = array())
  18. {
  19. global $event_config;
  20. // get the config for the event passed in parameter ($event_name)
  21. // and execute every actions with the values
  22. foreach ($event_config[$event_name]["actions"] as $func) {
  23. $execute = true;
  24. // if the function doesn't exist, we log
  25. if (!function_exists($func)) {
  26. error_log("EventsDispatcher warning : ".$func." does not exist.");
  27. $execute = false;
  28. }
  29. // check if the event's got a filter
  30. if (function_exists($event_name."_".$func."_filter_func")) {
  31. $filter = $event_name."_".$func."_filter_func";
  32. // if it does, we execute the filter (which changes the data
  33. // in-place and returns true on success or false on error)
  34. $execute = $filter($event_data);
  35. } else {
  36. // if there's no filter
  37. error_log("EventsDispatcher warning : ".$event_name."_".$func."_filter_func does not exist.");
  38. }
  39. if (!$execute) {
  40. // if the filter says we cannot send the mail, we get out of here
  41. return false;
  42. }
  43. // finally, if the filter says yes (or the filter doesn't exist),
  44. // we execute the in-between function that will call the needed
  45. // function
  46. $func($event_name, $event_data);
  47. }
  48. }
  49. }