Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
factory.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Database
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('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  */
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  * @throws RuntimeException
45  */
46  public function getDriver($name = 'mysqli', $options = array())
47  {
48  // Sanitize the database connector options.
49  $options['driver'] = preg_replace('/[^A-Z0-9_\.-]/i', '', $name);
50  $options['database'] = (isset($options['database'])) ? $options['database'] : null;
51  $options['select'] = (isset($options['select'])) ? $options['select'] : true;
52 
53  // Derive the class name from the driver.
54  $class = 'JDatabaseDriver' . ucfirst(strtolower($options['driver']));
55 
56  // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
57  if (!class_exists($class))
58  {
59  throw new RuntimeException(sprintf('Unable to load Database Driver: %s', $options['driver']));
60  }
61 
62  // Create our new JDatabaseDriver connector based on the options given.
63  try
64  {
65  $instance = new $class($options);
66  }
67  catch (RuntimeException $e)
68  {
69  throw new RuntimeException(sprintf('Unable to connect to the Database: %s', $e->getMessage()));
70  }
71 
72  return $instance;
73  }
74 
75  /**
76  * Gets an exporter class object.
77  *
78  * @param string $name Name of the driver you want an exporter for.
79  * @param JDatabaseDriver $db Optional JDatabaseDriver instance
80  *
81  * @return JDatabaseExporter An exporter object.
82  *
83  * @since 12.1
84  * @throws RuntimeException
85  */
86  public function getExporter($name, JDatabaseDriver $db = null)
87  {
88  // Derive the class name from the driver.
89  $class = 'JDatabaseExporter' . ucfirst(strtolower($name));
90 
91  // Make sure we have an exporter class for this driver.
92  if (!class_exists($class))
93  {
94  // If it doesn't exist we are at an impasse so throw an exception.
95  throw new RuntimeException('Database Exporter not found.');
96  }
97 
98  $o = new $class;
99 
100  if ($db instanceof JDatabaseDriver)
101  {
102  $o->setDbo($db);
103  }
104 
105  return $o;
106  }
107 
108  /**
109  * Gets an importer class object.
110  *
111  * @param string $name Name of the driver you want an importer for.
112  * @param JDatabaseDriver $db Optional JDatabaseDriver instance
113  *
114  * @return JDatabaseImporter An importer object.
115  *
116  * @since 12.1
117  * @throws RuntimeException
118  */
119  public function getImporter($name, JDatabaseDriver $db = null)
120  {
121  // Derive the class name from the driver.
122  $class = 'JDatabaseImporter' . ucfirst(strtolower($name));
123 
124  // Make sure we have an importer class for this driver.
125  if (!class_exists($class))
126  {
127  // If it doesn't exist we are at an impasse so throw an exception.
128  throw new RuntimeException('Database importer not found.');
129  }
130 
131  $o = new $class;
132 
133  if ($db instanceof JDatabaseDriver)
134  {
135  $o->setDbo($db);
136  }
137 
138  return $o;
139  }
140 
141  /**
142  * Gets an instance of the factory object.
143  *
144  * @return JDatabaseFactory
145  *
146  * @since 12.1
147  */
148  public static function getInstance()
149  {
150  return self::$_instance ? self::$_instance : new JDatabaseFactory;
151  }
152 
153  /**
154  * Get the current query object or a new JDatabaseQuery object.
155  *
156  * @param string $name Name of the driver you want an query object for.
157  * @param JDatabaseDriver $db Optional JDatabaseDriver instance
158  *
159  * @return JDatabaseQuery The current query object or a new object extending the JDatabaseQuery class.
160  *
161  * @since 12.1
162  * @throws RuntimeException
163  */
164  public function getQuery($name, JDatabaseDriver $db = null)
165  {
166  // Derive the class name from the driver.
167  $class = 'JDatabaseQuery' . ucfirst(strtolower($name));
168 
169  // Make sure we have a query class for this driver.
170  if (!class_exists($class))
171  {
172  // If it doesn't exist we are at an impasse so throw an exception.
173  throw new RuntimeException('Database Query class not found');
174  }
175 
176  return new $class($db);
177  }
178 
179  /**
180  * Gets an instance of a factory object to return on subsequent calls of getInstance.
181  *
182  * @param JDatabaseFactory $instance A JDatabaseFactory object.
183  *
184  * @return void
185  *
186  * @since 12.1
187  */
188  public static function setInstance(JDatabaseFactory $instance = null)
189  {
190  self::$_instance = $instance;
191  }
192 }