ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables
ModuleManager.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Tools;
4 
10 {
16  private $context;
17 
24  private $modules = array();
25 
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 
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 
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 
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 
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 
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 
static getActivated()
Definition: Module.php:22
loadModule($path, $module)
__construct(&$context)
install($modulename)