Merge branch 'feature-config-datatypes'

This commit is contained in:
2010-09-16 23:18:45 +01:00
4 changed files with 143 additions and 35 deletions

View File

@@ -2,22 +2,85 @@
class RippingCluster_Config { class RippingCluster_Config {
/**
* Boolean value type
* @var bool
*/
const TYPE_BOOL = 'bool';
/**
* Integer value type
* @var int
*/
const TYPE_INT = 'int';
/**
* Float value type
* @var float
*/
const TYPE_FLOAT = 'float';
/**
* String value type
* @var string
*/
const TYPE_STRING = 'string';
/**
* String List value type; list of newline separated strings
* @var array(string)
*/
const TYPE_STRING_LIST = 'array(string)';
/**
* Contents of the dbconfig file
* @var string
*/
private $dbconfig; private $dbconfig;
/**
* Database object created for the lifetime of this script
* @var RippingCluster_Database
*/
private $database; private $database;
/**
* Associative array of connection parameters for the database configuration
* @var array(string=>string)
*/
private $databaseConfig = array(); private $databaseConfig = array();
/**
* Associative array of settings loaded from the database
* @var array(string=>array(string=>string))
*/
private $settings = array(); private $settings = array();
/**
* Constructs a new instance of the Config class
*
* @param string $dbconfig Database configuration file contents
* @return RippingCluster_Config
*/
public function __construct($dbconfig) { public function __construct($dbconfig) {
$this->dbconfig = $dbconfig; $this->dbconfig = $dbconfig;
$this->parseDatabaseConfig(); $this->parseDatabaseConfig();
} }
/**
* Parses the contents of the database configuration file so that individual settings can be retrieved.
*
*/
public function parseDatabaseConfig() { public function parseDatabaseConfig() {
$this->databaseConfig = parse_ini_file($this->dbconfig); $this->databaseConfig = parse_ini_file($this->dbconfig);
} }
/**
* Returns the value of the named item from the database configuration file
*
* @param string $key Name of the setting to retrieve
*/
public function getDatabase($key) { public function getDatabase($key) {
if (!isset($this->databaseConfig[$key])) { if (!isset($this->databaseConfig[$key])) {
throw new RippingCluster_Exception_DatabaseConfigMissing($key); throw new RippingCluster_Exception_DatabaseConfigMissing($key);
@@ -26,29 +89,55 @@ class RippingCluster_Config {
return $this->databaseConfig[$key]; return $this->databaseConfig[$key];
} }
/**
* Sets the database instance used by this object
*
* @param RippingCluster_Database $database Database instance
*/
public function setDatabase(RippingCluster_Database $database) { public function setDatabase(RippingCluster_Database $database) {
$this->database = $database; $this->database = $database;
$this->preload(); $this->preload();
} }
public function preload() { /**
* Loads the entire list of settings from the database
*
*/
private function preload() {
if (!$this->database) { if (!$this->database) {
throw new RippingCluster_Exception_NoDatabaseConnection(); throw new RippingCluster_Exception_NoDatabaseConnection();
} }
$this->settings = $this->database->selectAssoc('SELECT name,value FROM settings', 'name', 'value'); $this->settings = $this->database->selectAssoc('SELECT name,type,value FROM settings', 'name', array('name', 'value', 'type'));
} }
/**
* Identifies whether the named setting exists
*
* @param string $key Name of the setting
* @return bool
*/
public function exists($key) { public function exists($key) {
return isset($this->settings[$key]); return isset($this->settings[$key]);
} }
/**
* Fetches the value of the named setting
*
* @param string $key Name of the setting
*/
public function get($key) { public function get($key) {
if (!isset($this->settings[$key])) { if (!isset($this->settings[$key])) {
throw new RippingCluster_Exception_UnknownSetting($key); throw new RippingCluster_Exception_UnknownSetting($key);
} }
return $this->settings[$key]; switch ($this->settings[$key]['type']) {
case TYPE_STRING_LIST:
return explode("\n", $this->settings[$key]['value']);
default:
return $this->settings[$key]['value'];
}
} }
}; };

View File

@@ -32,12 +32,21 @@ class RippingCluster_Database {
$this->dbh = null; $this->dbh = null;
} }
public function selectAssoc($sql, $key_col, $value_col) { public function selectAssoc($sql, $key_col, $value_cols) {
$results = array(); $results = array();
foreach ($this->dbh->query($sql) as $row) { foreach ($this->dbh->query($sql) as $row) {
if (is_array($value_cols)) {
$values = array();
foreach ($value_cols as $value_col) {
$values[$value_col] = $row[$value_col];
}
$results[$row[$key_col]] = $values;
} else {
$results[$row[$key_col]] = $row[$value_col]; $results[$row[$key_col]] = $row[$value_col];
} }
}
return $results; return $results;
} }

View File

@@ -13,8 +13,9 @@ class RippingCluster_Source_Plugin_Bluray extends RippingCluster_PluginBase impl
*/ */
public static function enumerate() { public static function enumerate() {
$config = RippingCluster_Main::instance()->config(); $config = RippingCluster_Main::instance()->config();
$directory = $config->get('source.bluray.dir'); $directories = $config->get('source.bluray.dir');
foreach ($directories as $directory) {
if (!is_dir($directory)) { if (!is_dir($directory)) {
throw new RippingCluster_Exception_InvalidSourceDirectory($directory); throw new RippingCluster_Exception_InvalidSourceDirectory($directory);
} }
@@ -25,6 +26,7 @@ class RippingCluster_Source_Plugin_Bluray extends RippingCluster_PluginBase impl
foreach ($iterator as /** @var SplFileInfo */ $source_vts) { foreach ($iterator as /** @var SplFileInfo */ $source_vts) {
$sources[] = self::load($source_vts->getPathname(), false); $sources[] = self::load($source_vts->getPathname(), false);
} }
}
return $sources; return $sources;
} }
@@ -99,12 +101,15 @@ class RippingCluster_Source_Plugin_Bluray extends RippingCluster_PluginBase impl
} }
$real_source_filename = realpath($source_filename); $real_source_filename = realpath($source_filename);
$source_basedir = $config->get('source.bluray.dir'); // Check all of the source directories specified in the config
$source_directories = $config->get('source.bluray.dir');
foreach ($source_directories as $source_basedir) {
$real_source_basedir = realpath($source_basedir); $real_source_basedir = realpath($source_basedir);
if (substr($real_source_filename, 0, strlen($real_source_basedir)) != $real_source_basedir) { if (substr($real_source_filename, 0, strlen($real_source_basedir)) != $real_source_basedir) {
return false; return false;
} }
}
return true; return true;
} }

View File

@@ -23,8 +23,9 @@ class RippingCluster_Source_Plugin_HandBrake extends RippingCluster_PluginBase i
*/ */
public static function enumerate() { public static function enumerate() {
$config = RippingCluster_Main::instance()->config(); $config = RippingCluster_Main::instance()->config();
$directory = $config->get('source.handbrake.dir'); $directories = $config->get('source.handbrake.dir');
foreach ($directories as $directory) {
if (!is_dir($directory)) { if (!is_dir($directory)) {
throw new RippingCluster_Exception_InvalidSourceDirectory($directory); throw new RippingCluster_Exception_InvalidSourceDirectory($directory);
} }
@@ -35,6 +36,7 @@ class RippingCluster_Source_Plugin_HandBrake extends RippingCluster_PluginBase i
foreach ($iterator as /** @var SplFileInfo */ $source_vts) { foreach ($iterator as /** @var SplFileInfo */ $source_vts) {
$sources[] = self::load($source_vts->getPathname(), false); $sources[] = self::load($source_vts->getPathname(), false);
} }
}
return $sources; return $sources;
} }
@@ -203,12 +205,15 @@ class RippingCluster_Source_Plugin_HandBrake extends RippingCluster_PluginBase i
} }
$real_source_filename = realpath($source_filename); $real_source_filename = realpath($source_filename);
$source_basedir = $config->get('source.handbrake.dir'); // Check all of the source directories specified in the config
$source_directories = $config->get('source.handbrake.dir');
foreach ($source_directories as $source_basedir) {
$real_source_basedir = realpath($source_basedir); $real_source_basedir = realpath($source_basedir);
if (substr($real_source_filename, 0, strlen($real_source_basedir)) != $real_source_basedir) { if (substr($real_source_filename, 0, strlen($real_source_basedir)) != $real_source_basedir) {
return false; return false;
} }
}
return true; return true;
} }