# Code Coverage for /win/work/Project/ecom/core/tools/ModuleManager.php

| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 71.43% covered (warning) | 71.43% | 5 / 7 | CRAP | 81.67% covered (warning) | 81.67% | 49 / 60
ModuleManager | 0.00% covered (danger) | 0.00% | 0 / 1 | 71.43% covered (warning) | 71.43% | 5 / 7 | 26.26 | 81.67% covered (warning) | 81.67% | 49 / 60
__construct | 100.00% covered (success) | 100.00% | 1 / 1 | 4 | 100.00% covered (success) | 100.00% | 15 / 15
loadModuleFile | 100.00% covered (success) | 100.00% | 1 / 1 | 3 | 100.00% covered (success) | 100.00% | 5 / 5
loadModule | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 6 / 6
install | 100.00% covered (success) | 100.00% | 1 / 1 | 3 | 100.00% covered (success) | 100.00% | 14 / 14
listAvailableModules | 0.00% covered (danger) | 0.00% | 0 / 1 | 30 | 0.00% covered (danger) | 0.00% | 0 / 10
getModuleFromId | 100.00% covered (success) | 100.00% | 1 / 1 | 3 | 100.00% covered (success) | 100.00% | 5 / 5
__get | 0.00% covered (danger) | 0.00% | 0 / 1 | 3.07 | 80.00% covered (warning) | 80.00% | 4 / 5

1 | <?php
2 |
3 | namespace Tools;
4 |
5 | /**
6 | * Container all modules.
7 | * Allow module managment
8 | **/
9 | class ModuleManager
10 | {
11 | /**
12 | * @var \Tools\Context $context
13 | * /core/tools/Context.php
14 | * Contains website's informations
15 | **/
16 | private $context;
17 |
18 | /**
19 | * @var array(\Tool\AModule) $modules
20 | * /core/tools/AModule.php
21 | * Contains all loaded modules
22 | * Can be accessed read-only via $instance->modules
23 | **/
24 | private $modules = array();
25 |
26 | /**
27 | * @var _id_{ID}
28 | * Get the module identified with id ID
29 | * ex: $context->moduleManager->_id_2 will return module with id 2
30 | **/
31 |
32 | /**
33 | * @param \Tool\Context $context
34 | * Load all active modules from database.
35 | * Enable hooks for these modules
36 | * Disable them if the module cannot be loaded
37 | **/
38 | public function __construct(&$context)
39 | {
40 | $this->context = $context;
41 | $modulesRoot = $context->router->modulesPath;
42 | $modules = \Entity\Module::getActivated();
43 | $ids = array();
44 | foreach ($modules as $i)
45 | {
46 | $modulePath = "{$modulesRoot}{$i->directory}/main.php";
47 | if (file_exists($modulePath) && $this->loadModule($modulePath, $i))
48 | {
49 | $ids[] = $i->id;
50 | }
51 | else
52 | {
53 | $i->active = false;
54 | $i->save();
55 | }
56 | }
57 | $context->hookManager->loadHooks(\Entity\ModuleHook::getModules($ids));
58 | }
59 |
60 | /**
61 | * @param string $path
62 | * @return \Tools\AModule loaded module (or false on error)
63 | **/
64 | private function loadModuleFile($path)
65 | {
66 | $mod = include_once($path);
67 | if (!$mod || !($mod instanceof \Tools\AModule))
68 | return false;
69 | $mod->setContext($this->context);
70 | return $mod;
71 | }
72 |
73 | /**
74 | * @param string $path path to module's main file
75 | * @param \Entity\Module $module module's database object to load
76 | * /core/models/Module.php
77 | * @return TRUE on success
78 | * Will try to load module located at $path.
79 | * This function will include the main.php file located in the module's directory
80 | * The file MUST return an AModule object to be considered as successfull
81 | * /core/tools/AModule.php
82 | **/
83 | private function loadModule($path, $module)
84 | {
85 | $mod = $this->loadModuleFile($path);
86 | if ($mod === false)
87 | return false;
88 | $mod->setEntity($module);
89 | $this->modules[] = $mod;
90 | return true;
91 | }
92 |
93 | /**
94 | * @param string $modulename
95 | * @return \tools\AModule|false on failure
96 | * Install the module located in {modulePath}/modulename/modulename.php
97 | **/
98 | public function install($modulename)
99 | {
100 | $entity = new \Entity\Module();
101 | $entity->name = $modulename;
102 | $entity->directory = $modulename;
103 | $entity->active = true;
104 | $module = $this->loadModuleFile($this->context->router->modulesPath.$modulename.'/main.php');
105 | if ($module === false)
106 | return false;
107 | $entity->name = $module->getName();
108 | $entity->description = $module->getDescription();
109 | if ($module->install() == false)
110 | return false;
111 | $entity->save();
112 | $module->setEntity($entity);
113 | return $module;
114 | }
115 |
116 | /*
117 | * TODO revoir tout
118 | * @return array(AModule)
119 | * Will load every modules, and return them.
120 | **/
121 | public function listAvailableModules()
122 | {
123 | $modulesRoot = $context->router->modulesPath;
124 | $result = array();
125 | $modules = scandir($modulesRoot, SCANDIR_SORT_NONE);
126 | foreach ($modules as $i)
127 | {
128 | $path = $modulesRoot.$i;
129 | if ($i == '.' || $i == '..' || !is_dir($path))
130 | continue;
131 | $this->loadModule($path);
132 | }
133 | }
134 |
135 | /**
136 | * @param int $id
137 | * @return AModule on success, FALSE on failure (no such ID / module not loaded)
138 | * Get the module identified with id ID
139 | **/
140 | public function getModuleFromId($id)
141 | {
142 | foreach ($this->modules as $i)
143 | {
144 | if ($i->entity->id == $id)
145 | return $i;
146 | }
147 | return FALSE;
148 | }
149 |
150 | /**
151 | * Getter
152 | **/
153 | public function __get($key)
154 | {
155 | switch ($key)
156 | {
157 | case "modules":
158 | return $this->modules; break;
159 | }
160 | if (substr($key, 0, 4) == "_id_")
161 | return $this->getModuleFromId((int) substr($key, 4));
162 | throw new \Exception("Cannot access attribute {$key}");
163 | }
164 | }
165 |