PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/_lib/model/class.DAOFactory.php

https://github.com/unruthless/ThinkUp
PHP | 171 lines | 63 code | 3 blank | 105 comment | 3 complexity | 87a2f79d2de55b8229e407349bffadfb MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/_lib/model/class.DAOFactory.php
  5. *
  6. * Copyright (c) 2009-2011 Mark Wilkie, Gina Trapani
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * Data Access Object Factory
  25. *
  26. * Inits a DAO based on the ThinkUp config db_type and $dao_mapping definitions.
  27. * db_type is defined in webapp/config.inc.php as:
  28. *
  29. * $THINKUP_CFG['db_type'] = 'somedb';
  30. *
  31. * Example of use:
  32. *
  33. * <code>
  34. * DAOFactory::getDAO('SomeDAO');
  35. * </code>
  36. *
  37. * @license http://www.gnu.org/licenses/gpl.html
  38. * @copyright 2009-2011 Mark Wilkie, Gina Trapani
  39. * @author Mark Wilkie
  40. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  41. */
  42. class DAOFactory {
  43. /**
  44. * Maps DAO from db_type and defines interface names and class implementation
  45. */
  46. static $dao_mapping = array (
  47. //Test DAO
  48. 'TestDAO' => array(
  49. //MySQL Version
  50. 'mysql' => 'TestMySQLDAO',
  51. //faux Version
  52. 'faux' => 'TestFauxDAO' ),
  53. //Instance DAO
  54. 'InstanceDAO' => array(
  55. //MySQL Version
  56. 'mysql' => 'InstanceMySQLDAO' ),
  57. //@TODO Figure out a way to let a plugin define its DAOs in the plugin code
  58. //Twitter Instance DAO
  59. 'TwitterInstanceDAO' => array(
  60. //MySQL Version
  61. 'mysql' => 'TwitterInstanceMySQLDAO' ),
  62. //Follow DAO
  63. 'FollowDAO' => array(
  64. //MySQL Version
  65. 'mysql' => 'FollowMySQLDAO' ),
  66. //Post Error DAO
  67. 'PostErrorDAO' => array(
  68. //MySQL Version
  69. 'mysql' => 'PostErrorMySQLDAO' ),
  70. //Post DAO
  71. 'PostDAO' => array(
  72. //MySQL Version
  73. 'mysql' => 'PostMySQLDAO' ),
  74. //FavoritePost DAO
  75. 'FavoritePostDAO' => array(
  76. //MySQL Version
  77. 'mysql' => 'FavoritePostMySQLDAO' ),
  78. //User DAO
  79. 'UserDAO' => array(
  80. //MySQL Version
  81. 'mysql' => 'UserMySQLDAO' ),
  82. //UserError DAO
  83. 'UserErrorDAO' => array(
  84. //MySQL Version
  85. 'mysql' => 'UserErrorMySQLDAO' ),
  86. //Location DAO
  87. 'LocationDAO' => array(
  88. //MySQL Version
  89. 'mysql' => 'LocationMySQLDAO' ),
  90. //Link DAO
  91. 'LinkDAO' => array(
  92. //MySQL Version
  93. 'mysql' => 'LinkMySQLDAO' ),
  94. //Owner MySQL DAO
  95. 'OwnerDAO' => array(
  96. //MySQL Version
  97. 'mysql' => 'OwnerMySQLDAO' ),
  98. //OwnerInstance MySQL DAO
  99. 'OwnerInstanceDAO' => array(
  100. //MySQL Version
  101. 'mysql' => 'OwnerInstanceMySQLDAO' ),
  102. //Plugin MySQL DAO
  103. 'PluginDAO' => array(
  104. //MySQL Version
  105. 'mysql' => 'PluginMySQLDAO' ),
  106. //Plugin Option MySQL DAO
  107. 'PluginOptionDAO' => array(
  108. //MySQL Version
  109. 'mysql' => 'PluginOptionMySQLDAO' ),
  110. //Follower Count MySQL DAO
  111. 'FollowerCountDAO' => array(
  112. //MySQL Version
  113. 'mysql' => 'FollowerCountMySQLDAO'),
  114. //Installer MySQL DAO
  115. 'InstallerDAO' => array (
  116. //MySQL Version
  117. 'mysql' => 'InstallerMySQLDAO'),
  118. //Option MySQL DAO
  119. 'OptionDAO' => array (
  120. //MySQL Version
  121. 'mysql' => 'OptionMySQLDAO'),
  122. //Backup MySQL DAO
  123. 'BackupDAO' => array (
  124. //MySQL Version
  125. 'mysql' => 'BackupMySQLDAO'),
  126. //Mutex MySQL DAO
  127. 'MutexDAO' => array (
  128. //MySQL Version
  129. 'mysql' => 'MutexMySQLDAO')
  130. );
  131. /*
  132. * Creates a DAO instance and returns it
  133. *
  134. * @param string $dao_key the name of the dao you wish to init
  135. * @param array $cfg_vals Optionally override config.inc.php vals; needs 'table_prefix', 'db_type',
  136. * 'db_socket', 'db_name', 'db_host', 'db_user', 'db_password'
  137. * @returns PDODAO A concrete dao instance
  138. */
  139. public static function getDAO($dao_key, $cfg_vals=null) {
  140. $db_type = self::getDBType($cfg_vals);
  141. if(! isset(self::$dao_mapping[$dao_key]) ) {
  142. throw new Exception("No DAO mapping defined for: " . $dao_key);
  143. }
  144. if(! isset(self::$dao_mapping[$dao_key][$db_type])) {
  145. throw new Exception("No db mapping defined for '" . $dao_key . "' with db type: " . $db_type);
  146. }
  147. $class_name = self::$dao_mapping[$dao_key][$db_type];
  148. $dao = new $class_name($cfg_vals);
  149. return $dao;
  150. }
  151. /**
  152. * Gets the db_type for our configured ThinkUp instance, defaults to mysql,
  153. * db_type can optionally be defined in webapp/config.inc as:
  154. *
  155. *<code>
  156. * $THINKUP_CFG['db_type'] = 'somedb';
  157. *</code>
  158. *
  159. * @param array $cfg_vals Optionally override config.inc.php vals; needs 'table_prefix', 'db_type',
  160. * 'db_socket', 'db_name', 'db_host', 'db_user', 'db_password'
  161. * @return string db_type, will default to 'mysql' if not defined
  162. */
  163. public static function getDBType($cfg_vals=null) {
  164. $type = Config::getInstance($cfg_vals)->getValue('db_type');
  165. $type = is_null($type) ? 'mysql' : $type;
  166. return $type;
  167. }
  168. }