All Data Structures Namespaces Files Functions Variables
ModuleManager.php
Go to the documentation of this file.
<?php
namespace Tools;
class ModuleManager
{
private $context;
private $modules = array();
private static $installing = false;
public function __construct(&$context)
{
$this->context = $context;
$modulesRoot = $context->router->modulesPath;
$modules = \Entity\Module::getActivated();
$ids = array();
foreach ($modules as $i)
{
$modulePath = "{$modulesRoot}{$i->directory}/main.php";
if (file_exists($modulePath) && $this->loadModule($modulePath, $i))
{
$ids[] = $i->id;
}
else
{
$i->active = false;
$i->save();
}
}
$context->hookManager->loadHooks(\Entity\ModuleHook::getModules($ids));
}
private function loadModuleFile($path)
{
$mod = include_once($path);
if (!$mod || !($mod instanceof \Tools\AModule))
return false;
$mod->setContext($this->context);
return $mod;
}
private function loadModule($path, $module)
{
$mod = $this->loadModuleFile($path);
if ($mod === false)
return false;
$mod->setEntity($module);
$this->modules[] = $mod;
return true;
}
public function install($modulename)
{
$entity = new \Entity\Module();
$entity->name = $modulename;
$entity->directory = $modulename;
$entity->active = true;
$module = $this->loadModuleFile($this->context->router->modulesPath.$modulename.'/main.php');
if ($module === false)
return false;
$module->setEntity($entity);
$entity->name = $module->getName();
$entity->description = $module->getDescription();
$entity->save();
self::$installing = true;
if ($module->install() == false)
{
self::$installing = false;
$hooks = \Entity\ModuleHook::getModules($entity->id);
foreach ($hooks as $i)
$i->delete();
$entity->delete();
return false;
}
self::$installing = false;
return $module;
}
/*
* TODO revoir tout
* @return array(AModule)
* Will load every modules, and return them.
**/
public function listAvailableModules()
{
$modulesRoot = $context->router->modulesPath;
$result = array();
$modules = scandir($modulesRoot, SCANDIR_SORT_NONE);
foreach ($modules as $i)
{
$path = $modulesRoot.$i;
if ($i == '.' || $i == '..' || !is_dir($path))
continue;
$this->loadModule($path);
}
}
public function getModuleFromId($id)
{
foreach ($this->modules as $i)
{
if ($i->entity->id == $id)
return $i;
}
return FALSE;
}
public static function isInstalling()
{ return self::$installing; }
public function __get($key)
{
switch ($key)
{
case "modules":
return $this->modules; break;
}
if (substr($key, 0, 4) == "_id_")
return $this->getModuleFromId((int) substr($key, 4));
throw new \Exception("Cannot access attribute {$key}");
}
}
Tools\ModuleManager\$context
$context
Definition: ModuleManager.php:16
Entity\Module\getActivated
static getActivated()
Definition: Module.php:22
Entity\ModuleHook\getModules
static getModules($moduleIds)
Definition: ModuleHook.php:23
Entity
Definition: Address.php:3
Tools\ModuleManager
Definition: ModuleManager.php:9
Tools\ModuleManager\loadModuleFile
loadModuleFile($path)
Definition: ModuleManager.php:69
Tools\ModuleManager\listAvailableModules
listAvailableModules()
Definition: ModuleManager.php:135
Tools\ModuleManager\loadModule
loadModule($path, $module)
Definition: ModuleManager.php:88
Tools\ModuleManager\__construct
__construct(&$context)
Definition: ModuleManager.php:43
Tools\AModule
Definition: AModule.php:9
Tools\ModuleManager\__get
__get($key)
Definition: ModuleManager.php:173
Tools
Definition: AController.php:3
Tools\ModuleManager\getModuleFromId
getModuleFromId($id)
Definition: ModuleManager.php:154
Tools\ModuleManager\install
install($modulename)
Definition: ModuleManager.php:103
Tools\ModuleManager\isInstalling
static isInstalling()
Definition: ModuleManager.php:167