Pastie: Fix initialization of SQL driver
authorBen Klang <ben@alkaloid.net>
Tue, 8 Jun 2010 20:56:32 +0000 (16:56 -0400)
committerBen Klang <ben@alkaloid.net>
Tue, 8 Jun 2010 20:56:32 +0000 (16:56 -0400)
pastie/config/conf.xml
pastie/lib/Driver.php

index 7442ac4..9727214 100644 (file)
@@ -3,14 +3,14 @@
 <configuration>
  <configsection name="storage">
   <configheader>Storage System Settings</configheader>
-  <configswitch name="driver" desc="What storage driver should we use?">sql
-   <case name="sql" desc="SQL">
-    <configsection name="params">
+  <configsection name="params">
+   <configswitch name="driver" desc="What storage driver should we use?">sql
+    <case name="sql" desc="SQL">
      <configsql switchname="driverconfig">
-     </configsql>
-    </configsection>
-   </case>
-  </configswitch>
+     </configsql> 
+    </case>
+   </configswitch>
+  </configsection>
  </configsection>
  <configsection name="highlighter">
   <configheader>Text Highlighting Engine</configheader>
index e1c23df..e799456 100644 (file)
@@ -8,7 +8,7 @@
  * See the enclosed file COPYING for license information (GPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  *
- * @author  Your Name <you@example.com>
+ * @author  Ben Klang <ben@alkaloid.net>
  * @package Pastie
  */
 class Pastie_Driver
@@ -29,14 +29,14 @@ class Pastie_Driver
     static public function factory($driver = null, $params = null)
     {
         if (is_null($driver)) {
-            $driver = $GLOBALS['conf']['storage']['driver'];
+            $driver = $GLOBALS['conf']['storage']['params']['driver'];
         }
-        $driver = ucfirst(basename($driver));
 
         if (is_null($params)) {
             $params = Horde::getDriverConfig('storage', $driver);
         }
 
+        $driver = ucfirst(basename($driver));
         $class = 'Pastie_Driver_' . $driver;
         if (class_exists($class)) {
             return new $class($params);
@@ -45,4 +45,48 @@ class Pastie_Driver
         throw new Horde_Exception('Could not find driver ' . $class);
     }
 
+    /**
+     * Attempts to return a reference to a concrete Pastie_Driver instance based
+     * on $driver.
+     *
+     * It will only create a new instance if no Pastie_Driver instance with the
+     * same parameters currently exists.
+     *
+     * This should be used if multiple storage sources are required.
+     *
+     * This method must be invoked as: $var = &Pastie_Driver::singleton()
+     *
+     * @param string    $notepad    The name of the current notepad.
+     *
+     * @param string    $driver     The type of concrete Pastie_Driver subclass
+     *                              to return.  The is based on the storage
+     *                              driver ($driver).  The code is dynamically
+     *                              included.
+     *
+     * @param array     $params     (optional) A hash containing any additional
+     *                              configuration or connection parameters a
+     *                              subclass might need.
+     *
+     * @return mixed    The created concrete Pastie_Driver instance, or false
+     *                  on error.
+     */
+    function &singleton($bin = '', $driver = null, $params = null)
+    {
+        static $instances = array();
+        if (is_null($driver)) {
+            $driver = $GLOBALS['conf']['storage']['driver'];
+        }
+
+        if (is_null($params)) {
+            $params = Horde::getDriverConfig('storage', $driver);
+        }
+
+        $signature = serialize(array($notepad, $driver, $params));
+        if (!isset($instances[$signature])) {
+            $instances[$signature] = &Pastie_Driver::factory($notepad, $driver, $params);
+        }
+
+        return $instances[$signature];
+    }
+
 }