Remove reliance on injector in Horde_Perms
authorMichael M Slusarz <slusarz@curecanti.org>
Sat, 15 May 2010 03:01:56 +0000 (21:01 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 18 May 2010 17:17:02 +0000 (11:17 -0600)
framework/Perms/lib/Horde/Perms/Datatree.php
framework/Perms/lib/Horde/Perms/Permission.php
framework/Perms/lib/Horde/Perms/Permission/DataTreeObject.php
framework/Perms/lib/Horde/Perms/Permission/SqlObject.php
framework/Perms/lib/Horde/Perms/Sql.php

index a939164..85e5a90 100644 (file)
@@ -84,6 +84,7 @@ class Horde_Perms_Datatree extends Horde_Perms
         }
 
         $perm = new Horde_Perms_Permission_DataTreeObject($name, $this->_cacheVersion, $type, $params);
+        $perm->setCacheOb($this->_cache);
         $perm->setDataTree($this->_datatree);
 
         return $perm;
@@ -113,6 +114,9 @@ class Horde_Perms_Datatree extends Horde_Perms
             $this->_permsCache[$name] = unserialize($perm);
         }
 
+        $this->_permsCache[$name]->setCacheOb($this->_cache);
+        $this->_permsCache[$name]->setDataTree($this->_datatree);
+
         return $this->_permsCache[$name];
     }
 
index f0e8df1..2b26144 100644 (file)
  */
 class Horde_Perms_Permission
 {
-    // TODO
+    /**
+     * TODO
+     */
     public $data;
 
-    // TODO
+    /**
+     * TODO
+     */
     public $name;
 
     /**
@@ -36,7 +40,8 @@ class Horde_Perms_Permission
      * @param array $params          A hash with any parameters that the
      *                               permission type needs.
      */
-    public function __construct($name, $cacheVersion = null, $type = 'matrix', $params = null)
+    public function __construct($name, $cacheVersion = null, $type = 'matrix',
+                                $params = null)
     {
         $this->setName($name);
         $this->setCacheVersion($cacheVersion);
index 6926929..af66847 100644 (file)
 class Horde_Perms_Permission_DataTreeObject extends DataTreeObject
 {
     /**
+     * Cache object.
+     *
+     * @var Horde_Cache
+     */
+    protected $_cache;
+
+    /**
      * Constructor. Just makes sure to call the parent constructor so that
      * the perm's name is set properly.
      *
@@ -36,6 +43,26 @@ class Horde_Perms_Permission_DataTreeObject extends DataTreeObject
     }
 
     /**
+     * Don't store cache object on serialize().
+     *
+     * @return array  List of variables to serialize.
+     */
+    public function __sleep()
+    {
+        return array_diff(array_keys(get_class_vars(__CLASS__)), array('_cache'));
+    }
+
+    /**
+     * Sets the cache instance in the object.
+     *
+     * @param Horde_Cache $cache  The cache object.
+     */
+    public function setCacheOb(Horde_Cache $cache)
+    {
+        $this->_cache = $cache;
+    }
+
+    /**
      * Gets one of the attributes of the object, or null if it isn't defined.
      *
      * @param string $attribute  The attribute to get.
@@ -554,9 +581,10 @@ class Horde_Perms_Permission_DataTreeObject extends DataTreeObject
 
         parent::save();
 
-        $cache = $GLOBALS['injector']->getInstance('Horde_Cache');
-        $cache->expire('perm_' . $this->_cacheVersion . $name);
-        $cache->expire('perm_exists_' . $this->_cacheVersion . $name);
+        if ($this->_cache) {
+            $this->_cache->expire('perm_' . $this->_cacheVersion . $name);
+            $this->_cache->expire('perm_exists_' . $this->_cacheVersion . $name);
+        }
     }
 
 }
index 76224d5..9ffefac 100644 (file)
@@ -22,20 +22,39 @@ class Horde_Perms_Permission_SqlObject extends Horde_Perms_Permission
     protected $_id;
 
     /**
+     * Cache object.
+     *
+     * @var Horde_Cache
+     */
+    protected $_cache;
+
+    /**
      * Database handle for saving changes.
      *
-     * @var DB
+     * @var Horde_Db_Adapter_Base
      */
-    protected $_write_db;
+    protected $_db;
 
     /**
-     * Associates a DB object with this share.
+     * Tasks to run on serialize().
      *
-     * @param DB $write_db  The DB object.
+     * @return array  Parameters that are stored.
      */
-    public function setSqlOb($write_db)
+    public function __sleep()
     {
-        $this->_write_db = $write_db;
+        return array_diff(array_keys(get_class_vars(__CLASS__)), array('_cache', '_db'));
+    }
+
+    /**
+     * Sets the helper functions within the object.
+     *
+     * @param Horde_Cache $cache         The cache object.
+     * @param Horde_Db_Adapter_Base $db  The database object.
+     */
+    public function setObs(Horde_Cache $cache, Horde_Db_Adapter_Base $db)
+    {
+        $this->_cache = $cache;
+        $this->_db = $db;
     }
 
     /**
@@ -66,22 +85,26 @@ class Horde_Perms_Permission_SqlObject extends Horde_Perms_Permission
      */
     public function save()
     {
+        if (!isset($this->_db)) {
+            throw new Horde_Perms_Exception('Cannot save because the DB instances has not been set in this object.');
+        }
+
         $name = $this->getName();
         if (empty($name)) {
             throw new Horde_Perms_Exception('Permission names must be non-empty');
         }
+
         $query = 'UPDATE horde_perms SET perm_data = ? WHERE perm_id = ?';
         $params = array(serialize($this->data), $this->getId());
 
         try {
-            $this->_write_db->update($query, $params);
+            $this->_db->update($query, $params);
         } catch (Horde_Db_Exception $e) {
             throw new Horde_Perms_Exception($e);
         }
 
-        $cache = $GLOBALS['injector']->getInstance('Horde_Cache');
-        $cache->expire('perm_sql_' . $this->_cacheVersion . $name);
-        $cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name);
+        $this->_cache->expire('perm_sql_' . $this->_cacheVersion . $name);
+        $this->_cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name);
     }
 
 }
index 2da835f..153f2c0 100644 (file)
@@ -95,7 +95,10 @@ class Horde_Perms_Sql extends Horde_Perms
             } catch (Horde_Perms_Exception $e) {}
         }
 
-        return new Horde_Perms_Permission_SqlObject($name, $this->_cacheVersion, $type, $params);
+        $ob = new Horde_Perms_Permission_SqlObject($name, $this->_cacheVersion, $type, $params);
+        $ob->setObs($this->_cache, $this->_db);
+
+        return $ob;
     }
 
     /**
@@ -139,7 +142,7 @@ class Horde_Perms_Sql extends Horde_Perms
             $this->_permsCache[$name] = unserialize($perm);
         }
 
-        $this->_permsCache[$name]->setSQLOb($this->_db);
+        $this->_permsCache[$name]->setObs($this->_cache, $this->_db);
 
         return $this->_permsCache[$name];
     }
@@ -174,7 +177,7 @@ class Horde_Perms_Sql extends Horde_Perms
             $object = new Horde_Perms_Permission_SqlObject($result['perm_name'], $this->_cacheVersion);
             $object->setId($id);
             $object->setData(unserialize($result['perm_data']));
-            $object->setSQLOb($this->_db);
+            $object->setObs($this->_cache, $this->_db);
         }
 
         return $object;