mirror of
https://github.com/dg/dibi.git
synced 2025-08-13 01:24:06 +02:00
* added dibi::addHandler & dibi::invokeEvent
* logging moved from DibiDriver -> Dibi::afterQuery()
This commit is contained in:
115
dibi/dibi.php
115
dibi/dibi.php
@@ -138,6 +138,17 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
private static $substs = array();
|
private static $substs = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see addHandler
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $handlers = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private static $timer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -474,8 +485,102 @@ class dibi
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Error logging
|
* Add new event handler
|
||||||
* EXPERIMENTAL
|
*
|
||||||
|
* @param string event name
|
||||||
|
* @param callback
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function addHandler($event, $callback)
|
||||||
|
{
|
||||||
|
if (!is_callable($callback)) {
|
||||||
|
throw new DibiException("Invalid callback");
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$handlers[$event][] = $callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke registered handlers
|
||||||
|
*
|
||||||
|
* @param string event name
|
||||||
|
* @param array arguments passed into handler
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function invokeEvent($event, $args)
|
||||||
|
{
|
||||||
|
if (!isset(self::$handlers[$event])) return;
|
||||||
|
|
||||||
|
foreach (self::$handlers[$event] as $handler) {
|
||||||
|
call_user_func_array($handler, $args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static function beforeQuery($driver, $sql)
|
||||||
|
{
|
||||||
|
self::$sql = $sql;
|
||||||
|
self::$timer = -microtime(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error logging - EXPERIMENTAL
|
||||||
|
*/
|
||||||
|
public static function afterQuery($driver, $sql, $res)
|
||||||
|
{
|
||||||
|
if ($res === FALSE) { // query error
|
||||||
|
if (self::$logFile) { // log to file
|
||||||
|
$info = $driver->errorInfo();
|
||||||
|
if ($info['code']) {
|
||||||
|
$info['message'] = "[$info[code]] $info[message]";
|
||||||
|
}
|
||||||
|
|
||||||
|
self::log(
|
||||||
|
"ERROR: $info[message]"
|
||||||
|
. "\n-- SQL: " . self::$sql
|
||||||
|
. "\n-- driver: " . $driver->getConfig('driver')
|
||||||
|
. ";\n-- " . date('Y-m-d H:i:s ')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self::$throwExceptions) {
|
||||||
|
$info = $driver->errorInfo();
|
||||||
|
throw new DibiException('Query error (driver ' . $driver->getConfig('driver') . ')', $info, self::$sql);
|
||||||
|
} else {
|
||||||
|
$info = $driver->errorInfo();
|
||||||
|
if ($info['code']) {
|
||||||
|
$info['message'] = "[$info[code]] $info[message]";
|
||||||
|
}
|
||||||
|
|
||||||
|
trigger_error("self: $info[message]", E_USER_WARNING);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self::$logFile && self::$logAll) { // log success
|
||||||
|
self::$timer += microtime(true);
|
||||||
|
$msg = $res instanceof DibiResult ? 'object('.get_class($res).') rows: '.$res->rowCount() : 'OK';
|
||||||
|
|
||||||
|
self::log(
|
||||||
|
"OK: " . self::$sql
|
||||||
|
. ";\n-- result: $msg"
|
||||||
|
. "\n-- takes: " . sprintf('%0.3f', self::$timer * 1000) . ' ms'
|
||||||
|
. "\n-- driver: " . $driver->getConfig('driver')
|
||||||
|
. "\n-- " . date('Y-m-d H:i:s ')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error logging - EXPERIMENTAL
|
||||||
*/
|
*/
|
||||||
public static function log($message)
|
public static function log($message)
|
||||||
{
|
{
|
||||||
@@ -490,3 +595,9 @@ class dibi
|
|||||||
|
|
||||||
|
|
||||||
} // class dibi
|
} // class dibi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// this is experimental:
|
||||||
|
dibi::addHandler('beforeQuery', array('dibi', 'beforeQuery'));
|
||||||
|
dibi::addHandler('afterQuery', array('dibi', 'afterQuery'));
|
@@ -73,14 +73,24 @@ abstract class DibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the configuration descriptor
|
* Returns configuration variable. If no $key is passed, returns the entire array.
|
||||||
*
|
*
|
||||||
* @see DibiDriver::__construct
|
* @see DibiDriver::__construct
|
||||||
* @return array
|
* @param string
|
||||||
|
* @param mixed default value to use if key not found
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
final public function getConfig()
|
final public function getConfig($key = NULL, $default = NULL)
|
||||||
{
|
{
|
||||||
return $this->config;
|
if ($key === NULL) {
|
||||||
|
return $this->config;
|
||||||
|
|
||||||
|
} elseif (isset($this->config[$key])) {
|
||||||
|
return $this->config[$key];
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -113,55 +123,18 @@ abstract class DibiDriver
|
|||||||
|
|
||||||
// and generate SQL
|
// and generate SQL
|
||||||
$trans = new DibiTranslator($this);
|
$trans = new DibiTranslator($this);
|
||||||
dibi::$sql = $sql = $trans->translate($args);
|
$sql = $trans->translate($args);
|
||||||
|
|
||||||
|
// event handler
|
||||||
|
dibi::invokeEvent('beforeQuery', array($this, $sql));
|
||||||
|
|
||||||
if ($sql === FALSE) return FALSE;
|
if ($sql === FALSE) return FALSE;
|
||||||
|
|
||||||
// execute SQL
|
// execute SQL
|
||||||
$timer = -microtime(true);
|
|
||||||
$res = $this->nativeQuery($sql);
|
$res = $this->nativeQuery($sql);
|
||||||
|
|
||||||
if ($res === FALSE) { // query error
|
// event handler
|
||||||
if (dibi::$logFile) { // log to file
|
dibi::invokeEvent('afterQuery', array($this, $sql, $res));
|
||||||
$info = $this->errorInfo();
|
|
||||||
if ($info['code']) {
|
|
||||||
$info['message'] = "[$info[code]] $info[message]";
|
|
||||||
}
|
|
||||||
|
|
||||||
dibi::log(
|
|
||||||
"ERROR: $info[message]"
|
|
||||||
. "\n-- SQL: " . $sql
|
|
||||||
. "\n-- driver: " . $this->config['driver']
|
|
||||||
. ";\n-- " . date('Y-m-d H:i:s ')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dibi::$throwExceptions) {
|
|
||||||
$info = $this->errorInfo();
|
|
||||||
throw new DibiException('Query error (driver ' . $this->config['driver'] . ')', $info, $sql);
|
|
||||||
} else {
|
|
||||||
$info = $this->errorInfo();
|
|
||||||
if ($info['code']) {
|
|
||||||
$info['message'] = "[$info[code]] $info[message]";
|
|
||||||
}
|
|
||||||
|
|
||||||
trigger_error("dibi: $info[message]", E_USER_WARNING);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dibi::$logFile && dibi::$logAll) { // log success
|
|
||||||
$timer += microtime(true);
|
|
||||||
$msg = $res instanceof DibiResult ? 'object('.get_class($res).') rows: '.$res->rowCount() : 'OK';
|
|
||||||
|
|
||||||
dibi::log(
|
|
||||||
"OK: " . $sql
|
|
||||||
. ";\n-- result: $msg"
|
|
||||||
. "\n-- takes: " . sprintf('%0.3f', $timer * 1000) . ' ms'
|
|
||||||
. "\n-- driver: " . $this->config['driver']
|
|
||||||
. "\n-- " . date('Y-m-d H:i:s ')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user