summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/Factory.php
blob: 0ea9009b39bad81b7d5776833d9920e732c94b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

/**
 * Connect to the database and build services.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class SemanticScuttle_Service_Factory
{
    /**
     * Array of service instances.
     * Key is service name (i.e. "Bookmark")
     *
     * @var array
     */
    protected static $instances = array();

    /**
     * Database connection
     *
     * @var sql_qb
     */
    protected static $db = null;

    /**
     * Array of service names -> new service class
     * in case you want to overwrite the services.
     *
     * Key is the old service name (i.e. "Bookmark"),
     * value the new class name, e.g.
     * "My_Cool_Own_BookmarkService"
     *
     * @var array
     */
    protected static $serviceoverrides = array();



    /**
     * Returns the service for the given name.
     *
     * @param string $name Service name (i.e. "Bookmark")
     *
     * @return SemanticScuttle_Service Service object
     */
    public static function get($name)
    {
        self::loadDb();
        self::loadService($name);
        return self::$instances[$name];
    }



    /**
     * Loads service with the given name into
     * self::$instances[$name].
     *
     * @param string $name Service name (i.e. 'Bookmark')
     *
     * @return void
     */
    protected static function loadService($name)
    {
        if (isset(self::$instances[$name])) {
            return;
        }

        if (isset(self::$serviceoverrides[$name])) {
            $class = self::$serviceoverrides[$name];
        } else {
            $class = 'SemanticScuttle_Service_' . $name;
        }

        if (!class_exists($class)) {
            //PEAR classname to filename rule
            $file = str_replace('_', '/', $class) . '.php';
            include_once $file;
        }

        self::$instances[$name] = call_user_func(
            array($class, 'getInstance'),
            self::$db
        );
    }



    /**
     * Loads self::$db if it is not loaded already.
     *
     * @return void
     */
    protected static function loadDb()
    {
        global $dbhost, $dbuser, $dbpass, $dbname, 
            $dbport, $dbpersist, $dbtype;

        if (self::$db !== null) {
            return;
        }
        include_once 'SemanticScuttle/db/'. $dbtype .'.php';
        $db = new sql_db();
        $db->sql_connect(
            $dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist
        );
        if (!$db->db_connect_id) {
            message_die(
                CRITICAL_ERROR,
                'Could not connect to the database',
                self::$db
            );
        }
        $db->sql_query('SET NAMES UTF8');
        self::$db = $db;
    }

}
?>