Hooks.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Tools;
  3. /**
  4. * Allow user to manage hooks
  5. **/
  6. class Hooks
  7. {
  8. /**
  9. * @var \Tools\Context $context
  10. * /core/tools/Context.php
  11. * Contains website's informations
  12. **/
  13. private $context;
  14. /**
  15. * @var array( string: hookName => module Id )
  16. **/
  17. private $hooks;
  18. /**
  19. * @var SplStack $currentHook
  20. * Contains current trigerred event
  21. **/
  22. private $currentHook;
  23. /**
  24. * @var \Tools\HookEvent $currentHook
  25. * get current hook event
  26. **/
  27. /**
  28. * ctor. Initialize context for having them passed to hook as parameter
  29. **/
  30. public function __construct(&$context)
  31. {
  32. $this->context = $context;
  33. $this->currentHook = array();
  34. $this->hooks = array();
  35. }
  36. /**
  37. * @param \Tool\AModule $module
  38. * @param string $hookname
  39. * Attach module $module to $hookName
  40. * Can only be called while installing the module.
  41. * When fired, the AModule::doAction($hookName, $context) function will be called.
  42. **/
  43. public function register($module, $hookName)
  44. {
  45. if (!\Tools\ModuleManager::isInstalling())
  46. throw new \Exception("You can only register hooks while installing");
  47. if (!isset($this->hooks[$hookName]))
  48. $this->hooks[$hookName] = array();
  49. $this->hooks[$hookName][] = $module->entity->id;
  50. $moduleHook = new \Entity\ModuleHook();
  51. $moduleHook->module_id = $module->entity->id;
  52. $moduleHook->hookName = $hookName;
  53. $moduleHook->hookPosition = 10;
  54. $moduleHook->save();
  55. }
  56. /**
  57. * @param string $hookName
  58. * @return number of modules successfully reached
  59. * fire the hook hookName
  60. * call the AModule::doAction($hookEvent) function for each attached modules
  61. **/
  62. public function trigger($hookName, $params =null)
  63. {
  64. if (empty($this->hooks[$hookName]))
  65. return 0;
  66. $hookEvent = new HookEvent($hookName, $this->context, $params);
  67. array_push($this->currentHook, $hookEvent);
  68. $result = 0;
  69. foreach ($this->hooks[$hookName] as $module_id)
  70. {
  71. $module = $this->context->moduleManager->getModuleFromId($module_id);
  72. if (!$module)
  73. continue;
  74. $module->doAction($hookEvent);
  75. $result++;
  76. }
  77. array_pop($this->currentHook);
  78. return $result;
  79. }
  80. /**
  81. * @param string $hookName
  82. * @return boolean
  83. * Check if $hookName is treating
  84. **/
  85. public function isInHook($hookName)
  86. {
  87. foreach ($this->currentHook as $i)
  88. if ($i->hookName == $hookName)
  89. return true;
  90. return false;
  91. }
  92. /**
  93. * @param array(\Entity\ModuleHook) $module_hookEntities entities to load
  94. * Reload hooks from entities
  95. **/
  96. public function loadHooks($hookEntities)
  97. {
  98. $this->hooks = array();
  99. foreach ($hookEntities as $i)
  100. $this->hooks[$i->hookName][] = (int) $i->module_id;
  101. }
  102. /**
  103. * Getter
  104. **/
  105. public function __get($key)
  106. {
  107. switch ($key)
  108. {
  109. case "currentHook": return end($this->currentHook);
  110. }
  111. throw new \Exception("Cannot access attribute {$key}");
  112. }
  113. }