From bfe071685e6e89d5432a6579353610fcaf9c20cb Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 19 Oct 2016 10:16:21 +0200 Subject: [PATCH] Exporter management + can list modules + enable/disable modules according to dependencies --- exporter/Exporter.php | 218 ++++++++++++++++++++++++++++++++ exporter/main.php | 79 ++++++++++++ exporter/modules.json | 77 +++++------ src/files/.htaccess | 3 + src/files/autoloader.php | 46 +++++++ src/files/build/.htaccess | 2 + src/files/config/.htaccess | 2 + src/files/public_html/.htaccess | 4 + src/files/public_html/index.php | 56 ++++++++ 9 files changed, 444 insertions(+), 43 deletions(-) create mode 100644 exporter/Exporter.php create mode 100644 exporter/main.php create mode 100644 src/files/.htaccess create mode 100644 src/files/autoloader.php create mode 100644 src/files/build/.htaccess create mode 100644 src/files/config/.htaccess create mode 100644 src/files/public_html/.htaccess create mode 100644 src/files/public_html/index.php diff --git a/exporter/Exporter.php b/exporter/Exporter.php new file mode 100644 index 0000000..0fa880d --- /dev/null +++ b/exporter/Exporter.php @@ -0,0 +1,218 @@ + LOADS CONFIG + * + */ + public function __construct(){ + + $this->modules = json_decode( file_get_contents(self::config_path()), true); + + } + + + /* RETURNS AVAILABLE MODULE LIST + * + * @return modules 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 Module's name + * @pVersion Module's version + * @pDep TRUE if dependency + * + * @return err_msg 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 Module's name + * + * @return err_msg 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 Module's content + * @pFirst First version + * @pSecond Second version + * + * @return lower 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)); + } + + } + +?> diff --git a/exporter/main.php b/exporter/main.php new file mode 100644 index 0000000..35b1643 --- /dev/null +++ b/exporter/main.php @@ -0,0 +1,79 @@ +available(); + + foreach($modules as $module=>$versions){ + + foreach($versions as $version) + echo " - $module-$version\n"; + + echo "\n"; + } + + } break; + + /* (2) Enables a module and its version + ---------------------------------------------------------*/ + case 'enable': { + + if( $arglen < 2 || !preg_match("/^(.+)-([0-9\.]+)$/i", $arguments[1], $matches) ){ + echo "You must specify @module-@version.\n"; + return; + } + + $err = $exporter->enable($matches[1], $matches[2]); + + /* (1) Managing state */ + if( $err === true ) echo "success.\n"; + else echo $err; + + } break; + + /* (3) Disabled a module + ---------------------------------------------------------*/ + case 'disable': { + + if( $arglen < 2 ){ + echo "You must specify @module.\n"; + return; + } + + $err = $exporter->disable($arguments[1]); + + /* (1) Managing state */ + if( $err === true ) echo "success.\n"; + else echo $err; + + } break; + + } + + +?> diff --git a/exporter/modules.json b/exporter/modules.json index 1a1253e..3ba694a 100644 --- a/exporter/modules.json +++ b/exporter/modules.json @@ -1,44 +1,35 @@ { - - "available": { - - "error": { - "versions": [1] - }, - - "api": { - "versions": [1], - "dependencies": [ - ["error", 1] - ] - }, - - "orm": { - "versions": [0.8], - "dependencies": [ - ["database", 1] - ] - }, - - "database": { - "versions": [1], - "dependencies": [ - ["error", 1] - ] - }, - - "lightdb": { - "versions": [1] - }, - - "router": { - "versions": [1] - } - - }, - - "enabled": { - - } - -} + "available": { + "error": { + "1": [], + "1.2": [], + "1.5": [] + }, + "api": { + "0.8": [], + "1": { + "error": "1.2" + } + }, + "orm": { + "0.8": { + "database": "1" + } + }, + "database": { + "1": { + "error": "1" + } + }, + "lightdb": { + "1": [] + }, + "router": { + "1": [] + } + }, + "enabled": { + "api": "1", + "error": "1.2" + } +} \ No newline at end of file diff --git a/src/files/.htaccess b/src/files/.htaccess new file mode 100644 index 0000000..a80b524 --- /dev/null +++ b/src/files/.htaccess @@ -0,0 +1,3 @@ +RewriteEngine on + +RewriteRule ^(.*)$ public_html/$1 [QSA,L] diff --git a/src/files/autoloader.php b/src/files/autoloader.php new file mode 100644 index 0000000..b17cf87 --- /dev/null +++ b/src/files/autoloader.php @@ -0,0 +1,46 @@ + Nom de la classe appelee + * + */ + function autoloader($className){ + $path = ''; + + /* [1] On utilise le namespace pour localiser + ===============================================*/ + // On remplace les '\' par des '/' + $path = str_replace('\\', '/', $className) . '.php'; + $path = __BUILD__.'/'.$path; + + // Si le fichier existe + if( file_exists($path) ) + require_once $path; // on inclue le fichier + + } + + // On definit l'autoloader comme autoloader (obvious) + spl_autoload_register('autoloader', false, true); + + +?> diff --git a/src/files/build/.htaccess b/src/files/build/.htaccess new file mode 100644 index 0000000..93169e4 --- /dev/null +++ b/src/files/build/.htaccess @@ -0,0 +1,2 @@ +Order deny,allow +Deny from all diff --git a/src/files/config/.htaccess b/src/files/config/.htaccess new file mode 100644 index 0000000..93169e4 --- /dev/null +++ b/src/files/config/.htaccess @@ -0,0 +1,2 @@ +Order deny,allow +Deny from all diff --git a/src/files/public_html/.htaccess b/src/files/public_html/.htaccess new file mode 100644 index 0000000..ab2545e --- /dev/null +++ b/src/files/public_html/.htaccess @@ -0,0 +1,4 @@ +RewriteEngine on + +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] diff --git a/src/files/public_html/index.php b/src/files/public_html/index.php new file mode 100644 index 0000000..b80cce8 --- /dev/null +++ b/src/files/public_html/index.php @@ -0,0 +1,56 @@ + page d'accueil + $R->get('/?', function(){ header('Location: /homepage/'); }); + + + /* [2] On recupere la liste des pages du site + =========================================================*/ + // inclusion de la page + $R->get('([a-z+])/?', function($pagename){ include __PUBLIC__."/$pagename.php"; }); + + + + + // http://host/api/module/method -> ModuleRequest + $R->post('api(?:/(.*))?', function($url){ + $request = ModuleRequest::fromPost($url, $_POST); + $answer = $request->dispatch(); + + // Si c'est une réponse + if( $answer instanceof ModuleResponse ) + echo $answer->serialize(); + + }); + + + // N'importe -> page d'accueil + $R->get('.+', function(){ header('Location: /homepage/'); }); + $R->post('.+', function(){ header('Location: /homepage/'); }); + + + + + /* [3] On lance le routeur + ===================================================*/ + $R->run(); + +?>