All Data Structures Namespaces Files Functions Variables
Hooks.php
Go to the documentation of this file.
<?php
namespace Tools;
class Hooks
{
private $context;
private $hooks;
private $currentHook;
public function __construct(&$context)
{
$this->context = $context;
$this->currentHook = array();
$this->hooks = array();
}
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();
}
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;
}
public function isInHook($hookName)
{
foreach ($this->currentHook as $i)
if ($i->hookName == $hookName)
return true;
return false;
}
public function loadHooks($hookEntities)
{
$this->hooks = array();
foreach ($hookEntities as $i)
$this->hooks[$i->hookName][] = (int) $i->module_id;
}
public function __get($key)
{
switch ($key)
{
case "currentHook": return end($this->currentHook);
}
throw new \Exception("Cannot access attribute {$key}");
}
}
Tools\Hooks\$currentHook
$currentHook
Definition: Hooks.php:26
Tools\Hooks\__construct
__construct(&$context)
Definition: Hooks.php:36
Tools\Hooks\$hooks
$hooks
Definition: Hooks.php:20
Tools\Hooks\isInHook
isInHook($hookName)
Definition: Hooks.php:94
Exception
Definition: Error404.php:3
Tools\Hooks\$context
$context
Definition: Hooks.php:15
Tools\Hooks\__get
__get($key)
Definition: Hooks.php:116
Tools\HookEvent
Definition: HookEvent.php:9
Tools\Hooks
Definition: Hooks.php:8
Tools\Hooks\loadHooks
loadHooks($hookEntities)
Definition: Hooks.php:106
Tools
Definition: AController.php:3
Tools\Hooks\trigger
trigger($hookName, $params=null)
Definition: Hooks.php:70