aboutsummaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/Factory.php
blob: b6f79d5298d75d4f246a8204b46433b522facf9c (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
<?php
/* Connect to the database and build services */

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 getServiceInstance($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';
            require_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;
        }
        require_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',
                $db
            );
        }
        $db->sql_query('SET NAMES UTF8');
    }

}
?>