# 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 | 0.00% covered (danger) | 0.00% | 0 / 5 | CRAP | 19.51% covered (danger) | 19.51% | 8 / 41
ModuleManager | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 5 | 186.94 | 19.51% covered (danger) | 19.51% | 8 / 41
__construct | 0.00% covered (danger) | 0.00% | 0 / 1 | 5.26 | 57.14% covered (warning) | 57.14% | 8 / 14
loadModule | 0.00% covered (danger) | 0.00% | 0 / 1 | 12 | 0.00% covered (danger) | 0.00% | 0 / 7
listAvailableModules | 0.00% covered (danger) | 0.00% | 0 / 1 | 30 | 0.00% covered (danger) | 0.00% | 0 / 10
getModuleFromId | 0.00% covered (danger) | 0.00% | 0 / 1 | 12 | 0.00% covered (danger) | 0.00% | 0 / 5
__get | 0.00% covered (danger) | 0.00% | 0 / 1 | 12 | 0.00% covered (danger) | 0.00% | 0 / 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 | die ("out");
54 | $i->active = false;
55 | $i->save();
56 | }
57 | }
58 | $context->hookManager->loadHooks(\Entity\ModuleHook::getModules($ids));
59 | }
60 |
61 | /**
62 | * @param string $path path to module's main file
63 | * @param \Entity\Module $module module's database object to load
64 | * /core/models/Module.php
65 | * @return TRUE on success
66 | * Will try to load module located at $path.
67 | * This function will include the main.php file located in the module's directory
68 | * The file MUST return an AModule object to be considered as successfull
69 | * /core/tools/AModule.php
70 | **/
71 | private function loadModule($path, $module)
72 | {
73 | $mod = include_once($path);
74 | if (!$mod || !($mod instanceof \Tools\AModule))
75 | return false;
76 | $mod->setContext($this->context);
77 | $mod->setEntity($module);
78 | $this->modules[] = $mod;
79 | return true;
80 | }
81 |
82 | /*
83 | * TODO revoir tout
84 | * @return array(AModule)
85 | * Will load every modules, and return them.
86 | **/
87 | public function listAvailableModules()
88 | {
89 | $modulesRoot = $context->router->modulesPath;
90 | $result = array();
91 | $modules = scandir($modulesRoot, SCANDIR_SORT_NONE);
92 | foreach ($modules as $i)
93 | {
94 | $path = $modulesRoot.$i;
95 | if ($i == '.' || $i == '..' || !is_dir($path))
96 | continue;
97 | $this->loadModule($path);
98 | }
99 | }
100 |
101 | /**
102 | * @param int $id
103 | * @return AModule on success, FALSE on failure (no such ID / module not loaded)
104 | * Get the module identified with id ID
105 | **/
106 | public function getModuleFromId($id)
107 | {
108 | foreach ($this->modules as $i)
109 | {
110 | if ($i->entity->id == $id)
111 | return $i;
112 | }
113 | return FALSE;
114 | }
115 |
116 | /**
117 | * Getter
118 | **/
119 | public function __get($key)
120 | {
121 | switch ($key)
122 | {
123 | case "modules":
124 | return $this->modules; break;
125 | }
126 | if (substr($key, 0, 4) == "_id_")
127 | return $this->getModuleFromId((int) substr($key, 4));
128 | throw new \Exception("Cannot access attribute {$key}");
129 | }
130 | }
131 |