aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Doxyfile2
-rw-r--r--classes/CinisisDb.php23
-rw-r--r--classes/MaleteDb.php9
-rw-r--r--classes/PhpIsisDb.php9
-rw-r--r--classes/SchemaDb.php29
-rw-r--r--interface.php16
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
@@ -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;
}
}
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);
}