event.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. abstract class Event
  22. {
  23. /**
  24. * @var array $instances Event_Instance container
  25. */
  26. protected static $instances = array();
  27. /**
  28. * Event instance forge.
  29. *
  30. * @param array $events events array
  31. * @return object new Event_Instance instance
  32. */
  33. public static function forge(array $events = array())
  34. {
  35. return new \Event_Instance($events);
  36. }
  37. /**
  38. * Multiton Event instance.
  39. *
  40. * @param string $name instance name
  41. * @param array $events events array
  42. * @return object Event_Instance object
  43. */
  44. public static function instance($name = 'fuelphp', array $events = array())
  45. {
  46. if ( ! array_key_exists($name, static::$instances))
  47. {
  48. $events = array_merge(\Config::get('event.'.$name, array()), $events);
  49. $instance = static::forge($events);
  50. static::$instances[$name] = &$instance;
  51. }
  52. return static::$instances[$name];
  53. }
  54. // --------------------------------------------------------------------
  55. /**
  56. * method called by register_shutdown_event
  57. *
  58. * @access public
  59. * @param void
  60. * @return void
  61. */
  62. public static function shutdown()
  63. {
  64. $instance = static::instance();
  65. if ($instance->has_events('shutdown'))
  66. {
  67. // trigger the shutdown events
  68. $instance->trigger('shutdown', '', 'none', true);
  69. }
  70. }
  71. /**
  72. * Static call forwarder
  73. *
  74. * @param string $func method name
  75. * @param array $args passed arguments
  76. * @return
  77. */
  78. public static function __callStatic($func, $args)
  79. {
  80. $instance = static::instance();
  81. if (method_exists($instance, $func))
  82. {
  83. return call_user_func_array(array($instance, $func), $args);
  84. }
  85. throw new \BadMethodCallException('Call to undefined method: '.get_called_class().'::'.$func);
  86. }
  87. /**
  88. * Load events config
  89. */
  90. public static function _init()
  91. {
  92. \Config::load('event', true);
  93. }
  94. }