From f6f1d270419bbd9696974719b15b8c01b19b032b Mon Sep 17 00:00:00 2001 From: Silvio Date: Tue, 6 Apr 2010 14:05:07 -0300 Subject: Config parse and check --- classes/CinisisDb.php | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 classes/CinisisDb.php (limited to 'classes/CinisisDb.php') diff --git a/classes/CinisisDb.php b/classes/CinisisDb.php new file mode 100644 index 0000000..ae80a00 --- /dev/null +++ b/classes/CinisisDb.php @@ -0,0 +1,106 @@ +parse($config); + + // Check database schema. + $schema = $this->parse('schemas/'. $config['database'] .'.yaml', 'schema'); + } catch (Exception $e) { + echo '[cinisis] caught exception: ', $e->getMessage(), "\n"; + return FALSE; + } + + // Setup database connection. + $this->implementation = $config['implementation'] .'Db'; + $this->db = new $this->implementation($schema); + } + + /** + * Config file load. + * + * @param $file + * Config file. + * + * @return + * Array with configuration. + */ + function load($file) { + if (!file_exists($file)) { + throw new Exception('Config '. $file .' not found.'); + } + + // Load configuration. + return Spyc::YAMLLoad($file); + } + + /** + * Parse configuration. + * + * @param $config + * Config file or array with configuration. + * + * @param $type + * Configuration type (either 'cinisis' or 'schema'). + * + * @return + * Array with configuration or FALSE on error. + */ + function parse($config, $type = 'cinisis') { + // Load configuration if needed. + if (!is_array($config)) { + $config = $this->load($config); + } + + // Check configuration. + $class = ucfirst($type) .'Db'; + return call_user_func(array($class, 'check'), $config); + } + + /** + * Check configuration. + * + * @param $config + * Config file or array with configuration. + * + * @return + * Array with configuration or FALSE on error. + */ + public function check($config) { + // Set default database backend if needed. + if (!isset($config['implementation'])) { + $config['implementation'] = 'PhpIsis'; + } + + // Check database configuration. + if (!isset($config['database'])) { + throw new Exception('No database set on configuration.'); + } + + return $config; + } +} -- cgit v1.2.3