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 
29  private static $installing = false;
30 
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 
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 
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 
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  $entity->name = $module->getName();
113  $entity->description = $module->getDescription();
114  $entity->save();
115  $module->setEntity($entity);
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 
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 
167  public static function isInstalling()
168  { return self::$installing; }
169 
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 
static getActivated()
Definition: Module.php:22
static getModules($moduleIds)
Definition: ModuleHook.php:23
loadModule($path, $module)
__construct(&$context)
install($modulename)