diff options
author | cweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f> | 2009-10-23 16:54:51 +0000 |
---|---|---|
committer | cweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f> | 2009-10-23 16:54:51 +0000 |
commit | 91eb43bec6dfdd2e16012f0fb56b6eaa87105e3f (patch) | |
tree | 2c2f42535a0b1d6336b1da64223decd6b39abf47 /src/SemanticScuttle/Service/Factory.php | |
parent | 3350658545a4645eb7eeea6323636b79310a18cc (diff) | |
download | semanticscuttle-91eb43bec6dfdd2e16012f0fb56b6eaa87105e3f.tar.gz semanticscuttle-91eb43bec6dfdd2e16012f0fb56b6eaa87105e3f.tar.bz2 |
revamp service factory
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@393 b3834d28-1941-0410-a4f8-b48e95affb8f
Diffstat (limited to 'src/SemanticScuttle/Service/Factory.php')
-rw-r--r-- | src/SemanticScuttle/Service/Factory.php | 134 |
1 files changed, 102 insertions, 32 deletions
diff --git a/src/SemanticScuttle/Service/Factory.php b/src/SemanticScuttle/Service/Factory.php index b4ba28e..b6f79d5 100644 --- a/src/SemanticScuttle/Service/Factory.php +++ b/src/SemanticScuttle/Service/Factory.php @@ -3,42 +3,112 @@ class SemanticScuttle_Service_Factory { - public function __construct($db, $serviceoverrules = array()) + /** + * 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 + ); + } - public function getServiceInstance($name, $servicedir = null) + + + /** + * Loads self::$db if it is not loaded already. + * + * @return void + */ + protected static function loadDb() { - global $dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist, $dbtype; - static $instances = array(); - static $db; - if (!isset($db)) { - 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"); - } - - if (!isset($instances[$name])) { - if (isset($serviceoverrules[$name])) { - $name = $serviceoverrules[$name]; - } - if (!class_exists($name)) { - if (!isset($servicedir)) { - $servicedir = 'SemanticScuttle/Service/'; - } - - require_once $servicedir . $name . '.php'; - } - $instances[$name] = call_user_func( - array('SemanticScuttle_Service_' . $name, 'getInstance'), + 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 ); - } - return $instances[$name]; - } + } + $db->sql_query('SET NAMES UTF8'); + } + } ?> |