HookObserver.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * @package chamilo.library.hook
  8. */
  9. /**
  10. * Class HookObserver
  11. * This abstract class implements Hook Observer Interface to build the base
  12. * for Hook Observer. This class have some public static method,
  13. * e.g for create Hook Observers
  14. */
  15. abstract class HookObserver implements HookObserverInterface
  16. {
  17. public $path;
  18. public $pluginName;
  19. /**
  20. * Construct method
  21. * Save the path of Hook Observer class implementation and
  22. * the plugin name where this class is included
  23. * @param string $path
  24. * @param string $pluginName
  25. */
  26. protected function __construct($path, $pluginName)
  27. {
  28. $this->path = $path;
  29. $this->pluginName = $pluginName;
  30. }
  31. /**
  32. * Return the singleton instance of Hook observer.
  33. * If Hook Management plugin is not enabled, will return NULL
  34. * @return HookObserver
  35. */
  36. public static function create()
  37. {
  38. static $result = null;
  39. if ($result) {
  40. return $result;
  41. } else {
  42. try {
  43. $class = get_called_class();
  44. return new $class;
  45. } catch (Exception $e) {
  46. return null;
  47. }
  48. }
  49. }
  50. /**
  51. * Return the path from the class, needed to store location or autoload later.
  52. * @return string
  53. */
  54. public function getPath()
  55. {
  56. return $this->path;
  57. }
  58. /**
  59. * Return the plugin name where is the Hook Observer.
  60. * @return string
  61. */
  62. public function getPluginName()
  63. {
  64. return $this->pluginName;
  65. }
  66. }