diff --git a/backend/lib/Dispatcher.php b/backend/lib/Dispatcher.php index e556b52..df9f8b3 100644 --- a/backend/lib/Dispatcher.php +++ b/backend/lib/Dispatcher.php @@ -34,6 +34,8 @@ final class Dispatcher { protected $view; // View protected $controller; // Controller + protected $debug = NULL; // optional debugging instance + /* * constructor */ @@ -48,6 +50,12 @@ final class Dispatcher { // initialize entity manager $this->em = Dispatcher::createEntityManager(); + // staring debugging + if (($request->getParameter('debug') && $request->getParameter('debug') > 0) || Util\Configuration::read('debug')) { + $this->debug = new Util\Debug($request->getParameter('debug')); + $this->em->getConnection()->getConfiguration()->setSQLLogger($this->debug); + } + // initialize view if (in_array($format, array('png', 'jpeg', 'gif'))) { $this->view = new View\JpGraph($request, $response, $format); @@ -80,6 +88,11 @@ final class Dispatcher { $action = ($this->view->request->getParameter('action')) ? 'get' : $this->view->request->getParameter('action'); // default action $this->controller->run($action); // run controllers actions (usually CRUD: http://de.wikipedia.org/wiki/CRUD) + + if (Util\Debug::isActivated()) { + $this->view->addDebug($this->debug); + } + $this->view->render(); // render view & send http response } @@ -104,11 +117,7 @@ final class Dispatcher { $config->setProxyNamespace('Volkszaehler\Model\Proxies'); $config->setAutoGenerateProxyClasses(DEV_ENV == true); - $config->setSQLLogger(Util\Debug::getSQLLogger()); - - $em = \Doctrine\ORM\EntityManager::create(Util\Configuration::read('db'), $config); - - return $em; + return \Doctrine\ORM\EntityManager::create(Util\Configuration::read('db'), $config); } } diff --git a/backend/lib/Util/Configuration.php b/backend/lib/Util/Configuration.php index 94fb492..f0d663d 100644 --- a/backend/lib/Util/Configuration.php +++ b/backend/lib/Util/Configuration.php @@ -22,7 +22,7 @@ namespace Volkszaehler\Util; class Configuration { - static public $values = array(); // TODO protected + static protected $values = array(); static public function write($var, $value) { if (!is_scalar($value) && !is_array($value)) { @@ -32,7 +32,7 @@ class Configuration { $values =& self::$values; $tree = explode('.', $var); foreach ($tree as $part) { - $values =& $values[$part]; + $values =& $values[$part]; // TODO array_merge_recursive() } $values = $value; diff --git a/backend/lib/Util/Debug.php b/backend/lib/Util/Debug.php index f3095d8..b86f8ce 100644 --- a/backend/lib/Util/Debug.php +++ b/backend/lib/Util/Debug.php @@ -21,15 +21,89 @@ namespace Volkszaehler\Util; -class Debug { - private static $logger = NULL; +use Doctrine\DBAL\Logging; + +class Debug implements Logging\SQLLogger { + protected static $instance = NULL; - static public function getSQLLogger() { - if (is_null(self::$logger)) { - self::$logger = new \Doctrine\DBAL\Logging\DebugStack(); + protected $queries = array(); + protected $messages = array(); + + protected $started; // holds timestamp of initialization, used later to return time of execution + protected $level; + + /* + * constructor + */ + public function __construct($level) { + // taking timestamp to stop execution time + $this->created = microtime(true); + + $this->level = $level; + + if (!is_null(self::$instance)) { + throw new \Exception('debugging has already been started. please use the static functions!'); } - return self::$logger; + self::$instance = $this; + + // assert options + assert_options(ASSERT_ACTIVE, true); + assert_options(ASSERT_BAIL, false); + assert_options(ASSERT_WARNING, false); + assert_options(ASSERT_CALLBACK, array($this, 'assertHandler')); + + } + + /* + * interface for doctrine's dbal sql logger + */ + function logSQL($sql, array $params = null) { + $this->queries[] = array('sql' => $sql, 'parameter' => $params); + } + + static public function log($message) { + if (!is_null(self::$instance)) { + $trace = debug_backtrace(); + + self::$instance->messages[] = array( + 'message' => $message, + 'file' => $trace[0]['file'], + 'line' => $trace[0]['line'], + 'time' => date('r'), + 'args' => array_slice($trace[0]['args'], 1), + 'trace' => $trace + ); + } + } + + /* + * simple assertion passthrough for future improvements + * + * @todo check how should be en/disabled (options etc..) + */ + public static function assert($code) { + assert($code); + } + + public function assertHandler($file, $line, $code) { + $trace = debug_backtrace(); + $info = $trace[2]; + + $this->messages[] = array( + 'message' => 'assertion failed: ' . $code, + 'file' => $info['file'], + 'line' => $info['line'], + 'time' => date('r'), + 'trace' => array_slice($trace, 2) + ); + } + + public static function isActivated() { return !is_null(self::$instance); } + + public function getExecutionTime() { return round((microtime(true) - $this->created), 5); } + public function getQueries() { return $this->queries; } + public function getMessages() { return $this->messages; } } ?> \ No newline at end of file diff --git a/backend/lib/View/Csv/Csv.php b/backend/lib/View/Csv/Csv.php index c713469..3b35873 100644 --- a/backend/lib/View/Csv/Csv.php +++ b/backend/lib/View/Csv/Csv.php @@ -62,7 +62,6 @@ abstract class Csv extends \Volkszaehler\View\View { } public function addDebug() { - $this->footer[] = 'time: ' . $this->getTime(); $this->footer[] = 'database: ' . Util\Configuration::read('db.driver'); diff --git a/backend/lib/View/Json/Json.php b/backend/lib/View/Json/Json.php index 43a1254..9731ae6 100644 --- a/backend/lib/View/Json/Json.php +++ b/backend/lib/View/Json/Json.php @@ -39,19 +39,15 @@ abstract class Json extends \Volkszaehler\View\View { } public function render() { - parent::render(); - // TODO solve rendering order problem - $json = json_encode($this->json); - if ($this->request->getParameter('debug')) { - echo self::format($json); - } - else { - echo $json; + if (Util\Debug::isActivated()) { + $json = self::format($json); } + + echo $json; - + parent::render(); } protected static function format($json) { @@ -104,21 +100,25 @@ abstract class Json extends \Volkszaehler\View\View { return $formatted; } - public function addDebug() { - $this->json['debug'] = array('time' => $this->getTime(), - 'database' => array('driver' => Util\Configuration::read('db.driver'), - 'queries' => Util\Debug::getSQLLogger()->queries) + public function addDebug(Util\Debug $debug) { + $this->json['debug'] = array( + 'time' => $debug->getExecutionTime(), + 'messages' => $debug->getMessages(), + 'database' => array( + 'driver' => Util\Configuration::read('db.driver'), + 'queries' => $debug->getQueries() + ) ); - } public function addException(\Exception $exception) { - $this->json['exception'] = array('type' => get_class($exception), - 'message' => $exception->getMessage(), - 'code' => $exception->getCode(), - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - 'trace' => $exception->getTrace() + $this->json['exception'] = array( + 'type' => get_class($exception), + 'message' => $exception->getMessage(), + 'code' => $exception->getCode(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + 'trace' => $exception->getTrace() ); } } diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php index 6177766..b92ccdc 100644 --- a/backend/lib/View/View.php +++ b/backend/lib/View/View.php @@ -21,19 +21,16 @@ namespace Volkszaehler\View; +use Volkszaehler\Util; + abstract class View { public $request; protected $response; - private $created; // holds timestamp of creation, used later to return time of execution - public function __construct(Http\Request $request, Http\Response $response) { $this->request = $request; $this->response = $response; - // TODO move to Debug or State class - $this->created = microtime(true); - // error & exception handling by view set_exception_handler(array($this, 'exceptionHandler')); set_error_handler(array($this, 'errorHandler')); @@ -58,16 +55,7 @@ abstract class View { die(); } - // TODO move this into Debug or State Class - protected function getTime() { - return round(microtime(true) - $this->created, 4); - } - public function render() { - if (!is_null($this->request->getParameter('debug')) && $this->request->getParameter('debug') > 0) { - $this->addDebug(); - } - $this->response->send(); } @@ -75,7 +63,7 @@ abstract class View { echo $e; } - public function addDebug() { + public function addDebug(Util\Debug $debug) { } } \ No newline at end of file