diff --git a/readme.md b/readme.md index df55847..4f1ea1a 100644 --- a/readme.md +++ b/readme.md @@ -3,7 +3,7 @@ Debug tools like: varExport* functions, timer, server dump, SQL Beautifier, etc. ## Prerequisite -* PHP 4 (v1.0), PHP 5.4+ (v1.1+) ou PHP 7 & 7.4 (v2) +* PHP 4 (v1.0), PHP 5.4+ (v1.1), PHP 5.6 (v1.2+) or PHP 7.4 (v2) ## Install Edit your [composer.json](https://getcomposer.org) (launch `composer update` after edit): @@ -12,7 +12,7 @@ Edit your [composer.json](https://getcomposer.org) (launch `composer update` aft "repositories": [ { "type": "git", "url": "git@github.com:jgauthi/component_debug.git" } ], - "require": { + "require-dev": { "jgauthi/component_debug": "1.*" } } diff --git a/src/timer.class.php b/src/timer.class.php new file mode 100644 index 0000000..43f7e65 --- /dev/null +++ b/src/timer.class.php @@ -0,0 +1,127 @@ + + * @maj 14/03/09 + * @version 1.0 + */ + +class timer +{ + var $debut; + var $fin; + var $time = array(); + var $start = false; + var $header = 0; + var $resultat; + var $etape; + + //-- CONSTRUCTEUR --------------------------------- + FUNCTION timer($header = 0) + { + $this -> debut = microtime(); + $this -> header = $header; + } + + //-- ETALONNER LE PARCOURS DU SCRIPT -------------- + FUNCTION etape($nom) + { + $this -> time[] = array('nom' => $nom, 'time' => microtime()); + } + + //-- METTRE FIN AU COMPTEUR + FUNCTION stop() + { + $this -> fin = microtime(); + + // Conversion + $this -> debut = $this -> _unit($this -> debut); + $this -> fin = $this -> _unit($this -> fin); + + // Vérifie si le header est correcte + if ($this -> header && headers_sent()) $this -> header = false; + + // Analyse des données + $this -> resultat = $this -> _conv($this -> fin - $this -> debut); + + // Analyse des étapes + if (count($this -> time) > 0) + { + $etape = ''; + foreach($this -> time as $id => $tmp) + { + $temp = $this -> _vir($this -> _conv($this -> _unit($tmp['time']) - $this -> debut)); + $this -> etape .= '- '.$tmp['nom'].': '.$temp." ms\n"; + if ($this -> header) + header($id.'-Time: '.$temp.' ms -> '.$tmp['nom']); + } + } + + // Renvoi des données + if ($this -> header) header('X-Time: '.$this -> _vir($this -> resultat).' ms'); + } + + //-- + FUNCTION OutPut($type = 'commentaire') + { + if ($type == 'commentaire') + { + $av = ''; + } + else + { + $av = '

Temps écoulé du script:

'; + $ap = ''; + } + + if (count($this -> time) == 0) + return ("\n\n".$av.'Time: '.$this -> _vir($this -> resultat).' ms'.$ap); + else + return "\n\n".$av. + (($type != 'commentaire') ? + str_replace("\n","
\n", $this -> etape): + $this -> etape). + $ap."\n". '=> Time: '.$this -> _vir($this -> resultat)." ms\n\n"; + } + + FUNCTION temps () + { + return $this -> _vir($this -> resultat); + } + + FUNCTION end($format = 'html') + { + $this->stop(); + echo $this->OutPut($format); + } + + FUNCTION shutdown() + { + register_shutdown_function(array($this, 'end')); + } + + //-- CONVERTISSEUR --------------------------------- + FUNCTION _unit($time) + { + list($usec,$sec) = explode(' ',$time); + return $usec + $sec; + } + + FUNCTION _conv($time) + { + return substr($time*1000, 0, 6); + + // Récupérer les 6 premiers chiffres + // Convertie en millisecondes + // Remplace . par , ( écriture FR) + } + + FUNCTION _vir($chiffre) + { + return str_replace('.', ',', $chiffre); + } +} + +?> \ No newline at end of file