* @link http://netmonsters.ru * @package Majestic * @subpackage util * @since 2010-03-09 * @version SVN: $Id$ * @filesource $URL$ */ class Profiler { protected $start = null; protected $end = null; protected $queries = array(); static protected $instance = null; private function __construct() { if (DEBUG == false) { throw new GeneralException('Need to turn on DEBUG before use.'); } } /** * Refuse cloning */ private function __clone(){} /** * @return Profiler */ static public function getInstance() { if (!isset(self::$instance)) { self::$instance = new self(); } return self::$instance; } /** * @param string $query * @return QueryProfiler */ public function profilerQuery($query) { $profiler = new QueryProfiler($query); $this->queries[] = $profiler; return $profiler; } public function start() { $this->start = microtime(true); } public function end($html) { $this->end = microtime(true); if (stripos($html, '') == False) { return $html; } return str_ireplace('', $this->getOutput() . '', $html); } protected function getOutput() { $html = '
' . 'Elapsed time: ' . round(($this->end- $this->start) * 1000) . 'ms.
' . 'Queries: ' . count($this->queries) . '
'; foreach ($this->queries as $query) { $html .= '[' . round($query->getElapsed() * 1000) . 'ms] ' . $query->getQuery() . '
'; } $html .= '
'; return $html; } }