Add componentFactory pattern to always create a specific object if one is
authorChuck Hagenbuch <chuck@horde.org>
Sat, 20 Dec 2008 05:20:49 +0000 (00:20 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Sat, 20 Dec 2008 05:20:49 +0000 (00:20 -0500)
present, but fall back to the abstract objects without having to think about it
each time. Gets rid of a few duplicate methods also.

framework/Db/lib/Horde/Db/Adapter/Abstract.php
framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php
framework/Db/lib/Horde/Db/Adapter/Abstract/TableDefinition.php
framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php
framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php
framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php

index ce39a8d..d25e2a4 100644 (file)
@@ -134,6 +134,29 @@ abstract class Horde_Db_Adapter_Abstract
 
 
     /*##########################################################################
+    # Object factory
+    ##########################################################################*/
+
+    /**
+     * Delegate calls to the schema object.
+     *
+     * @param  string  $method
+     * @param  array   $args
+     */
+    public function componentFactory($component, $args)
+    {
+        $class = str_replace('_Schema', '', $this->_schemaClass) . '_' . $component;
+        if (class_exists($class)) {
+            $class = new ReflectionClass($class);
+        } else {
+            $class = new ReflectionClass('Horde_Db_Adapter_Abstract_' . $component);
+        }
+
+        return $class->newInstanceArgs($args);
+    }
+
+
+    /*##########################################################################
     # Object composition
     ##########################################################################*/
 
index 733620a..7cb206d 100644 (file)
@@ -330,10 +330,9 @@ abstract class Horde_Db_Adapter_Abstract_Schema
      */
     public function createTable($name, $options=array())
     {
-        $pk = isset($options['primaryKey']) &&
-              $options['primaryKey'] === false ? false : 'id';
+        $pk = isset($options['primaryKey']) && $options['primaryKey'] === false ? false : 'id';
         $tableDefinition =
-            new Horde_Db_Adapter_Abstract_TableDefinition($name, $this, $options);
+            $this->componentFactory('TableDefinition', array($name, $this, $options));
         if ($pk != false) {
             $tableDefinition->primaryKey($pk);
         }
@@ -579,7 +578,7 @@ abstract class Horde_Db_Adapter_Abstract_Schema
      * @param   string  $table
      * @return  string
      */
-    abstract public function structureDump($table=null);
+    abstract public function structureDump($table = null);
 
     /**
      * Recreate the given db
index 06d2e2b..f57f193 100644 (file)
@@ -108,8 +108,8 @@ class Horde_Db_Adapter_Abstract_TableDefinition implements ArrayAccess
         if ($this[$name]) {
             $column = $this[$name];
         } else {
-            $column = new Horde_Db_Adapter_Abstract_ColumnDefinition(
-                $this->_base, $name, $type);
+            $column = $this->_base->componentFactory('ColumnDefinition', array(
+                $this->_base, $name, $type));
         }
 
         $natives = $this->_native();
index 12b1d1d..42db43d 100644 (file)
@@ -207,29 +207,13 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Abstract_Schema
         // create columns from rows
         $columns = array();
         foreach ($rows as $row) {
-            $columns[] = new Horde_Db_Adapter_Mysql_Column(
-                $row[0], $row[4], $row[1], $row[2] == 'YES');
+            $columns[] = $this->componentFactory('Column', array(
+                $row[0], $row[4], $row[1], $row[2] == 'YES'));
         }
         return $columns;
     }
 
     /**
-     * Override createTable to return a Mysql Table Definition
-     * param    string  $name
-     * param    array   $options
-     */
-    public function createTable($name, $options=array())
-    {
-        $pk = isset($options['primaryKey']) && $options['primaryKey'] === false ? false : 'id';
-        $tableDefinition =
-            new Horde_Db_Adapter_Mysql_TableDefinition($name, $this, $options);
-        if ($pk != false) {
-            $tableDefinition->primaryKey($pk);
-        }
-        return $tableDefinition;
-    }
-
-    /**
      * @param   string  $name
      * @param   array   $options
      */
index 45fb635..03a82df 100644 (file)
@@ -263,8 +263,8 @@ class Horde_Db_Adapter_Postgresql_Schema extends Horde_Db_Adapter_Abstract_Schem
         // create columns from rows
         $columns = array();
         foreach ($rows as $row) {
-            $columns[] = new Horde_Db_Adapter_Postgresql_Column(
-                $row[0], $row[2], $row[1], !(boolean)$row[3]);
+            $columns[] = $this->componentFactory('Column', array(
+                $row[0], $row[2], $row[1], !(boolean)$row[3]));
         }
         return $columns;
     }
index 3fe7ee7..4114978 100644 (file)
@@ -172,29 +172,13 @@ class Horde_Db_Adapter_Sqlite_Schema extends Horde_Db_Adapter_Abstract_Schema
         // create columns from rows
         $columns = array();
         foreach ($rows as $row) {
-            $columns[] = new Horde_Db_Adapter_Sqlite_Column(
-                $row[1], $row[4], $row[2], !(bool)$row[3]);
+            $columns[] = $this->componentFactory('Column', array(
+                $row[1], $row[4], $row[2], !(bool)$row[3]));
         }
         return $columns;
     }
 
     /**
-     * Override createTable to return a Sqlite Table Definition
-     * param    string  $name
-     * param    array   $options
-     */
-    public function createTable($name, $options=array())
-    {
-        $pk = isset($options['primaryKey']) && $options['primaryKey'] === false ? false : 'id';
-        $tableDefinition =
-            new Horde_Db_Adapter_Abstract_TableDefinition($name, $this, $options);
-        if ($pk != false) {
-            $tableDefinition->primaryKey($pk);
-        }
-        return $tableDefinition;
-    }
-
-    /**
      * @param   string  $name
      * @param   string  $newName
      */