1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage database
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 *
8 * This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning FOFTable objects
9 * instead of plain stdClass objects
10 */
11
12 // Protect from unauthorized access
13 defined('FOF_INCLUDED') or die;
14
15 /**
16 * Joomla Platform Database Factory class
17 *
18 * @since 12.1
19 */
20 class FOFDatabaseFactory
21 {
22 /**
23 * Contains the current FOFDatabaseFactory instance
24 *
25 * @var FOFDatabaseFactory
26 * @since 12.1
27 */
28 private static $_instance = null;
29
30 /**
31 * Method to return a FOFDatabaseDriver instance based on the given options. There are three global options and then
32 * the rest are specific to the database driver. The 'database' option determines which database is to
33 * be used for the connection. The 'select' option determines whether the connector should automatically select
34 * the chosen database.
35 *
36 * Instances are unique to the given options and new objects are only created when a unique options array is
37 * passed into the method. This ensures that we don't end up with unnecessary database connection resources.
38 *
39 * @param string $name Name of the database driver you'd like to instantiate
40 * @param array $options Parameters to be passed to the database driver.
41 *
42 * @return FOFDatabaseDriver A database driver object.
43 *
44 * @since 12.1
45 * @throws RuntimeException
46 */
47 public function getDriver($name = 'joomla', $options = array())
48 {
49 // Sanitize the database connector options.
50 $options['driver'] = preg_replace('/[^A-Z0-9_\.-]/i', '', $name);
51 $options['database'] = (isset($options['database'])) ? $options['database'] : null;
52 $options['select'] = (isset($options['select'])) ? $options['select'] : true;
53
54 // Derive the class name from the driver.
55 $class = 'FOFDatabaseDriver' . ucfirst(strtolower($options['driver']));
56
57 // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
58 if (!class_exists($class))
59 {
60 throw new RuntimeException(sprintf('Unable to load Database Driver: %s', $options['driver']));
61 }
62
63 // Create our new FOFDatabaseDriver connector based on the options given.
64 try
65 {
66 $instance = new $class($options);
67 }
68 catch (RuntimeException $e)
69 {
70 throw new RuntimeException(sprintf('Unable to connect to the Database: %s', $e->getMessage()), $e->getCode(), $e);
71 }
72
73 return $instance;
74 }
75
76 /**
77 * Gets an instance of the factory object.
78 *
79 * @return FOFDatabaseFactory
80 *
81 * @since 12.1
82 */
83 public static function getInstance()
84 {
85 return self::$_instance ? self::$_instance : new FOFDatabaseFactory;
86 }
87
88 /**
89 * Get the current query object or a new FOFDatabaseQuery object.
90 *
91 * @param string $name Name of the driver you want an query object for.
92 * @param FOFDatabaseDriver $db Optional FOFDatabaseDriver instance
93 *
94 * @return FOFDatabaseQuery The current query object or a new object extending the FOFDatabaseQuery class.
95 *
96 * @since 12.1
97 * @throws RuntimeException
98 */
99 public function getQuery($name, FOFDatabaseDriver $db = null)
100 {
101 // Derive the class name from the driver.
102 $class = 'FOFDatabaseQuery' . ucfirst(strtolower($name));
103
104 // Make sure we have a query class for this driver.
105 if (!class_exists($class))
106 {
107 // If it doesn't exist we are at an impasse so throw an exception.
108 throw new RuntimeException('Database Query class not found');
109 }
110
111 return new $class($db);
112 }
113
114 /**
115 * Gets an instance of a factory object to return on subsequent calls of getInstance.
116 *
117 * @param FOFDatabaseFactory $instance A FOFDatabaseFactory object.
118 *
119 * @return void
120 *
121 * @since 12.1
122 */
123 public static function setInstance(FOFDatabaseFactory $instance = null)
124 {
125 self::$_instance = $instance;
126 }
127 }
128