ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables
Hooks.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Tools;
4 
8 class Hooks
9 {
15  private $context;
16 
20  private $hooks;
21 
26  private $currentHook;
27 
36  public function __construct(&$context)
37  {
38  $this->context = $context;
39  $this->currentHook = array();
40  }
41 
49  public function register($module, $hookName)
50  {
51  if (!\Tools\ModuleManager::isInstalling())
52  throw new \Exception("You can only register hooks while installing");
53  //TODO@2
54  }
55 
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 
86  public function isInHook($hookName)
87  {
88  foreach ($this->currentHook as $i)
89  if ($i->hookName == $hookName)
90  return true;
91  return false;
92  }
93 
98  public function loadHooks($hookEntities)
99  {
100  $this->hooks = array();
101  foreach ($hookEntities as $i)
102  $this->hooks[$i->hookName][] = (int) $i->module_id;
103  }
104 
108  public function __get($key)
109  {
110  switch ($key)
111  {
112  case "currentHook": return end($this->currentHook);
113  }
114  throw new \Exception("Cannot access attribute {$key}");
115  }
116 }
117 
$currentHook
Definition: Hooks.php:26
__construct(&$context)
Definition: Hooks.php:36
isInHook($hookName)
Definition: Hooks.php:86
__get($key)
Definition: Hooks.php:108
loadHooks($hookEntities)
Definition: Hooks.php:98
trigger($hookName, $params=null)
Definition: Hooks.php:62