observer.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 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 Orm;
  13. /**
  14. * Observer base class
  15. */
  16. abstract class Observer
  17. {
  18. /**
  19. * @var array list of created observer instances created
  20. */
  21. protected static $_instances = array();
  22. /**
  23. * Get notified of an event
  24. *
  25. * @param Model $instance
  26. * @param string $event
  27. */
  28. public static function orm_notify($instance, $event)
  29. {
  30. $model_class = get_class($instance);
  31. if (method_exists(static::instance($model_class), $event))
  32. {
  33. static::instance($model_class)->{$event}($instance);
  34. }
  35. }
  36. /**
  37. * Create an instance of this observer
  38. *
  39. * @param string name of the model class
  40. */
  41. public static function instance($model_class)
  42. {
  43. $observer = get_called_class();
  44. if (empty(static::$_instances[$observer][$model_class]))
  45. {
  46. static::$_instances[$observer][$model_class] = new static($model_class);
  47. }
  48. return static::$_instances[$observer][$model_class];
  49. }
  50. }