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  $this->hooks = array();
41  }
42 
50  public function register($module, $hookName)
51  {
52  if (!\Tools\ModuleManager::isInstalling())
53  throw new \Exception("You can only register hooks while installing");
54  if (!isset($this->hooks[$hookName]))
55  $this->hooks[$hookName] = array();
56  $this->hooks[$hookName][] = $module->entity->id;
57  $moduleHook = new \Entity\ModuleHook();
58  $moduleHook->module_id = $module->entity->id;
59  $moduleHook->hookName = $hookName;
60  $moduleHook->hookPosition = 10;
61  $moduleHook->save();
62  }
63 
70  public function trigger($hookName, $params =null)
71  {
72  if (empty($this->hooks[$hookName]))
73  return 0;
74  $hookEvent = new HookEvent($hookName, $this->context, $params);
75  array_push($this->currentHook, $hookEvent);
76  $result = 0;
77  foreach ($this->hooks[$hookName] as $module_id)
78  {
79  $module = $this->context->moduleManager->getModuleFromId($module_id);
80  if (!$module)
81  continue;
82  $module->doAction($hookEvent);
83  $result++;
84  }
85  array_pop($this->currentHook);
86  return $result;
87  }
88 
94  public function isInHook($hookName)
95  {
96  foreach ($this->currentHook as $i)
97  if ($i->hookName == $hookName)
98  return true;
99  return false;
100  }
101 
106  public function loadHooks($hookEntities)
107  {
108  $this->hooks = array();
109  foreach ($hookEntities as $i)
110  $this->hooks[$i->hookName][] = (int) $i->module_id;
111  }
112 
116  public function __get($key)
117  {
118  switch ($key)
119  {
120  case "currentHook": return end($this->currentHook);
121  }
122  throw new \Exception("Cannot access attribute {$key}");
123  }
124 }
125 
$currentHook
Definition: Hooks.php:26
__construct(&$context)
Definition: Hooks.php:36
isInHook($hookName)
Definition: Hooks.php:94
__get($key)
Definition: Hooks.php:116
loadHooks($hookEntities)
Definition: Hooks.php:106
trigger($hookName, $params=null)
Definition: Hooks.php:70