From: Chuck Hagenbuch Date: Sat, 20 Dec 2008 05:20:49 +0000 (-0500) Subject: Add componentFactory pattern to always create a specific object if one is X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=0b3c59b1b8cbafcb02358ddf301d1cad3ff917bc;p=horde.git Add componentFactory pattern to always create a specific object if one is present, but fall back to the abstract objects without having to think about it each time. Gets rid of a few duplicate methods also. --- diff --git a/framework/Db/lib/Horde/Db/Adapter/Abstract.php b/framework/Db/lib/Horde/Db/Adapter/Abstract.php index ce39a8dd4..d25e2a48e 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Abstract.php +++ b/framework/Db/lib/Horde/Db/Adapter/Abstract.php @@ -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 ##########################################################################*/ diff --git a/framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php index 733620a85..7cb206dec 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Abstract/Schema.php @@ -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 diff --git a/framework/Db/lib/Horde/Db/Adapter/Abstract/TableDefinition.php b/framework/Db/lib/Horde/Db/Adapter/Abstract/TableDefinition.php index 06d2e2b26..f57f193d3 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Abstract/TableDefinition.php +++ b/framework/Db/lib/Horde/Db/Adapter/Abstract/TableDefinition.php @@ -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(); diff --git a/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php index 12b1d1d68..42db43d0b 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php @@ -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 */ diff --git a/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php index 45fb6358a..03a82df8c 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Postgresql/Schema.php @@ -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; } diff --git a/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php b/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php index 3fe7ee773..4114978c1 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php +++ b/framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php @@ -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 */