Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
CRAP
84.62% covered (warning)
84.62%
33 / 39
Hooks
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
15.82
84.62% covered (warning)
84.62%
33 / 39
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 register
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
11 / 11
 trigger
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
14 / 14
 isInHook
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 4
 loadHooks
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 __get
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 2
<?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();
        $this->hooks = 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");
        if (!isset($this->hooks[$hookName]))
            $this->hooks[$hookName] = array();
        $this->hooks[$hookName][] = $module->entity->id;
        $moduleHook = new \Entity\ModuleHook();
        $moduleHook->module_id = $module->entity->id;
        $moduleHook->hookName = $hookName;
        $moduleHook->hookPosition = 10;
        $moduleHook->save();
    }
    /**
     * @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}");
    }
}