| 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

1 | <?php
2 |
3 | namespace Tools;
4 |
5 | /**
6 | * Allow user to manage hooks
7 | **/
8 | class Hooks
9 | {
10 | /**
11 | * @var \Tools\Context $context
12 | * /core/tools/Context.php
13 | * Contains website's informations
14 | **/
15 | private $context;
16 |
17 | /**
18 | * @var array( string: hookName => module Id )
19 | **/
20 | private $hooks;
21 |
22 | /**
23 | * @var SplStack $currentHook
24 | * Contains current trigerred event
25 | **/
26 | private $currentHook;
27 |
28 | /**
29 | * @var \Tools\HookEvent $currentHook
30 | * get current hook event
31 | **/
32 |
33 | /**
34 | * ctor. Initialize context for having them passed to hook as parameter
35 | **/
36 | public function __construct(&$context)
37 | {
38 | $this->context = $context;
39 | $this->currentHook = array();
40 | $this->hooks = array();
41 | }
42 |
43 | /**
44 | * @param \Tool\AModule $module
45 | * @param string $hookname
46 | * Attach module $module to $hookName
47 | * Can only be called while installing the module.
48 | * When fired, the AModule::doAction($hookName, $context) function will be called.
49 | **/
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 |
64 | /**
65 | * @param string $hookName
66 | * @return number of modules successfully reached
67 | * fire the hook hookName
68 | * call the AModule::doAction($hookEvent) function for each attached modules
69 | **/
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 |
89 | /**
90 | * @param string $hookName
91 | * @return boolean
92 | * Check if $hookName is treating
93 | **/
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 |
102 | /**
103 | * @param array(\Entity\ModuleHook) $module_hookEntities entities to load
104 | * Reload hooks from entities
105 | **/
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 |
113 | /**
114 | * Getter
115 | **/
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 |