1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-08 15:16:58 +02:00

dibi::$substs & fall-back provided via DibiLazyStorage

This commit is contained in:
David Grudl
2010-08-06 02:01:00 +02:00
parent 79735e96d1
commit 16dd0c7230
2 changed files with 13 additions and 18 deletions

View File

@@ -176,11 +176,8 @@ class dibi
/** @var DibiConnection Current connection */ /** @var DibiConnection Current connection */
private static $connection; private static $connection;
/** @var array Substitutions for identifiers */ /** @var DibiLazyStorage Substitutions for identifiers */
public static $substs = array(); public static $substs;
/** @var callback Substitution fallback */
public static $substFallBack = array(__CLASS__, 'defaultSubstFallback');
/** @var array @see addHandler */ /** @var array @see addHandler */
private static $handlers = array(); private static $handlers = array();
@@ -638,7 +635,7 @@ class dibi
*/ */
public static function addSubst($expr, $subst) public static function addSubst($expr, $subst)
{ {
self::$substs[$expr] = $subst; self::$substs->$expr = $subst;
} }
@@ -651,9 +648,9 @@ class dibi
public static function removeSubst($expr) public static function removeSubst($expr)
{ {
if ($expr === TRUE) { if ($expr === TRUE) {
self::$substs = array(); self::$substs = new DibiLazyStorage(self::$substs->getCallback());
} else { } else {
unset(self::$substs[$expr]); unset(self::$substs->$expr);
} }
} }
@@ -666,12 +663,7 @@ class dibi
*/ */
public static function setSubstFallback($callback) public static function setSubstFallback($callback)
{ {
if (!is_callable($callback)) { self::$substs->setCallback($callback);
$able = is_callable($callback, TRUE, $textual);
throw new InvalidArgumentException("Handler '$textual' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
}
self::$substFallBack = $callback;
} }
@@ -755,3 +747,8 @@ class dibi
} }
} }
// static constructor
dibi::$substs = new DibiLazyStorage(array('dibi', 'defaultSubstFallback'));

View File

@@ -543,8 +543,7 @@ final class DibiTranslator extends DibiObject
if ($matches[8]) { // SQL identifier substitution if ($matches[8]) { // SQL identifier substitution
$m = substr($matches[8], 0, -1); $m = substr($matches[8], 0, -1);
$m = isset(dibi::$substs[$m]) ? dibi::$substs[$m] : call_user_func(dibi::$substFallBack, $m); return $matches[9] == '' ? $this->formatValue(dibi::$substs->$m, FALSE) : dibi::$substs->$m . $matches[9]; // value or identifier
return $matches[9] == '' ? $this->formatValue($m, FALSE) : $m . $matches[9]; // value or identifier
} }
die('this should be never executed'); die('this should be never executed');
@@ -591,8 +590,7 @@ final class DibiTranslator extends DibiObject
*/ */
private static function subCb($m) private static function subCb($m)
{ {
$m = $m[1]; return dibi::$substs->{$m[1]};
return isset(dibi::$substs[$m]) ? dibi::$substs[$m] : call_user_func(dibi::$substFallBack, $m);
} }
} }