2016-10-19 08:16:21 +00:00
|
|
|
<?php if( !defined('__ROOT__') ) define('__ROOT__', dirname(dirname(__FILE__)) );
|
|
|
|
|
2016-12-05 13:42:38 +00:00
|
|
|
require_once 'Builder.php';
|
|
|
|
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
class Exporter{
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
private $packages; // will contain packages (config)
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
public static function config_path(){ return __ROOT__.'/exporter/packages.json'; }
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 13:42:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-19 08:16:21 +00:00
|
|
|
/* CONSTRUCTOR -> LOADS CONFIG
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct(){
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
$this->packages = json_decode( file_get_contents(self::config_path()), true);
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 13:42:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-05 13:49:51 +00:00
|
|
|
/* REMOVES ENABLED MODULES
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function init(){
|
2016-12-05 18:54:19 +00:00
|
|
|
$this->packages['installed'] = [];
|
2016-12-05 13:49:51 +00:00
|
|
|
$this->store();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 08:16:21 +00:00
|
|
|
/* RETURNS AVAILABLE MODULE LIST
|
|
|
|
*
|
2016-12-05 18:54:19 +00:00
|
|
|
* @return packages<Array> Set containing packages and their versions
|
2016-10-19 08:16:21 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function available(){
|
2016-12-05 18:54:19 +00:00
|
|
|
$packages = [];
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
foreach($this->packages['available'] as $package=>$versions){
|
|
|
|
$packages[$package] = [];
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
foreach($versions as $version=>$dependencies)
|
2016-12-05 18:54:19 +00:00
|
|
|
// if version of package enabled
|
|
|
|
if( isset($this->packages['installed'][$package]) && $version == $this->packages['installed'][$package] )
|
|
|
|
$packages[$package][] = [ 'version' => $version, 'enabled' => true ];
|
2016-12-05 13:42:38 +00:00
|
|
|
else
|
2016-12-05 18:54:19 +00:00
|
|
|
$packages[$package][] = [ 'version' => $version, 'enabled' => false ];
|
2016-10-19 08:16:21 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
return $packages;
|
2016-10-19 08:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ENABLES A MODULE'S VERSION
|
|
|
|
*
|
2016-12-05 18:54:19 +00:00
|
|
|
* @pPackage<String> Package's name
|
|
|
|
* @pVersion<String> Package's version
|
2016-12-05 14:57:08 +00:00
|
|
|
* @pDep<Boolean> dependency level
|
2016-10-19 08:16:21 +00:00
|
|
|
*
|
|
|
|
* @return err_msg<String> Error message | TRUE (if all is ok)
|
|
|
|
*
|
|
|
|
*/
|
2016-12-05 18:54:19 +00:00
|
|
|
public function install($pPackage=null, $pVersion=null, $pDep=0){
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* [1] Package management
|
2016-10-19 08:16:21 +00:00
|
|
|
=========================================================*/
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (1) If @package not given, exit */
|
|
|
|
if( is_null($pPackage) )
|
|
|
|
return "Missing @packageName.\n";
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (2) Checking package existence */
|
|
|
|
if( !isset($this->packages['available'][$pPackage]) )
|
|
|
|
return "Package `$pPackage` unknown.\n";
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (3) Set package name & content */
|
|
|
|
$mname = $pPackage;
|
|
|
|
$package = $this->packages['available'][$mname];
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] Version management
|
|
|
|
=========================================================*/
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (1) Set default latest version name & content */
|
|
|
|
$vname = array_slice( array_keys($package), -1 )[0];
|
|
|
|
$dependencies = $package[$vname];
|
|
|
|
$flag = '#latest';
|
|
|
|
|
|
|
|
/* (2) Existing version set it */
|
|
|
|
if( !is_null($pVersion) && isset($package[$pVersion]) ){
|
|
|
|
$dependencies = $package[( $vname = $pVersion )];
|
|
|
|
$flag = '';
|
|
|
|
}
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* [3] Enables package & version
|
2016-10-19 08:16:21 +00:00
|
|
|
=========================================================*/
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (1) If package disabled -> enables it */
|
|
|
|
if( !isset($this->packages['installed'][$mname]) )
|
|
|
|
$this->packages['installed'][$mname] = '0';
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (2) Checks cross version dependency -> if another installed package needs another version */
|
|
|
|
foreach($this->packages['installed'] as $xPackage=>$xVersion){
|
2016-12-05 13:42:38 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
// {2.1} Loads each installed package's dependencies //
|
|
|
|
$xDependencies = $this->getDependencies($xPackage,$xVersion);
|
2016-12-05 13:42:38 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
// if not found -> next
|
|
|
|
if( $xDependencies === false )
|
2016-12-05 13:42:38 +00:00
|
|
|
continue;
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
// {2.2} if package can't have version of current package //
|
|
|
|
if( isset($xDependencies[$mname]) && !in_array($vname, $xDependencies[$mname]) )
|
|
|
|
return str_repeat(" ", $pDep)."$xPackage:$xVersion doesn't support $mname:$vname\n\n** aborted **\n";
|
2016-12-05 13:42:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (5) If no conflict -> set version */
|
|
|
|
echo str_repeat(" ", $pDep)."[$mname:$vname] $flag\n";
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
// in any case -> store new package's version
|
|
|
|
$this->packages['installed'][$mname] = $vname;
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
/* [4] Loading dependencies
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Loading each dependency */
|
2016-12-05 18:54:19 +00:00
|
|
|
if( count($dependencies) > 0 ){
|
|
|
|
|
|
|
|
foreach($dependencies as $depMod=>$depVersions){
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
// Only load last version
|
|
|
|
foreach($depVersions as $version)
|
|
|
|
$installed = $this->install($depMod, $version, $pDep+1);
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
// error propagation
|
|
|
|
if( $installed !== true )
|
|
|
|
return $installed;
|
|
|
|
}
|
2016-10-19 08:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [5] Storing data
|
|
|
|
=========================================================*/
|
|
|
|
$this->store();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* DISABLES A MODULE'S VERSION
|
|
|
|
*
|
2016-12-05 18:54:19 +00:00
|
|
|
* @pPackage<String> Package's name
|
2016-10-19 08:16:21 +00:00
|
|
|
*
|
|
|
|
* @return err_msg<String> Error message || TRUE if success
|
|
|
|
*
|
|
|
|
*/
|
2016-12-05 18:54:19 +00:00
|
|
|
public function remove($pPackage=null){
|
|
|
|
/* [1] Package management (existence)
|
2016-10-19 08:16:21 +00:00
|
|
|
=========================================================*/
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (1) If @package not given, exit */
|
|
|
|
if( is_null($pPackage) )
|
|
|
|
return "Missing @packageName.\n";
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (2) Checking if package is enabled */
|
|
|
|
$enabled_packages = array_keys($this->packages['installed']);
|
|
|
|
if( !in_array($pPackage, $enabled_packages) )
|
|
|
|
return "Package `$pPackage` not enabled.\n";
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (3) Set package name & content */
|
|
|
|
$mname = $pPackage;
|
|
|
|
$package = $this->packages['installed'][$mname];
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] Dependency verification (not needed)
|
|
|
|
=========================================================*/
|
2016-12-05 18:54:19 +00:00
|
|
|
$dependency = false; // if it's a dependency of another enabled package
|
|
|
|
$callers = []; // list of enabled packages that needs current one
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (1) For each enabled package (excepted itself) */
|
|
|
|
foreach($enabled_packages as $package){
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
// except current package
|
|
|
|
if( $package == $mname ) continue;
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (2) For each version of its package (and its dependencies) */
|
|
|
|
foreach($this->packages['available'][$package] as $version=>$dependencies){
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
// if not the current enabled package's version
|
|
|
|
if( $version != $this->packages['installed'][$package] )
|
2016-10-19 08:16:21 +00:00
|
|
|
continue;
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (3) If current package in dependencies -> can't disable */
|
|
|
|
isset($dependencies[$mname]) && ($dependency = true) && ($callers[] = "`$package`");
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) If a dependency, explain */
|
|
|
|
if( $dependency )
|
2016-12-05 18:54:19 +00:00
|
|
|
return "Cannot disable package $mname because ".implode(',',$callers)." needs it.\n";
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* [3] Disabling package
|
2016-10-19 08:16:21 +00:00
|
|
|
=========================================================*/
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (1) Disabling package */
|
|
|
|
unset($this->packages['installed'][$mname]);
|
2016-10-19 08:16:21 +00:00
|
|
|
|
|
|
|
/* (2) Storing changes */
|
|
|
|
$this->store();
|
|
|
|
|
|
|
|
/* (3) Return success */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 13:42:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* BUILDS A PROJECT
|
|
|
|
*
|
|
|
|
* @pPath<String> Path to project root
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function build($pPath){
|
2016-12-05 18:54:19 +00:00
|
|
|
$builder = new Builder($pPath, $this->packages['installed']);
|
2016-12-05 13:42:38 +00:00
|
|
|
|
|
|
|
$builder->build();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* RETURNS THE LIST OF THE DEPENDENCIES OF A MODULE
|
2016-10-19 08:16:21 +00:00
|
|
|
*
|
2016-12-05 18:54:19 +00:00
|
|
|
* @package<String> Package's label
|
|
|
|
* @version<String> Package's version
|
2016-10-19 08:16:21 +00:00
|
|
|
*
|
2016-12-05 18:54:19 +00:00
|
|
|
* @return dependencies<Array> List of dependencies
|
|
|
|
* FALSE if error
|
2016-10-19 08:16:21 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-12-05 18:54:19 +00:00
|
|
|
private function getDependencies($package=null, $version=null){
|
|
|
|
/* [1] Search for package
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Checks package */
|
|
|
|
if( !isset($this->packages['available'][$package]) )
|
|
|
|
return false;
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (2) Checks version */
|
|
|
|
if( !isset($this->packages['available'][$package][$version]) )
|
|
|
|
return false;
|
2016-10-19 08:16:21 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* [2] Returns dependencies
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) If not an array */
|
|
|
|
if( !is_array($this->packages['available'][$package][$version]) )
|
|
|
|
return [];
|
2016-12-05 13:42:38 +00:00
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
/* (2) If all right */
|
|
|
|
return $this->packages['available'][$package][$version];
|
2016-10-19 08:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 18:54:19 +00:00
|
|
|
|
2016-10-19 08:16:21 +00:00
|
|
|
/* STORES DATA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function store(){
|
2016-12-05 18:54:19 +00:00
|
|
|
file_put_contents(self::config_path(), json_encode($this->packages, JSON_PRETTY_PRINT));
|
2016-10-19 08:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|