diff options
author | Silvio <silvio@devlet.com.br> | 2010-04-06 16:16:50 -0300 |
---|---|---|
committer | Silvio <silvio@devlet.com.br> | 2010-04-06 16:16:50 -0300 |
commit | a80a88d99cb102fad4a23d5ea5732d489e70eddf (patch) | |
tree | 8d5a4bb15b3d6d38ed8bc132dc507c56d23a1711 /classes | |
parent | f6f1d270419bbd9696974719b15b8c01b19b032b (diff) | |
download | cinisis-a80a88d99cb102fad4a23d5ea5732d489e70eddf.tar.gz cinisis-a80a88d99cb102fad4a23d5ea5732d489e70eddf.tar.bz2 |
Enhanced config check
Diffstat (limited to 'classes')
-rw-r--r-- | classes/CinisisDb.php | 23 | ||||
-rw-r--r-- | classes/MaleteDb.php | 9 | ||||
-rw-r--r-- | classes/PhpIsisDb.php | 9 | ||||
-rw-r--r-- | classes/SchemaDb.php | 29 |
4 files changed, 56 insertions, 14 deletions
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 @@ -88,6 +88,15 @@ class MaleteDb implements IsisDb { } /** + * Check configuration. + * + * @see IsisDb::check() + */ + public function check($schema, $section = NULL) { + return SchemaDb::check($schema, $section); + } + + /** * Tag results of a db query. * * This function converts the keys of query result from field numbers diff --git a/classes/PhpIsisDb.php b/classes/PhpIsisDb.php index 82fe6db..65a7f38 100644 --- a/classes/PhpIsisDb.php +++ b/classes/PhpIsisDb.php @@ -79,6 +79,15 @@ class PhpIsisDb implements IsisDb { } /** + * Check configuration. + * + * @see IsisDb::check() + */ + public function check($schema, $section = NULL) { + return SchemaDb::check($schema, $section); + } + + /** * Tag results of a db query. * * This function converts the keys of query result from field 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 @@ <?php +/** + * SchemaDb class with standard database procedures and + * configuration. + */ class SchemaDb { /** * Return the required database config. @@ -36,7 +40,7 @@ class SchemaDb { } /** - * Return an example schema. + * Return an example database schema. * * @see IsisDb::example() */ @@ -65,11 +69,28 @@ class SchemaDb { } /** - * Check required fields. + * Recursively check for required fields in a database schema. * - * @todo + * @see IsisDb::check() */ - function check($schema = NULL) { + function check($schema, $section = NULL) { + if ($section === NULL) { + $section = SchemaDb::required(); + } + + foreach ($section as $key => $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; } } |