| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace Tools;
- /**
- * Allow user to manage hooks
- **/
- class Hooks
- {
- /**
- * @var \Tools\Context $context
- * /core/tools/Context.php
- * Contains website's informations
- **/
- private $context;
- /**
- * @var array( string: hookName => module Id )
- **/
- private $hooks;
- /**
- * @var SplStack $currentHook
- * Contains current trigerred event
- **/
- private $currentHook;
- /**
- * @var \Tools\HookEvent $currentHook
- * get current hook event
- **/
- /**
- * ctor. Initialize context for having them passed to hook as parameter
- **/
- public function __construct(&$context)
- {
- $this->context = $context;
- $this->currentHook = array();
- }
- /**
- * @param \Tool\AModule $module
- * @param string $hookname
- * Attach module $module to $hookName
- * Can only be called while installing the module.
- * When fired, the AModule::doAction($hookName, $context) function will be called.
- **/
- public function register($module, $hookName)
- {
- if (!\Tools\ModuleManager::isInstalling())
- throw new \Exception("You can only register hooks while installing");
- //TODO@2
- }
- /**
- * @param string $hookName
- * @return number of modules successfully reached
- * fire the hook hookName
- * call the AModule::doAction($hookEvent) function for each attached modules
- **/
- public function trigger($hookName, $params =null)
- {
- if (empty($this->hooks[$hookName]))
- return 0;
- $hookEvent = new HookEvent($hookName, $this->context, $params);
- array_push($this->currentHook, $hookEvent);
- $result = 0;
- foreach ($this->hooks[$hookName] as $module_id)
- {
- $module = $this->context->moduleManager->getModuleFromId($module_id);
- if (!$module)
- continue;
- $module->doAction($hookEvent);
- $result++;
- }
- array_pop($this->currentHook);
- return $result;
- }
- /**
- * @param string $hookName
- * @return boolean
- * Check if $hookName is treating
- **/
- public function isInHook($hookName)
- {
- foreach ($this->currentHook as $i)
- if ($i->hookName == $hookName)
- return true;
- return false;
- }
- /**
- * @param array(\Entity\ModuleHook) $module_hookEntities entities to load
- * Reload hooks from entities
- **/
- public function loadHooks($hookEntities)
- {
- $this->hooks = array();
- foreach ($hookEntities as $i)
- $this->hooks[$i->hookName][] = (int) $i->module_id;
- }
- /**
- * Getter
- **/
- public function __get($key)
- {
- switch ($key)
- {
- case "currentHook": return end($this->currentHook);
- }
- throw new \Exception("Cannot access attribute {$key}");
- }
- }
|