/libraries/joomla/database/factory.php

https://bitbucket.org/eternaware/joomus · PHP · 191 lines · 71 code · 22 blank · 98 comment · 6 complexity · 65f7f9c1bae35a10c8e16c672dfc7ebc MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Database
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Joomla Platform Database Factory class
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Database
  15. * @since 12.1
  16. */
  17. class JDatabaseFactory
  18. {
  19. /**
  20. * Contains the current JDatabaseFactory instance
  21. *
  22. * @var JDatabaseFactory
  23. * @since 12.1
  24. */
  25. private static $_instance = null;
  26. /**
  27. * Method to return a JDatabaseDriver instance based on the given options. There are three global options and then
  28. * the rest are specific to the database driver. The 'database' option determines which database is to
  29. * be used for the connection. The 'select' option determines whether the connector should automatically select
  30. * the chosen database.
  31. *
  32. * Instances are unique to the given options and new objects are only created when a unique options array is
  33. * passed into the method. This ensures that we don't end up with unnecessary database connection resources.
  34. *
  35. * @param string $name Name of the database driver you'd like to instantiate
  36. * @param array $options Parameters to be passed to the database driver.
  37. *
  38. * @return JDatabaseDriver A database driver object.
  39. *
  40. * @since 12.1
  41. */
  42. public function getDriver($name = 'mysqli', $options = array())
  43. {
  44. // Sanitize the database connector options.
  45. $options['driver'] = preg_replace('/[^A-Z0-9_\.-]/i', '', $name);
  46. $options['database'] = (isset($options['database'])) ? $options['database'] : null;
  47. $options['select'] = (isset($options['select'])) ? $options['select'] : true;
  48. // Derive the class name from the driver.
  49. $class = 'JDatabaseDriver' . ucfirst(strtolower($options['driver']));
  50. // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
  51. if (!class_exists($class))
  52. {
  53. throw new RuntimeException(sprintf('Unable to load Database Driver: %s', $options['driver']));
  54. }
  55. // Create our new JDatabaseDriver connector based on the options given.
  56. try
  57. {
  58. $instance = new $class($options);
  59. }
  60. catch (RuntimeException $e)
  61. {
  62. throw new RuntimeException(sprintf('Unable to connect to the Database: %s', $e->getMessage()));
  63. }
  64. return $instance;
  65. }
  66. /**
  67. * Gets an exporter class object.
  68. *
  69. * @param string $name Name of the driver you want an exporter for.
  70. * @param JDatabaseDriver $db Optional JDatabaseDriver instance
  71. *
  72. * @return JDatabaseExporter An exporter object.
  73. *
  74. * @since 12.1
  75. * @throws RuntimeException
  76. */
  77. public function getExporter($name, JDatabaseDriver $db = null)
  78. {
  79. // Derive the class name from the driver.
  80. $class = 'JDatabaseExporter' . ucfirst(strtolower($name));
  81. // Make sure we have an exporter class for this driver.
  82. if (!class_exists($class))
  83. {
  84. // If it doesn't exist we are at an impasse so throw an exception.
  85. throw new RuntimeException('Database Exporter not found.');
  86. }
  87. $o = new $class;
  88. if ($db instanceof JDatabaseDriver)
  89. {
  90. $o->setDbo($db);
  91. }
  92. return $o;
  93. }
  94. /**
  95. * Gets an importer class object.
  96. *
  97. * @param string $name Name of the driver you want an importer for.
  98. * @param JDatabaseDriver $db Optional JDatabaseDriver instance
  99. *
  100. * @return JDatabaseImporter An importer object.
  101. *
  102. * @since 12.1
  103. * @throws RuntimeException
  104. */
  105. public function getImporter($name, JDatabaseDriver $db = null)
  106. {
  107. // Derive the class name from the driver.
  108. $class = 'JDatabaseImporter' . ucfirst(strtolower($name));
  109. // Make sure we have an importer class for this driver.
  110. if (!class_exists($class))
  111. {
  112. // If it doesn't exist we are at an impasse so throw an exception.
  113. throw new RuntimeException('Database importer not found.');
  114. }
  115. $o = new $class;
  116. if ($db instanceof JDatabaseDriver)
  117. {
  118. $o->setDbo($db);
  119. }
  120. return $o;
  121. }
  122. /**
  123. * Gets an instance of the factory object.
  124. *
  125. * @return JDatabaseFactory
  126. *
  127. * @since 12.1
  128. */
  129. public static function getInstance()
  130. {
  131. return self::$_instance ? self::$_instance : new JDatabaseFactory;
  132. }
  133. /**
  134. * Get the current query object or a new JDatabaseQuery object.
  135. *
  136. * @param string $name Name of the driver you want an importer for.
  137. * @param JDatabaseDriver $db Optional JDatabaseDriver instance
  138. *
  139. * @return JDatabaseQuery The current query object or a new object extending the JDatabaseQuery class.
  140. *
  141. * @since 12.1
  142. * @throws RuntimeException
  143. */
  144. public function getQuery($name, JDatabaseDriver $db = null)
  145. {
  146. // Derive the class name from the driver.
  147. $class = 'JDatabaseQuery' . ucfirst(strtolower($name));
  148. // Make sure we have a query class for this driver.
  149. if (!class_exists($class))
  150. {
  151. // If it doesn't exist we are at an impasse so throw an exception.
  152. throw new RuntimeException('Database Query class not found');
  153. }
  154. return new $class($db);
  155. }
  156. /**
  157. * Gets an instance of a factory object to return on subsequent calls of getInstance.
  158. *
  159. * @param JDatabaseFactory $instance A JDatabaseFactory object.
  160. *
  161. * @return void
  162. *
  163. * @since 12.1
  164. */
  165. public static function setInstance(JDatabaseFactory $instance = null)
  166. {
  167. self::$_instance = $instance;
  168. }
  169. }