CVS update: /cowiki/includes/cowiki/class/dao/

[email protected] 12 May 2005 17:03:58 -0000
Newsgroups gmane.comp.php.cowiki.cvs
Message-ID <[email protected]>
User: dgorski 
Date: 2005/05/12 10:03:58

Modified:
   cowiki/includes/cowiki/class/dao/class.StorageFactory.php

Log:
 Refactoring

File Changes:

Directory: /cowiki/includes/cowiki/class/dao/
=============================================

File [changed]: class.StorageFactory.php
Url: http://cowiki.tigris.org/source/browse/cowiki/includes/cowiki/class/dao/class.StorageFactory.php?r1=1.20&r2=1.21
Delta lines:  +45 -24
---------------------
--- class.StorageFactory.php	6 May 2005 02:05:17 -0000	1.20
+++ class.StorageFactory.php	12 May 2005 17:03:55 -0000	1.21
@@ -2,7 +2,7 @@
 
 /**
  *
- * $Id: class.StorageFactory.php,v 1.20 2005/05/06 02:05:17 dgorski Exp $
+ * $Id: class.StorageFactory.php,v 1.21 2005/05/12 17:03:55 dgorski Exp $
  *
  * This file is part of coWiki. coWiki is free software under the terms of
  * the GNU General Public License (GPL). Read the LICENSE file. If you did
@@ -17,7 +17,7 @@
  * @author      Daniel T. Gorski, <[email protected]>
  * @copyright   (C) Daniel T. Gorski, {@link http://www.develnet.org}
  * @license     http://www.gnu.org/licenses/gpl.html
- * @version     $Revision: 1.20 $
+ * @version     $Revision: 1.21 $
  *
  */
 
@@ -37,7 +37,7 @@
 
     protected static
         $Instance = null,
-        $aCache   = null;
+        $aPool    = null;
 
     // --------------------------------------------------------------------
 
@@ -95,7 +95,23 @@
      * @throws  StorageException
      */
     public function createDocumentStorage($sResource) {
-        return $this->create($sResource);
+        // Do we already have a handle?
+        if (isset($this->aPool[$sResource])) {
+            return $this->aPool[$sResource];
+        }
+
+        $UriInfo = new UriInfo($sResource);
+
+        // This method does not select (use) the database ...
+        $Storage = $this->create($UriInfo);
+
+        // ... we need to 'use database' here
+        $Storage->useDatabase($UriInfo->get('basepath'));
+
+        // Remember for future re-retrieval
+        $this->aPool[$sResource] = $Storage;
+
+        return $Storage;
     }
 
     // --------------------------------------------------------------------
@@ -118,17 +134,37 @@
      * @throws  StorageException
      */
     public function createUserStorage($sResource) {
-        return $this->create($sResource);
+
+        // Do we already have a handle?
+        if (isset($this->aPool[$sResource])) {
+            return $this->aPool[$sResource];
+        }
+
+        $UriInfo = new UriInfo($sResource);
+
+        // This method does not select (use) the database ...
+        $Storage = $this->create($UriInfo);
+
+        // ... we need to 'use database' here
+        $Storage->useDatabase($UriInfo->get('basepath'));
+
+        // Remember for future re-retrieval
+        $this->aPool[$sResource] = $Storage;
+
+        return $Storage;
     }
 
     // --------------------------------------------------------------------
 
     /**
-     * Create appropriate storage abstraction layer
+     * Create appropriate storage abstraction layer. The database might
+     * be not chosen if it does not exist but the connection was successful.
+     * The exception StorageDatabaseSelectionException is swallowed. You'll
+     * need to call explicit useDatabase() after calling this method.
      *
      * @access  public
-     * @param   string  Resource DSN
-     * @return  object
+     * @param   object  UriInfo
+     * @return  object  Storage
      *
      * @author  Daniel T. Gorski, <[email protected]>
      * @since   coWiki 0.4.0
@@ -137,19 +173,10 @@
      *
      * @throws  StorageApiException
      * @throws  StorageConnectionFailureException
-     * @throws  StorageDatabaseSelectionException
      * @throws  StorageException
      */
-    public function create($sResource) {
+    public function create($UriInfo) {
 
-        // Resource recycling, might speed up connections a bit
-        if (isset($this->aCache[$sResource])) {
-            return $this->aCache[$sResource];
-        }
-
-        // ---
-
-        $UriInfo = new UriInfo($sResource);
         $sScheme = $UriInfo->get('scheme');
 
         if ($sScheme == 'mysql' || $sScheme == 'mysql+innodb') {
@@ -177,9 +204,6 @@
                     // swallow
                 }
 
-                // Remember for future calls (resource recycling)
-                $this->aCache[$sResource] = $Storage;
-
                 return $Storage;
 
             } catch (StorageConnectionFailureException $scfe) {
@@ -216,9 +240,6 @@
                 } catch (StorageDatabaseSelectionException $sdse) {
                     // swallow
                 }
-
-                // Remember for future calls (resource recycling)
-                $this->aCache[$sResource] = $Storage;
 
                 return $Storage;