PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/ginatrapani/ThinkUp
PHP | 219 lines | 87 code | 3 blank | 129 comment | 3 complexity | 7630f674aa5e87470e166ddf973bd05b MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/_lib/model/class.DAOFactory.php
  5. *
  6. * Copyright (c) 2009-2012 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-2012 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. //Invite DAO
  63. 'InviteDAO' => array(
  64. //MySQL Version
  65. 'mysql' => 'InviteMySQLDAO' ),
  66. //Follow DAO
  67. 'FollowDAO' => array(
  68. //MySQL Version
  69. 'mysql' => 'FollowMySQLDAO' ),
  70. //Post Error DAO
  71. 'PostErrorDAO' => array(
  72. //MySQL Version
  73. 'mysql' => 'PostErrorMySQLDAO' ),
  74. //Post DAO
  75. 'PostDAO' => array(
  76. //MySQL Version
  77. 'mysql' => 'PostMySQLDAO' ),
  78. //Export DAO
  79. 'ExportDAO' => array(
  80. //MySQL Version
  81. 'mysql' => 'ExportMySQLDAO' ),
  82. //FavoritePost DAO
  83. 'FavoritePostDAO' => array(
  84. //MySQL Version
  85. 'mysql' => 'FavoritePostMySQLDAO' ),
  86. //User DAO
  87. 'UserDAO' => array(
  88. //MySQL Version
  89. 'mysql' => 'UserMySQLDAO' ),
  90. //UserError DAO
  91. 'UserErrorDAO' => array(
  92. //MySQL Version
  93. 'mysql' => 'UserErrorMySQLDAO' ),
  94. //Location DAO
  95. 'LocationDAO' => array(
  96. //MySQL Version
  97. 'mysql' => 'LocationMySQLDAO' ),
  98. //Link DAO
  99. 'LinkDAO' => array(
  100. //MySQL Version
  101. 'mysql' => 'LinkMySQLDAO' ),
  102. //Hashtag DAO
  103. 'HashtagDAO' => array(
  104. //MySQL Version
  105. 'mysql' => 'HashtagMySQLDAO' ),
  106. //Mention DAO
  107. 'MentionDAO' => array(
  108. //MySQL Version
  109. 'mysql' => 'MentionMySQLDAO' ),
  110. //Place DAO
  111. 'PlaceDAO' => array(
  112. //MySQL Version
  113. 'mysql' => 'PlaceMySQLDAO' ),
  114. //StreamData DAO
  115. 'StreamDataDAO' => array(
  116. //MySQL Version
  117. 'mysql' => 'StreamDataMySQLDAO' ),
  118. //StreamProc DAO
  119. 'StreamProcDAO' => array(
  120. //MySQL Version
  121. 'mysql' => 'StreamProcMySQLDAO' ),
  122. //Owner MySQL DAO
  123. 'OwnerDAO' => array(
  124. //MySQL Version
  125. 'mysql' => 'OwnerMySQLDAO' ),
  126. //OwnerInstance MySQL DAO
  127. 'OwnerInstanceDAO' => array(
  128. //MySQL Version
  129. 'mysql' => 'OwnerInstanceMySQLDAO' ),
  130. //Plugin MySQL DAO
  131. 'PluginDAO' => array(
  132. //MySQL Version
  133. 'mysql' => 'PluginMySQLDAO' ),
  134. //Plugin Option MySQL DAO
  135. 'PluginOptionDAO' => array(
  136. //MySQL Version
  137. 'mysql' => 'PluginOptionMySQLDAO' ),
  138. //Follower Count MySQL DAO
  139. 'FollowerCountDAO' => array(
  140. //MySQL Version
  141. 'mysql' => 'FollowerCountMySQLDAO'),
  142. //Installer MySQL DAO
  143. 'InstallerDAO' => array (
  144. //MySQL Version
  145. 'mysql' => 'InstallerMySQLDAO'),
  146. //Option MySQL DAO
  147. 'OptionDAO' => array (
  148. //MySQL Version
  149. 'mysql' => 'OptionMySQLDAO'),
  150. //Backup MySQL DAO
  151. 'BackupDAO' => array (
  152. //MySQL Version
  153. 'mysql' => 'BackupMySQLDAO'),
  154. //Mutex MySQL DAO
  155. 'MutexDAO' => array (
  156. //MySQL Version
  157. 'mysql' => 'MutexMySQLDAO'),
  158. //Group MySQL DAO
  159. 'GroupDAO' => array (
  160. //MySQL Version
  161. 'mysql' => 'GroupMySQLDAO'),
  162. //Group Membership Count MySQL DAO
  163. 'GroupMembershipCountDAO' => array (
  164. //MySQL Version
  165. 'mysql' => 'GroupMembershipCountMySQLDAO'),
  166. //Group Member MySQL DAO
  167. 'GroupMemberDAO' => array (
  168. //MySQL Version
  169. 'mysql' => 'GroupMemberMySQLDAO'),
  170. //Group Owner MySQL DAO
  171. 'GroupOwnerDAO' => array (
  172. //MySQL Version
  173. 'mysql' => 'GroupOwnerMySQLDAO'),
  174. //TableStats MySQL DAO
  175. 'TableStatsDAO' => array (
  176. //MySQL Version
  177. 'mysql' => 'TableStatsMySQLDAO')
  178. );
  179. /*
  180. * Creates a DAO instance and returns it
  181. *
  182. * @param string $dao_key the name of the dao you wish to init
  183. * @param array $cfg_vals Optionally override config.inc.php vals; needs 'table_prefix', 'db_type',
  184. * 'db_socket', 'db_name', 'db_host', 'db_user', 'db_password'
  185. * @returns PDODAO A concrete dao instance
  186. */
  187. public static function getDAO($dao_key, $cfg_vals=null) {
  188. $db_type = self::getDBType($cfg_vals);
  189. if (! isset(self::$dao_mapping[$dao_key]) ) {
  190. throw new Exception("No DAO mapping defined for: " . $dao_key);
  191. }
  192. if (! isset(self::$dao_mapping[$dao_key][$db_type])) {
  193. throw new Exception("No db mapping defined for '" . $dao_key . "' with db type: " . $db_type);
  194. }
  195. $class_name = self::$dao_mapping[$dao_key][$db_type];
  196. $dao = new $class_name($cfg_vals);
  197. return $dao;
  198. }
  199. /**
  200. * Gets the db_type for our configured ThinkUp instance, defaults to mysql,
  201. * db_type can optionally be defined in webapp/config.inc as:
  202. *
  203. *<code>
  204. * $THINKUP_CFG['db_type'] = 'somedb';
  205. *</code>
  206. *
  207. * @param array $cfg_vals Optionally override config.inc.php vals; needs 'table_prefix', 'db_type',
  208. * 'db_socket', 'db_name', 'db_host', 'db_user', 'db_password'
  209. * @return string db_type, will default to 'mysql' if not defined
  210. */
  211. public static function getDBType($cfg_vals=null) {
  212. $type = Config::getInstance($cfg_vals)->getValue('db_type');
  213. $type = is_null($type) ? 'mysql' : $type;
  214. return $type;
  215. }
  216. }