1: <?php
2: /**
3: * You may not change or alter any portion of this comment or credits
4: * of supporting developers from this source code or any supporting source code
5: * which is considered copyrighted (c) material of the original comment or credit authors.
6: *
7: * This program is distributed in the hope that it will be useful,
8: * but WITHOUT ANY WARRANTY; without even the implied warranty of
9: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Database;
13:
14: use Xoops\Core\Database\Logging\XoopsDebugStack;
15:
16: /**
17: * Xoops Database Factory class
18: *
19: * PHP version 5.3
20: *
21: * @category Xoops\Class\Database\Factory
22: * @package Factory
23: * @author Kazumi Ono <onokazu@xoops.org>
24: * @author readheadedrod <redheadedrod@hotmail.com>
25: * @copyright 2013-2014 XOOPS Project (http://xoops.org)
26: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
27: * @version Release:2.6
28: * @link http://xoops.org
29: * @since 2.6.0
30: */
31: class Factory
32: {
33:
34: /**
35: * Get a reference to the only instance of database class and connects to DB
36: *
37: * if the class has not been instantiated yet, this will also take
38: * care of that
39: *
40: * Doctrine connection function
41: *
42: * NOTE: Persistance connection is not included. XOOPS_DB_PCONNECT is ignored.
43: * allowWebChanges also needs to be addressed
44: *
45: * @param array $options driverOptions for Doctrine
46: *
47: * @return Connection|null Reference to the only instance of database class
48: *
49: * @todo change driver to support other databases and support for port, unix_socket and driver options.
50: */
51: public static function getConnection($options = null)
52: {
53: static $instance;
54: if (!isset($instance)) {
55: $xoops = \Xoops::getInstance();
56: $config = new \Doctrine\DBAL\Configuration();
57: $config->setSQLLogger(new XoopsDebugStack());
58: $parameters = \XoopsBaseConfig::get('db-parameters');
59: if (!empty($parameters) && is_array($parameters)) {
60: $connectionParams = $parameters;
61: $connectionParams['wrapperClass'] = '\Xoops\Core\Database\Connection';
62: } else {
63: $driver = 'pdo_' . \XoopsBaseConfig::get('db-type');
64: $connectionParams = array(
65: 'dbname' => \XoopsBaseConfig::get('db-name'),
66: 'user' => \XoopsBaseConfig::get('db-user'),
67: 'password' => \XoopsBaseConfig::get('db-pass'),
68: 'host' => \XoopsBaseConfig::get('db-host'),
69: 'charset' => \XoopsBaseConfig::get('db-charset'),
70: 'driver' => $driver,
71: 'wrapperClass' => '\Xoops\Core\Database\Connection',
72: );
73: // Support for other doctrine databases
74: $xoops_db_port = \XoopsBaseConfig::get('db-port');
75: if (!empty($xoops_db_port)) {
76: $connectionParams['port'] = $xoops_db_port;
77: }
78: $xoops_db_socket = \XoopsBaseConfig::get('db-socket');
79: if (!empty($xoops_db_socket)) {
80: $connectionParams['unix_socket'] = $xoops_db_socket;
81: }
82: if (!is_null($options) && is_array($options)) {
83: $connectionParams['driverOptions'] = $options;
84: }
85: }
86:
87: $instance = \Doctrine\DBAL\DriverManager::getConnection(
88: $connectionParams,
89: $config
90: );
91: }
92: return $instance;
93: }
94: }
95: