| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 75.00% covered (warning) | 75.00% | 6 / 8 | CRAP | 84.06% covered (warning) | 84.06% | 58 / 69
ModuleManager | 0.00% covered (danger) | 0.00% | 0 / 1 | 75.00% covered (warning) | 75.00% | 6 / 8 | 27.53 | 84.06% covered (warning) | 84.06% | 58 / 69
__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 | 4 | 100.00% covered (success) | 100.00% | 22 / 22
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
isInstalling | 100.00% covered (success) | 100.00% | 1 / 1 | 1 | 100.00% covered (success) | 100.00% | 1 / 1
__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 boolean $installing true if a module is currently installing
28 | **/
29 | private static $installing = false;
30 |
31 | /**
32 | * @var _id_{ID}
33 | * Get the module identified with id ID
34 | * ex: $context->moduleManager->_id_2 will return module with id 2
35 | **/
36 |
37 | /**
38 | * @param \Tool\Context $context
39 | * Load all active modules from database.
40 | * Enable hooks for these modules
41 | * Disable them if the module cannot be loaded
42 | **/
43 | public function __construct(&$context)
44 | {
45 | $this->context = $context;
46 | $modulesRoot = $context->router->modulesPath;
47 | $modules = \Entity\Module::getActivated();
48 | $ids = array();
49 | foreach ($modules as $i)
50 | {
51 | $modulePath = "{$modulesRoot}{$i->directory}/main.php";
52 | if (file_exists($modulePath) && $this->loadModule($modulePath, $i))
53 | {
54 | $ids[] = $i->id;
55 | }
56 | else
57 | {
58 | $i->active = false;
59 | $i->save();
60 | }
61 | }
62 | $context->hookManager->loadHooks(\Entity\ModuleHook::getModules($ids));
63 | }
64 |
65 | /**
66 | * @param string $path
67 | * @return \Tools\AModule loaded module (or false on error)
68 | **/
69 | private function loadModuleFile($path)
70 | {
71 | $mod = include_once($path);
72 | if (!$mod || !($mod instanceof \Tools\AModule))
73 | return false;
74 | $mod->setContext($this->context);
75 | return $mod;
76 | }
77 |
78 | /**
79 | * @param string $path path to module's main file
80 | * @param \Entity\Module $module module's database object to load
81 | * /core/models/Module.php
82 | * @return TRUE on success
83 | * Will try to load module located at $path.
84 | * This function will include the main.php file located in the module's directory
85 | * The file MUST return an AModule object to be considered as successfull
86 | * /core/tools/AModule.php
87 | **/
88 | private function loadModule($path, $module)
89 | {
90 | $mod = $this->loadModuleFile($path);
91 | if ($mod === false)
92 | return false;
93 | $mod->setEntity($module);
94 | $this->modules[] = $mod;
95 | return true;
96 | }
97 |
98 | /**
99 | * @param string $modulename
100 | * @return \tools\AModule|false on failure
101 | * Install the module located in {modulePath}/modulename/modulename.php
102 | **/
103 | public function install($modulename)
104 | {
105 | $entity = new \Entity\Module();
106 | $entity->name = $modulename;
107 | $entity->directory = $modulename;
108 | $entity->active = true;
109 | $module = $this->loadModuleFile($this->context->router->modulesPath.$modulename.'/main.php');
110 | if ($module === false)
111 | return false;
112 | $module->setEntity($entity);
113 | $entity->name = $module->getName();
114 | $entity->description = $module->getDescription();
115 | $entity->save();
116 | self::$installing = true;
117 | if ($module->install() == false)
118 | {
119 | self::$installing = false;
120 | $hooks = \Entity\ModuleHook::getModules($entity->id);
121 | foreach ($hooks as $i)
122 | $i->delete();
123 | $entity->delete();
124 | return false;
125 | }
126 | self::$installing = false;
127 | return $module;
128 | }
129 |
130 | /*
131 | * TODO revoir tout
132 | * @return array(AModule)
133 | * Will load every modules, and return them.
134 | **/
135 | public function listAvailableModules()
136 | {
137 | $modulesRoot = $context->router->modulesPath;
138 | $result = array();
139 | $modules = scandir($modulesRoot, SCANDIR_SORT_NONE);
140 | foreach ($modules as $i)
141 | {
142 | $path = $modulesRoot.$i;
143 | if ($i == '.' || $i == '..' || !is_dir($path))
144 | continue;
145 | $this->loadModule($path);
146 | }
147 | }
148 |
149 | /**
150 | * @param int $id
151 | * @return AModule on success, FALSE on failure (no such ID / module not loaded)
152 | * Get the module identified with id ID
153 | **/
154 | public function getModuleFromId($id)
155 | {
156 | foreach ($this->modules as $i)
157 | {
158 | if ($i->entity->id == $id)
159 | return $i;
160 | }
161 | return FALSE;
162 | }
163 |
164 | /**
165 | * @return true if a module is currently installing
166 | **/
167 | public static function isInstalling()
168 | { return self::$installing; }
169 |
170 | /**
171 | * Getter
172 | **/
173 | public function __get($key)
174 | {
175 | switch ($key)
176 | {
177 | case "modules":
178 | return $this->modules; break;
179 | }
180 | if (substr($key, 0, 4) == "_id_")
181 | return $this->getModuleFromId((int) substr($key, 4));
182 | throw new \Exception("Cannot access attribute {$key}");
183 | }
184 | }
185 |