From 16dd0c7230252d4b59a67c919b598c24d9fb2dbc Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 6 Aug 2010 02:01:00 +0200 Subject: [PATCH] dibi::$substs & fall-back provided via DibiLazyStorage --- dibi/dibi.php | 25 +++++++++++-------------- dibi/libs/DibiTranslator.php | 6 ++---- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index c4ba2f7f..96790609 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -176,11 +176,8 @@ class dibi /** @var DibiConnection Current connection */ private static $connection; - /** @var array Substitutions for identifiers */ - public static $substs = array(); - - /** @var callback Substitution fallback */ - public static $substFallBack = array(__CLASS__, 'defaultSubstFallback'); + /** @var DibiLazyStorage Substitutions for identifiers */ + public static $substs; /** @var array @see addHandler */ private static $handlers = array(); @@ -638,7 +635,7 @@ class dibi */ 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) { if ($expr === TRUE) { - self::$substs = array(); + self::$substs = new DibiLazyStorage(self::$substs->getCallback()); } else { - unset(self::$substs[$expr]); + unset(self::$substs->$expr); } } @@ -666,12 +663,7 @@ class dibi */ public static function setSubstFallback($callback) { - if (!is_callable($callback)) { - $able = is_callable($callback, TRUE, $textual); - throw new InvalidArgumentException("Handler '$textual' is not " . ($able ? 'callable.' : 'valid PHP callback.')); - } - - self::$substFallBack = $callback; + self::$substs->setCallback($callback); } @@ -755,3 +747,8 @@ class dibi } } + + + +// static constructor +dibi::$substs = new DibiLazyStorage(array('dibi', 'defaultSubstFallback')); \ No newline at end of file diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index 3242f35c..e5277580 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -543,8 +543,7 @@ final class DibiTranslator extends DibiObject if ($matches[8]) { // SQL identifier substitution $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($m, FALSE) : $m . $matches[9]; // value or identifier + return $matches[9] == '' ? $this->formatValue(dibi::$substs->$m, FALSE) : dibi::$substs->$m . $matches[9]; // value or identifier } die('this should be never executed'); @@ -591,8 +590,7 @@ final class DibiTranslator extends DibiObject */ private static function subCb($m) { - $m = $m[1]; - return isset(dibi::$substs[$m]) ? dibi::$substs[$m] : call_user_func(dibi::$substFallBack, $m); + return dibi::$substs->{$m[1]}; } }