]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Memcached_DataObject.php
Don't use DB_DataObject::factory (statically at least)
[quix0rs-gnu-social.git] / classes / Memcached_DataObject.php
index 54c885e737e83454a4488ab573f44883e7d8eb18..aa3a70395f07f81c5405f818232e5eae0c7ceeed 100644 (file)
@@ -30,24 +30,23 @@ class Memcached_DataObject extends Safe_DataObject
      * @param mixed $v key field value, or leave out for primary key lookup
      * @return mixed Memcached_DataObject subtype or false
      */
-    function &staticGet($cls, $k, $v=null)
+    static function getClassKV($cls, $k, $v=null)
     {
+        if (!is_a($cls, __CLASS__, true)) {
+            throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
+        }
         if (is_null($v)) {
             $v = $k;
             $keys = self::pkeyCols($cls);
             if (count($keys) > 1) {
-                // FIXME: maybe call pkeyGet() ourselves?
-                throw new Exception('Use pkeyGet() for compound primary keys');
+                // FIXME: maybe call pkeyGetClass() ourselves?
+                throw new Exception('Use pkeyGetClass() for compound primary keys');
             }
             $k = $keys[0];
         }
-        $i = Memcached_DataObject::getcached($cls, $k, $v);
+        $i = self::getcached($cls, $k, $v);
         if ($i === false) { // false == cache miss
-            $i = DB_DataObject::factory($cls);
-            if (empty($i)) {
-                $i = false;
-                return $i;
-            }
+            $i = new $cls;
             $result = $i->get($k, $v);
             if ($result) {
                 // Hit!
@@ -106,6 +105,9 @@ class Memcached_DataObject extends Safe_DataObject
      */
     static function pivotGet($cls, $keyCol, $keyVals, $otherCols = array())
     {
+        if (!is_a($cls, __CLASS__, true)) {
+            throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
+        }
         if (is_array($keyCol)) {
             foreach ($keyVals as $keyVal) {
                 $result[implode(',', $keyVal)] = null;
@@ -140,11 +142,7 @@ class Memcached_DataObject extends Safe_DataObject
         }
 
         if (count($toFetch) > 0) {
-            $i = DB_DataObject::factory($cls);
-            if (empty($i)) {
-                // TRANS: Exception thrown when a program code class (%s) cannot be instantiated.
-                throw new Exception(sprintf(_('Cannot instantiate class %s.'),$cls));
-            }
+            $i = new $cls;
             foreach ($otherCols as $otherKeyCol => $otherKeyVal) {
                 $i->$otherKeyCol = $otherKeyVal;
             }
@@ -248,11 +246,10 @@ class Memcached_DataObject extends Safe_DataObject
 
     static function pkeyCols($cls)
     {
-        $i = DB_DataObject::factory($cls);
-        if (empty($i)) {
-            // TRANS: Exception thrown when a program code class (%s) cannot be instantiated.
-            throw new Exception(sprintf(_('Cannot instantiate class %s.'),$cls));
+        if (!is_a($cls, __CLASS__, true)) {
+            throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
         }
+        $i = new $cls;
         $types = $i->keyTypes();
         ksort($types);
 
@@ -267,8 +264,11 @@ class Memcached_DataObject extends Safe_DataObject
         return $pkey;
     }
 
-    function listGet($cls, $keyCol, $keyVals)
+    static function listGetClass($cls, $keyCol, $keyVals)
     {
+        if (!is_a($cls, __CLASS__, true)) {
+            throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
+        }
         $pkeyMap = array_fill_keys($keyVals, array());
         $result = array_fill_keys($keyVals, array());
 
@@ -305,14 +305,10 @@ class Memcached_DataObject extends Safe_DataObject
         }
 
         if (count($toFetch) > 0) {
-            $i = DB_DataObject::factory($cls);
-            if (empty($i)) {
-                // TRANS: Exception thrown when a program code class (%s) cannot be instantiated.
-                throw new Exception(sprintf(_('Cannot instantiate class %s.'),$cls));
-            }
+            $i = new $cls;
             $i->whereAddIn($keyCol, $toFetch, $i->columnType($keyCol));
             if ($i->find()) {
-                sprintf("listGet() got {$i->N} results for class $cls key $keyCol");
+                sprintf(__CLASS__ . "() got {$i->N} results for class $cls key $keyCol");
                 while ($i->fetch()) {
                     $copy = clone($i);
                     $copy->encache();
@@ -350,18 +346,18 @@ class Memcached_DataObject extends Safe_DataObject
     }
 
     /**
-     * @todo FIXME: Should this return false on lookup fail to match staticGet?
+     * @todo FIXME: Should this return false on lookup fail to match getKV?
      */
-    function pkeyGet($cls, $kv)
+    static function pkeyGetClass($cls, $kv)
     {
+        if (!is_a($cls, __CLASS__, true)) {
+            throw new Exception('Trying to fetch ' . __CLASS__ . ' into a non-related class');
+        }
         $i = Memcached_DataObject::multicache($cls, $kv);
         if ($i !== false) { // false == cache miss
             return $i;
         } else {
-            $i = DB_DataObject::factory($cls);
-            if (empty($i) || PEAR::isError($i)) {
-                return false;
-            }
+            $i = new $cls;
             foreach ($kv as $k => $v) {
                 if (is_null($v)) {
                     // XXX: possible SQL injection...? Don't
@@ -542,7 +538,7 @@ class Memcached_DataObject extends Safe_DataObject
         return $ckeys;
     }
 
-    function multicache($cls, $kv)
+    static function multicache($cls, $kv)
     {
         ksort($kv);
         $c = self::memcache();
@@ -680,8 +676,10 @@ class Memcached_DataObject extends Safe_DataObject
                         'delete',
                         'update',
                         'find');
-        $ignoreStatic = array('staticGet',
+        $ignoreStatic = array('getKV',
+                              'getClassKV',
                               'pkeyGet',
+                              'pkeyGetClass',
                               'cachedQuery');
         $here = get_class($this); // if we get confused
         $bt = debug_backtrace();