From a80a88d99cb102fad4a23d5ea5732d489e70eddf Mon Sep 17 00:00:00 2001 From: Silvio Date: Tue, 6 Apr 2010 16:16:50 -0300 Subject: Enhanced config check --- Doxyfile | 2 +- classes/CinisisDb.php | 23 +++++++++++++---------- classes/MaleteDb.php | 9 +++++++++ classes/PhpIsisDb.php | 9 +++++++++ classes/SchemaDb.php | 29 +++++++++++++++++++++++++---- interface.php | 16 +++++++++++++++- 6 files changed, 72 insertions(+), 16 deletions(-) diff --git a/Doxyfile b/Doxyfile index c79baf1..6b2cfca 100644 --- a/Doxyfile +++ b/Doxyfile @@ -25,7 +25,7 @@ DOXYFILE_ENCODING = UTF-8 # The PROJECT_NAME tag is a single word (or a sequence of words surrounded # by quotes) that should identify the project. -PROJECT_NAME = "CinIsis Database Reader" +PROJECT_NAME = "Cinisis Database Reader" # The PROJECT_NUMBER tag can be used to enter a project or revision number. # This could be handy for archiving the generated documentation or diff --git a/classes/CinisisDb.php b/classes/CinisisDb.php index ae80a00..3dead27 100644 --- a/classes/CinisisDb.php +++ b/classes/CinisisDb.php @@ -28,16 +28,18 @@ class CinisisDb { // Check main configuration. $config = $this->parse($config); + // Set database implementation. + $this->implementation = $config['implementation'] .'Db'; + // Check database schema. - $schema = $this->parse('schemas/'. $config['database'] .'.yaml', 'schema'); + $schema = $this->parse('schemas/'. $config['database'] .'.yaml', $this->implementation); } catch (Exception $e) { - echo '[cinisis] caught exception: ', $e->getMessage(), "\n"; + echo __CLASS__ .' caught exception: ', $e->getMessage(), "\n"; return FALSE; } // Setup database connection. - $this->implementation = $config['implementation'] .'Db'; - $this->db = new $this->implementation($schema); + $this->db = new $this->implementation($schema); } /** @@ -47,11 +49,12 @@ class CinisisDb { * Config file. * * @return - * Array with configuration. + * Array with configuration or FALSE if error. */ - function load($file) { + public function load($file) { if (!file_exists($file)) { throw new Exception('Config '. $file .' not found.'); + return FALSE; } // Load configuration. @@ -64,20 +67,19 @@ class CinisisDb { * @param $config * Config file or array with configuration. * - * @param $type - * Configuration type (either 'cinisis' or 'schema'). + * @param $class + * Configuration class name. * * @return * Array with configuration or FALSE on error. */ - function parse($config, $type = 'cinisis') { + public function parse($config, $class = __CLASS__) { // 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); } @@ -99,6 +101,7 @@ class CinisisDb { // Check database configuration. if (!isset($config['database'])) { throw new Exception('No database set on configuration.'); + return FALSE; } return $config; diff --git a/classes/MaleteDb.php b/classes/MaleteDb.php index b9d1350..24c6d35 100644 --- a/classes/MaleteDb.php +++ b/classes/MaleteDb.php @@ -87,6 +87,15 @@ class MaleteDb implements IsisDb { return SchemaDb::example(); } + /** + * Check configuration. + * + * @see IsisDb::check() + */ + public function check($schema, $section = NULL) { + return SchemaDb::check($schema, $section); + } + /** * Tag results of a db query. * diff --git a/classes/PhpIsisDb.php b/classes/PhpIsisDb.php index 82fe6db..65a7f38 100644 --- a/classes/PhpIsisDb.php +++ b/classes/PhpIsisDb.php @@ -78,6 +78,15 @@ class PhpIsisDb implements IsisDb { return SchemaDb::example(); } + /** + * Check configuration. + * + * @see IsisDb::check() + */ + public function check($schema, $section = NULL) { + return SchemaDb::check($schema, $section); + } + /** * Tag results of a db query. * diff --git a/classes/SchemaDb.php b/classes/SchemaDb.php index 2756026..c00ae66 100644 --- a/classes/SchemaDb.php +++ b/classes/SchemaDb.php @@ -1,5 +1,9 @@ $value) { + if (!isset($schema[$key])) { + throw new Exception('Undefined required parameter '. $key .' on database configuration.'); + return FALSE; + } + + if (is_array($value)) { + if (SchemaDb::check($schema[$key], $section[$key]) == FALSE) { + return FALSE; + } + } + } + return $schema; } } diff --git a/interface.php b/interface.php index 7a40cc2..1e63f38 100644 --- a/interface.php +++ b/interface.php @@ -37,7 +37,7 @@ interface IsisDb { public function rows(); /** - * Return a default example schema. + * Return an example database schema. * * The example schema should have all information the implementation * needs to be able to open and read a database. @@ -46,4 +46,18 @@ interface IsisDb { * Array with a sample database schema. */ public function example(); + + /** + * Configuration check. + * + * @param $schema + * Database schema to check. + * + * @param $section + * Configuration section. + * + * @return + * Database schema or FALSE if error. + */ + function check($schema, $section = NULL); } -- cgit v1.2.3