HookObserver.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains an abstract Hook observer class
  5. * Used for Hook Observers in plugins, called when a hook event happens
  6. * (e.g Create user, Webservice registration).
  7. *
  8. * @package chamilo.library.hook
  9. */
  10. /**
  11. * Class HookObserver
  12. * This abstract class implements Hook Observer Interface to build the base
  13. * for Hook Observer. This class have some public static method,
  14. * e.g for create Hook Observers.
  15. */
  16. abstract class HookObserver implements HookObserverInterface
  17. {
  18. public $path;
  19. public $pluginName;
  20. /**
  21. * Construct method
  22. * Save the path of Hook Observer class implementation and
  23. * the plugin name where this class is included.
  24. *
  25. * @param string $path
  26. * @param string $pluginName
  27. */
  28. protected function __construct($path, $pluginName)
  29. {
  30. $this->path = $path;
  31. $this->pluginName = $pluginName;
  32. }
  33. /**
  34. * Return the singleton instance of Hook observer.
  35. * If Hook Management plugin is not enabled, will return NULL.
  36. *
  37. * @return HookObserver
  38. */
  39. public static function create()
  40. {
  41. static $result = null;
  42. if ($result) {
  43. return $result;
  44. } else {
  45. try {
  46. $class = get_called_class();
  47. return new $class();
  48. } catch (Exception $e) {
  49. return null;
  50. }
  51. }
  52. }
  53. /**
  54. * Return the path from the class, needed to store location or autoload later.
  55. *
  56. * @return string
  57. */
  58. public function getPath()
  59. {
  60. return $this->path;
  61. }
  62. /**
  63. * Return the plugin name where is the Hook Observer.
  64. *
  65. * @return string
  66. */
  67. public function getPluginName()
  68. {
  69. return $this->pluginName;
  70. }
  71. }