219 lines
5.8 KiB
PHP
219 lines
5.8 KiB
PHP
|
<?php if( !defined('__ROOT__') ) define('__ROOT__', dirname(dirname(__FILE__)) );
|
||
|
|
||
|
|
||
|
class Exporter{
|
||
|
|
||
|
private $modules; // will contain modules (config)
|
||
|
|
||
|
public static function config_path(){ return __ROOT__.'/exporter/modules.json'; }
|
||
|
|
||
|
/* CONSTRUCTOR -> LOADS CONFIG
|
||
|
*
|
||
|
*/
|
||
|
public function __construct(){
|
||
|
|
||
|
$this->modules = json_decode( file_get_contents(self::config_path()), true);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/* RETURNS AVAILABLE MODULE LIST
|
||
|
*
|
||
|
* @return modules<Array> Set containing modules and their versions
|
||
|
*
|
||
|
*/
|
||
|
public function available(){
|
||
|
$modules = [];
|
||
|
|
||
|
foreach($this->modules['available'] as $module=>$versions){
|
||
|
$modules[$module] = [];
|
||
|
|
||
|
foreach($versions as $version=>$dependencies)
|
||
|
$modules[$module][] = $version;
|
||
|
}
|
||
|
|
||
|
return $modules;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* ENABLES A MODULE'S VERSION
|
||
|
*
|
||
|
* @pModule<String> Module's name
|
||
|
* @pVersion<String> Module's version
|
||
|
* @pDep<Boolean> TRUE if dependency
|
||
|
*
|
||
|
* @return err_msg<String> Error message | TRUE (if all is ok)
|
||
|
*
|
||
|
*/
|
||
|
public function enable($pModule=null, $pVersion=null, $pDep=false){
|
||
|
|
||
|
/* [1] Module management
|
||
|
=========================================================*/
|
||
|
/* (1) If @module not given, exit */
|
||
|
if( is_null($pModule) )
|
||
|
return "Missing @moduleName.\n";
|
||
|
|
||
|
/* (2) Checking module existence */
|
||
|
if( !isset($this->modules['available'][$pModule]) )
|
||
|
return "Module `$pModule` unknown.\n";
|
||
|
|
||
|
/* (3) Set module name & content */
|
||
|
$mname = $pModule;
|
||
|
$module = $this->modules['available'][$mname];
|
||
|
|
||
|
|
||
|
/* [2] Version management
|
||
|
=========================================================*/
|
||
|
/* (1) Set default version name & content */
|
||
|
$vname = array_slice(array_keys($module), -1)[0];
|
||
|
$version = $module[$vname];
|
||
|
|
||
|
/* (2) If wrong version given, set to default */
|
||
|
if( is_null($pVersion) || !isset($module[$pVersion]) )
|
||
|
echo "chosing latest $vname version.\n";
|
||
|
|
||
|
/* (2) Else, we get given @version */
|
||
|
else
|
||
|
$version = $module[( $vname = $pVersion )];
|
||
|
|
||
|
|
||
|
|
||
|
/* [3] Enables module & version
|
||
|
=========================================================*/
|
||
|
/* (1) If module disabled -> enables it */
|
||
|
if( !isset($this->modules['enabled'][$mname]) )
|
||
|
$this->modules['enabled'][$mname] = '0';
|
||
|
|
||
|
/* (2) If not dependency or version lower -> Set version */
|
||
|
if( !$pDep || $this->lower($module, $this->modules['enabled'][$mname], $vname) ){
|
||
|
|
||
|
if( $pDep ) echo "- $mname-$vname ($vname+ required)\n";
|
||
|
|
||
|
$this->modules['enabled'][$mname] = $vname;
|
||
|
|
||
|
} else if( $pDep ) echo "- $mname-".$this->modules['enabled'][$mname]." ($vname+ required)\n";
|
||
|
|
||
|
|
||
|
/* [4] Loading dependencies
|
||
|
=========================================================*/
|
||
|
/* (1) Loading each dependency */
|
||
|
if( count($version) > 0 ){
|
||
|
|
||
|
echo "dependencies:\n";
|
||
|
|
||
|
foreach($version as $depMod=>$depVer)
|
||
|
$enabled = $this->enable($depMod, $depVer, true);
|
||
|
}
|
||
|
|
||
|
|
||
|
/* [5] Storing data
|
||
|
=========================================================*/
|
||
|
$this->store();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* DISABLES A MODULE'S VERSION
|
||
|
*
|
||
|
* @pModule<String> Module's name
|
||
|
*
|
||
|
* @return err_msg<String> Error message || TRUE if success
|
||
|
*
|
||
|
*/
|
||
|
public function disable($pModule=null){
|
||
|
/* [1] Module management (existence)
|
||
|
=========================================================*/
|
||
|
/* (1) If @module not given, exit */
|
||
|
if( is_null($pModule) )
|
||
|
return "Missing @moduleName.\n";
|
||
|
|
||
|
/* (2) Checking if module is enabled */
|
||
|
$enabled_modules = array_keys($this->modules['enabled']);
|
||
|
if( !in_array($pModule, $enabled_modules) )
|
||
|
return "Module `$pModule` not enabled.\n";
|
||
|
|
||
|
/* (3) Set module name & content */
|
||
|
$mname = $pModule;
|
||
|
$module = $this->modules['enabled'][$mname];
|
||
|
|
||
|
|
||
|
/* [2] Dependency verification (not needed)
|
||
|
=========================================================*/
|
||
|
$dependency = false; // if it's a dependency of another enabled module
|
||
|
$callers = []; // list of enabled modules that needs current one
|
||
|
|
||
|
/* (1) For each enabled module (excepted itself) */
|
||
|
foreach($enabled_modules as $module){
|
||
|
|
||
|
// except current module
|
||
|
if( $module == $mname ) continue;
|
||
|
|
||
|
/* (2) For each version of its module (and its dependencies) */
|
||
|
foreach($this->modules['available'][$module] as $version=>$dependencies){
|
||
|
|
||
|
// if not the current enabled module's version
|
||
|
if( $version != $this->modules['enabled'][$module] )
|
||
|
continue;
|
||
|
|
||
|
/* (3) If current module in dependencies -> can't disable */
|
||
|
isset($dependencies[$mname]) && ($dependency = true) && ($callers[] = "`$module`");
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/* (4) If a dependency, explain */
|
||
|
if( $dependency )
|
||
|
return "Cannot disable module $mname because ".implode(',',$callers)." needs it.\n";
|
||
|
|
||
|
|
||
|
/* [3] Disabling module
|
||
|
=========================================================*/
|
||
|
/* (1) Disabling module */
|
||
|
unset($this->modules['enabled'][$mname]);
|
||
|
|
||
|
/* (2) Storing changes */
|
||
|
$this->store();
|
||
|
|
||
|
/* (3) Return success */
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* CHECKS IF @pFirst IS LOWER THAN @pSecond VERSION
|
||
|
*
|
||
|
* @pModule<Array> Module's content
|
||
|
* @pFirst<String> First version
|
||
|
* @pSecond<String> Second version
|
||
|
*
|
||
|
* @return lower<Boolean> Returns if @pFirst is lower
|
||
|
*
|
||
|
* Note: Supposing that @pFirst and @pSecond are keys of @pModule (no check)
|
||
|
*
|
||
|
*/
|
||
|
public function lower($pModule, $pFirst, $pSecond){
|
||
|
/* (1) Get @pModule keys */
|
||
|
$keys = array_keys($pModule);
|
||
|
|
||
|
/* (1) Get @pFirst index */
|
||
|
$pf_index = array_search($pFirst, $keys);
|
||
|
|
||
|
/* (2) Get @pSecond index */
|
||
|
$ps_index = array_search($pSecond, $keys);
|
||
|
|
||
|
return $pf_index < $ps_index;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* STORES DATA
|
||
|
*
|
||
|
*/
|
||
|
public function store(){
|
||
|
file_put_contents(self::config_path(), json_encode($this->modules, JSON_PRETTY_PRINT));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
?>
|