diff --git a/build/api/core/Request.php b/build/api/core/Request.php index dce1f28..f9a9ac3 100755 --- a/build/api/core/Request.php +++ b/build/api/core/Request.php @@ -5,7 +5,8 @@ use \api\core\AuthSystem; use \api\core\ModuleFactory; use \api\core\Config; - use \error\core\Error; +use database\core\Repo; +use \error\core\Error; use \error\core\Err; @@ -503,6 +504,11 @@ /* (3) On ajoute les données */ $response->appendAll($returned); + /* (4) Si le Debug est actif on ajoute le debug des repo */ + if(Repo::isDebugEnabled()){ + $response->append("repoDebug" , Repo::getDebug()); + } + /* (4) On retourne la réponse */ return $response; diff --git a/build/api/module/excelController.php b/build/api/module/excelController.php index 6c786e5..2e1fb4b 100644 --- a/build/api/module/excelController.php +++ b/build/api/module/excelController.php @@ -63,9 +63,11 @@ class excelController $addEU = function() use (&$UECode,&$allUE,&$UE){ //determine if UE is disabled (if cours+td+tp = 0) $totalVH = 0; - foreach ($UE["groups"] as $groups){ - foreach ($groups as $group){ - $totalVH += $group["VH"]; + if(is_array($UE["groups"]) && count($UE["groups"]) > 0){ + foreach ($UE["groups"] as $groups){ + foreach ($groups as $group){ + $totalVH += $group["VH"]; + } } } @@ -428,7 +430,8 @@ class excelController Repo::flushStack(); - return [ 'data' => ["professors" => $allProf, "formations" => $allFormations, "UEs" => $allUE ] ]; + //return [ 'data' => ["professors" => $allProf, "formations" => $allFormations, "UEs" => $allUE ] ]; + return["data" => true]; }catch (Exception $e){ return [ 'error' => new Error(Err::UnknownError) ]; } diff --git a/build/database/core/DatabaseDriver.php b/build/database/core/DatabaseDriver.php index 7ffe06d..695b06d 100755 --- a/build/database/core/DatabaseDriver.php +++ b/build/database/core/DatabaseDriver.php @@ -61,7 +61,7 @@ * @password Database password * */ - private function __construct($host, $dbname, $username, $password){ + private function __construct($host, $dbname, $username, $password, $debug = false){ /* (2) Stores configuration */ $this->host = $host; $this->dbname = $dbname; @@ -79,6 +79,10 @@ $this->pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false); $this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + if($debug){ + $this->pdo->enableDebug(); + } + // On signale que tout s'est bien passe $this->error = new Error(Err::Success); @@ -120,7 +124,7 @@ /* (1) If local -> instanciates with local configuration */ // if( !checkdnsrr($_SERVER['SERVER_NAME'], 'NS') ) - self::$instance[$label] = new DatabaseDriver($conf[$label]['local']['host'], $conf[$label]['local']['dbname'], $conf[$label]['local']['user'], $conf[$label]['local']['password']); + self::$instance[$label] = new DatabaseDriver($conf[$label]['local']['host'], $conf[$label]['local']['dbname'], $conf[$label]['local']['user'], $conf[$label]['local']['password'],$conf[$label]['local']['debug']); /* (2) If Remote -> instanciates with Remote configuration */ // else // self::$instance[$label] = new DatabaseDriver($conf[$label]['remote']['host'], $conf[$label]['remote']['dbname'], $conf[$label]['remote']['user'], $conf[$label]['remote']['password']); @@ -204,6 +208,14 @@ $this->pdo->executeStack(); } + public function getDebug() : array{ + return $this->pdo->getDebug(); + } + + public function isDebugEnabled() : bool { + return $this->pdo->isDebugEnabled(); + } + } diff --git a/build/database/core/PDOWrapper/PDOWrapper.php b/build/database/core/PDOWrapper/PDOWrapper.php index d8526c2..5674744 100644 --- a/build/database/core/PDOWrapper/PDOWrapper.php +++ b/build/database/core/PDOWrapper/PDOWrapper.php @@ -13,6 +13,8 @@ class PDOWrapper extends \PDO { private $statements = []; private $stacking = false; + private $debug = []; + private $debugEnabled = false; public function __construct(String $dsn, String $username, String $passwd, array $options = []) { @@ -21,6 +23,10 @@ class PDOWrapper extends \PDO public function prepare($statement, $options = []) { + if($this->debugEnabled){ + $this->storeDebug(); + } + if($this->stacking){ return new PDOStatementWrapper($statement, $this); }else{ @@ -30,14 +36,64 @@ class PDOWrapper extends \PDO } } + private function storeDebug(){ + //get all the debug info about the repo + $prepareStack = debug_backtrace(0,3)[1]; + $stack = debug_backtrace(0,3)[2]; + //create the reflection object + $f = new \ReflectionMethod($stack["class"],$stack["function"]); + //get only the repo name + $className = explode("\\",$stack["class"]); + $className = $className[count($className)-1]; + + $result = []; + + //if we are flushing a stack, just count the number of request stacked + if($stack["function"] == "executeStack"){ + + $result["StackedRequest"] = true; + $result["numberOfStackedRequest"] = substr_count($prepareStack["args"][0],";"); + //if we are not stacking, log the repo call + }else if(!$this->stacking){ + //store results + $result["repoName"] = $className; + $result["methodName"] = $stack["function"]; + $result["args"] = []; + + foreach ($f->getParameters() as $key => $param) { + $result["args"][$param->name] = $stack["args"][$key]; + } + //else we are stacking a request, we should not log it + }else{ + return; + } + + $this->debug[] = $result; + } + public function getDebug() : array{ + return $this->debug; + } + public function enableStacking(){ $this->stacking = true; } + public function isDebugEnabled() : bool{ + return $this->debugEnabled; + } + public function stackStatement(PDOStatementWrapper $st){ array_push($this->statements,$st); } + public function enableDebug(){ + $this->debugEnabled = true; + } + + public function disableDebug(){ + $this->debugEnabled = false; + } + public function executeStack(){ //init the statements and the generator of number $finalStatement = ''; diff --git a/build/database/core/Repo.php b/build/database/core/Repo.php index 88b88f7..d5d86f3 100644 --- a/build/database/core/Repo.php +++ b/build/database/core/Repo.php @@ -69,6 +69,14 @@ static::$driver->flushStack(); } + public static function getDebug() : array{ + return static::$driver->getDebug(); + } + + public static function isDebugEnabled() : bool{ + return static::$driver->isDebugEnabled(); + } + diff --git a/config/database-driver.json b/config/database-driver.json index fb2da3c..0c99f5d 100755 --- a/config/database-driver.json +++ b/config/database-driver.json @@ -4,7 +4,8 @@ "host" : "mariadb", "dbname" : "vhost", "user" : "php", - "password" : "4JB1dtbrIC8pT935" + "password" : "4JB1dtbrIC8pT935", + "debug" : true }, "remote": { "host" : "db_remote_host",