revamp service factory

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@393 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
cweiske 2009-10-23 16:54:51 +00:00
parent 3350658545
commit 91eb43bec6
4 changed files with 111 additions and 33 deletions

1
data/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
config.php

View file

@ -1,6 +1,12 @@
Upgrading SemanticScuttle from a previous version Upgrading SemanticScuttle from a previous version
================================================= =================================================
From versin 0.94 to 0.10.0
--------------------------
- ALTER TABLE `sc_bookmarks` ADD `bVoting` INT NOT NULL ;
- Add the new votes database table. See data/tables.sql.
From version 0.93 to 0.94 From version 0.93 to 0.94
------------------------- -------------------------

View file

@ -3,42 +3,112 @@
class SemanticScuttle_Service_Factory 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];
}
public function getServiceInstance($name, $servicedir = null)
/**
* 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)
{ {
global $dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist, $dbtype; if (isset(self::$instances[$name])) {
static $instances = array(); return;
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(self::$serviceoverrides[$name])) {
if (isset($serviceoverrules[$name])) { $class = self::$serviceoverrides[$name];
$name = $serviceoverrules[$name]; } else {
} $class = 'SemanticScuttle_Service_' . $name;
if (!class_exists($name)) { }
if (!isset($servicedir)) {
$servicedir = 'SemanticScuttle/Service/';
}
require_once $servicedir . $name . '.php'; if (!class_exists($class)) {
} //PEAR classname to filename rule
$instances[$name] = call_user_func( $file = str_replace('_', '/', $class) . '.php';
array('SemanticScuttle_Service_' . $name, 'getInstance'), 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
); );
} }
return $instances[$name]; $db->sql_query('SET NAMES UTF8');
} }
} }
?> ?>

View file

@ -173,6 +173,7 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_Service
public function rewriteVotings() public function rewriteVotings()
{ {
//FIXME //FIXME
//SELECT bid, SUM( vote ) FROM sc_votes GROUP BY bid
} }