1<?php
2/**
3 * Factory Class for XOOPS Database
4 *
5 * You may not change or alter any portion of this comment or credits
6 * of supporting developers from this source code or any supporting source code
7 * which is considered copyrighted (c) material of the original comment or credit authors.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14 * @package             kernel
15 * @subpackage          database
16 */
17defined('XOOPS_ROOT_PATH') || exit('Restricted access');
18
19/**
20 * XoopsDatabaseFactory
21 *
22 * @package Kernel
23 * @author  Kazumi Ono <onokazu@xoops.org>
24 * @access  public
25 */
26class XoopsDatabaseFactory
27{
28    /**
29     * XoopsDatabaseFactory constructor.
30     */
31    public function __construct()
32    {
33    }
34
35    /**
36     * Get a reference to the only instance of database class and connects to DB
37     *
38     * if the class has not been instantiated yet, this will also take
39     * care of that
40     *
41     * @static
42     * @staticvar XoopsDatabase The only instance of database class
43     * @return XoopsDatabase Reference to the only instance of database class
44     */
45    public static function getDatabaseConnection()
46    {
47        static $instance;
48        if (!isset($instance)) {
49            if (file_exists($file = XOOPS_ROOT_PATH . '/class/database/' . XOOPS_DB_TYPE . 'database.php')) {
50                require_once $file;
51
52                if (!defined('XOOPS_DB_PROXY')) {
53                    $class = 'Xoops' . ucfirst(XOOPS_DB_TYPE) . 'DatabaseSafe';
54                } else {
55                    $class = 'Xoops' . ucfirst(XOOPS_DB_TYPE) . 'DatabaseProxy';
56                }
57
58                $xoopsPreload = XoopsPreload::getInstance();
59                $xoopsPreload->triggerEvent('core.class.database.databasefactory.connection', array(&$class));
60
61                $instance = new $class();
62                $instance->setLogger(XoopsLogger::getInstance());
63                $instance->setPrefix(XOOPS_DB_PREFIX);
64                if (!$instance->connect()) {
65                    trigger_error('notrace:Unable to connect to database', E_USER_ERROR);
66                }
67            } else {
68                trigger_error('notrace:Failed to load database of type: ' . XOOPS_DB_TYPE . ' in file: ' . __FILE__ . ' at line ' . __LINE__, E_USER_WARNING);
69            }
70        }
71
72        return $instance;
73    }
74
75    /**
76     * Gets a reference to the only instance of database class. Currently
77     * only being used within the installer.
78     *
79     * @static
80     * @staticvar object  The only instance of database class
81     * @return object Reference to the only instance of database class
82     */
83    public static function getDatabase()
84    {
85        static $database;
86        if (!isset($database)) {
87            if (file_exists($file = XOOPS_ROOT_PATH . '/class/database/' . XOOPS_DB_TYPE . 'database.php')) {
88                include_once $file;
89                if (!defined('XOOPS_DB_PROXY')) {
90                    $class = 'Xoops' . ucfirst(XOOPS_DB_TYPE) . 'DatabaseSafe';
91                } else {
92                    $class = 'Xoops' . ucfirst(XOOPS_DB_TYPE) . 'DatabaseProxy';
93                }
94                unset($database);
95                $database = new $class();
96            } else {
97                trigger_error('notrace:Failed to load database of type: ' . XOOPS_DB_TYPE . ' in file: ' . __FILE__ . ' at line ' . __LINE__, E_USER_WARNING);
98            }
99        }
100
101        return $database;
102    }
103}
104