Hooks.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. }
  35. /**
  36. * @param \Tool\AModule $module
  37. * @param string $hookname
  38. * Attach module $module to $hookName
  39. * Can only be called while installing the module.
  40. * When fired, the AModule::doAction($hookName, $context) function will be called.
  41. **/
  42. public function register($module, $hookName)
  43. {
  44. if (!\Tools\ModuleManager::isInstalling())
  45. throw new \Exception("You can only register hooks while installing");
  46. //TODO@2
  47. }
  48. /**
  49. * @param string $hookName
  50. * @return number of modules successfully reached
  51. * fire the hook hookName
  52. * call the AModule::doAction($hookEvent) function for each attached modules
  53. **/
  54. public function trigger($hookName, $params =null)
  55. {
  56. if (empty($this->hooks[$hookName]))
  57. return 0;
  58. $hookEvent = new HookEvent($hookName, $this->context, $params);
  59. array_push($this->currentHook, $hookEvent);
  60. $result = 0;
  61. foreach ($this->hooks[$hookName] as $module_id)
  62. {
  63. $module = $this->context->moduleManager->getModuleFromId($module_id);
  64. if (!$module)
  65. continue;
  66. $module->doAction($hookEvent);
  67. $result++;
  68. }
  69. array_pop($this->currentHook);
  70. return $result;
  71. }
  72. /**
  73. * @param string $hookName
  74. * @return boolean
  75. * Check if $hookName is treating
  76. **/
  77. public function isInHook($hookName)
  78. {
  79. foreach ($this->currentHook as $i)
  80. if ($i->hookName == $hookName)
  81. return true;
  82. return false;
  83. }
  84. /**
  85. * @param array(\Entity\ModuleHook) $module_hookEntities entities to load
  86. * Reload hooks from entities
  87. **/
  88. public function loadHooks($hookEntities)
  89. {
  90. $this->hooks = array();
  91. foreach ($hookEntities as $i)
  92. $this->hooks[$i->hookName][] = (int) $i->module_id;
  93. }
  94. public function __get($key)
  95. {
  96. switch ($key)
  97. {
  98. case "currentHook": return end($this->currentHook);
  99. }
  100. throw new \Exception("Cannot access attribute {$key}");
  101. }
  102. }