From: dj3c1t Date: Thu, 26 Sep 2013 19:32:56 +0000 (+0200) Subject: gestion des erreurs pendant l'initialisation X-Git-Tag: mtweb.0.10.1 X-Git-Url: http://git.dj3c1t.com/index.cgi?a=commitdiff_plain;ds=sidebyside;h=b91d4ed0a098b00708072de6fecd8c5047cb586b;p=mtweb gestion des erreurs pendant l'initialisation --- diff --git a/index.php b/index.php index 3276947..f43715b 100644 --- a/index.php +++ b/index.php @@ -2,7 +2,10 @@ require "mw/mw_app.php"; $app = new mw_app("pathes.php"); - if($app->init() && $app->run()) $app->display(); -// else $app->show_logs(); -?> \ No newline at end of file + if(!$app->init()){ + $app->show_error(); + return; + } + + if($app->run()) $app->display(); diff --git a/mw/mw_app.php b/mw/mw_app.php index b198197..1c18b21 100644 --- a/mw/mw_app.php +++ b/mw/mw_app.php @@ -8,22 +8,17 @@ var $config_file; var $config; var $bdd; - var $INITED; - var $logs; + var $error; var $DO_SETUP; - function mw_app($path_file){ - - // executer le setup si le fichier de config est absent - $this->DO_SETUP = true; - + function mw_app($path_file, $DO_SETUP = true){ + $this->DO_SETUP = $DO_SETUP; $this->path_file = $path_file; $this->pathes = array(); $this->config_file = ""; $this->config = array(); $this->bdd = array(); - $this->INITED = false; - $this->logs = array(); + $this->error = false; } function set_env(&$env){ @@ -35,13 +30,11 @@ } function init(){ - $this->INITED = - $this->init_pathes() - && $this->init_config() - && $this->init_env(); - if(!$this->INITED) return false; + if(!$this->init_pathes()) return false; + if(!$this->init_config()) return false; + if(!$this->init_env()) return false; if($this->config_file) return true; - $this->setup(); + if($this->DO_SETUP) $this->setup(); return false; } @@ -56,7 +49,7 @@ !$this->path_file || !file_exists($this->path_file) ){ - $this->log("path_file introuvable: ".$this->path_file); + $this->error("le fichier des chemins est introuvable"); return false; } require_once $this->path_file; @@ -64,8 +57,7 @@ !isset($PATHES) || !is_array($PATHES) ){ - $this->log("path_file: ".$this->path_file); - $this->log("variable PATHES non defini"); + $this->erreur("variable PATHES non defini"); return false; } foreach($PATHES as $path_name => $path_value){ @@ -77,18 +69,18 @@ || !isset($this->pathes["mw_path"]) || !isset($this->pathes["content"]) ){ - $this->log("variable PATHES incomplete"); + $this->error("variable PATHES incomplete"); return false; } if( !file_exists($this->pathes["content"]) || !is_dir($this->pathes["content"]) ){ - $this->log("dossier content introuvable: ".$this->pathes["content"]); + $this->error("dossier content introuvable"); return false; } if(!is_writable($this->pathes["content"])){ - $this->log("Php ne peut pas ecrire dans le dossier content: ".$this->pathes["content"]); + $this->error("Php ne peut pas ecrire dans le dossier content"); return false; } return true; @@ -115,48 +107,57 @@ } } } - else $this->log("pas de fichier de config: ".$config_file); + else{ + if(!$this->DO_SETUP){ + $this->error("fichier config.php manquant"); + return false; + } + } return true; } function init_env(){ - $env = false; - $this->set_env($env); if( - ($sxml_class_file = (file_exists($this->pathes["mw_dir"]."libs/sxml.php") ? $this->pathes["mw_dir"]."libs/sxml.php" : "")) - && ($empty_class_file = (file_exists($this->pathes["mw_dir"]."libs/empty_class.php") ? $this->pathes["mw_dir"]."libs/empty_class.php" : "")) - && ($env_class_file = (file_exists($this->pathes["mw_dir"]."env/mw_env.php") ? $this->pathes["mw_dir"]."env/mw_env.php" : "")) + !($sxml_class_file = (file_exists($this->pathes["mw_dir"]."libs/sxml.php") ? $this->pathes["mw_dir"]."libs/sxml.php" : "")) + || !($empty_class_file = (file_exists($this->pathes["mw_dir"]."libs/empty_class.php") ? $this->pathes["mw_dir"]."libs/empty_class.php" : "")) + || !($env_class_file = (file_exists($this->pathes["mw_dir"]."env/mw_env.php") ? $this->pathes["mw_dir"]."env/mw_env.php" : "")) ){ - if(!class_exists("sxml")) require_once $sxml_class_file; - if(!class_exists("empty_class")) require_once $empty_class_file; - if(!class_exists("mw_env")) require_once $env_class_file; - if( - class_exists("sxml") - && class_exists("empty_class") - && class_exists("mw_env") - ){ - $env = new mw_env(true); - $this->set_env($env); - $env->load_modules($this->pathes["mw_dir"], "env/modules/"); - $env->set_config_file($this->config_file); - $env->set_PATHES($this->pathes); - $env->init_plugins(); - $env->load_config($this->bdd, $this->config); - $env->init(); - } + $this->error("des fichiers sont introuvables. impossible d'initialiser l'environnement"); + return false; } - return $this->env(); + if(!class_exists("sxml")) require_once $sxml_class_file; + if(!class_exists("empty_class")) require_once $empty_class_file; + if(!class_exists("mw_env")) require_once $env_class_file; + if( + !class_exists("sxml") + || !class_exists("empty_class") + || !class_exists("mw_env") + ){ + $this->error("des classes sont introuvables. impossible d'initialiser l'environnement"); + return false; + } + $env = new mw_env(true); + $this->set_env($env); + $env->load_modules($this->pathes["mw_dir"], "env/modules/"); + $env->set_config_file($this->config_file); + $env->set_PATHES($this->pathes); + $env->init_plugins(); + $env->load_config($this->bdd, $this->config); + $env->init(); + return true; } function run($etat = "", $params = array(), $valid_role = true){ - if($this->INITED){ - $env = $this->env(); - $etat = ($etat === false ? false : ($etat ? $etat : (isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : ""))); - if($etat !== false) $env->run($etat, $params, $valid_role); - return true; - } - $this->log("run sans init. l'application ne s'est pas executee"); - return false; + $env = $this->env(); + if(!is_callable(array($env, "run"))) return false; + $env->run( + $etat ? + $etat + : (isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : ""), + $params, + $valid_role + ); + return true; } function display(){ @@ -168,32 +169,20 @@ } } - function log($content){ - $this->logs[] = array( - "time" => date("Y-m-d H:i:s"), - "value" => $content - ); + function setup(){ + $env = $this->env(); + $etat = isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "install"; + $env->run($etat, array(), false); + $this->display(); + exit; } - function setup(){ - if($this->DO_SETUP){ - $env = $this->env(); - $etat = isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "install"; - $env->run($etat, array(), false); - $this->display(); - exit; - } + function error($content){ + $this->error = $content; } - function show_logs(){ - foreach($this->logs as $log_time => $log_value){ - $log_value = is_bool($log_value) ? "bool(".($log_value ? "true" : "false").")" : $log_value; - echo PHP_SAPI == "cli" ? - "\n".print_r($log_value, true)."\n" - : "
".htmlentities(print_r($log_value, true), ENT_QUOTES, "UTF-8")."
"; - } + function show_error(){ + echo $this->error; } } - -?> \ No newline at end of file