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