PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Joomla/Database/DatabaseFactory.php

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