aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio <silvio@devlet.com.br>2010-04-06 14:05:07 -0300
committerSilvio <silvio@devlet.com.br>2010-04-06 14:05:07 -0300
commitf6f1d270419bbd9696974719b15b8c01b19b032b (patch)
treeb27a0cd0cc3e79a9d7d1b8fa75e183411db79569
parentf3fea8713e9c82b3afe2c54a8fa997396f8bb87b (diff)
downloadcinisis-f6f1d270419bbd9696974719b15b8c01b19b032b.tar.gz
cinisis-f6f1d270419bbd9696974719b15b8c01b19b032b.tar.bz2
Config parse and check
-rw-r--r--classes/CinIsis.php62
-rw-r--r--classes/CinisisDb.php106
-rw-r--r--classes/MaleteDb.php8
-rw-r--r--classes/PhpIsisDb.php8
-rw-r--r--classes/SchemaDb.php52
-rw-r--r--index.php2
-rw-r--r--interface.php2
7 files changed, 164 insertions, 76 deletions
diff --git a/classes/CinIsis.php b/classes/CinIsis.php
deleted file mode 100644
index ff1de68..0000000
--- a/classes/CinIsis.php
+++ /dev/null
@@ -1,62 +0,0 @@
-<?php
-
-/**
- * CinIsis main class.
- */
-class CinIsis {
- /**
- * @var $db
- * Database resource.
- */
- var $db;
-
- /**
- * @var $implementation
- * Database implementation.
- */
- var $implementation;
-
- /**
- * Constructor.
- *
- * @param $file
- * Optional parameter to set alternative config file.
- *
- * @todo
- * Config check.
- */
- function __construct($file = 'config/config.yaml') {
- try {
- // Load main configuration.
- $config = $this->config($file);
-
- // Load database schema.
- $schema = $this->config('schemas/'. $config['database'] .'.yaml');
- } 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 config($file) {
- if (!file_exists($file)) {
- throw new Exception('Config '. $file .' not found.');
- }
-
- // Load configuration.
- return Spyc::YAMLLoad($file);
- }
-}
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 @@
+<?php
+
+/**
+ * CinisisDb main class.
+ */
+class CinisisDb {
+ /**
+ * @var $db
+ * Database resource.
+ */
+ var $db;
+
+ /**
+ * @var $implementation
+ * Database implementation.
+ */
+ var $implementation;
+
+ /**
+ * Constructor.
+ *
+ * @param $config
+ * Optional parameter to set alternative config file or array
+ * with configuration.
+ */
+ function __construct($config = 'config/config.yaml') {
+ try {
+ // Check main configuration.
+ $config = $this->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;
+ }
+}
diff --git a/classes/MaleteDb.php b/classes/MaleteDb.php
index a2d45aa..b9d1350 100644
--- a/classes/MaleteDb.php
+++ b/classes/MaleteDb.php
@@ -79,12 +79,12 @@ class MaleteDb implements IsisDb {
}
/**
- * Return a default example schema.
+ * Return an example schema.
*
- * @see IsisDb::default_schema()
+ * @see IsisDb::example()
*/
- public function default_schema() {
- return SchemaDb::default_schema();
+ public function example() {
+ return SchemaDb::example();
}
/**
diff --git a/classes/PhpIsisDb.php b/classes/PhpIsisDb.php
index e436666..82fe6db 100644
--- a/classes/PhpIsisDb.php
+++ b/classes/PhpIsisDb.php
@@ -70,12 +70,12 @@ class PhpIsisDb implements IsisDb {
}
/**
- * Return a default example schema.
+ * Return an example schema.
*
- * @see IsisDb::default_schema()
+ * @see IsisDb::example()
*/
- public function default_schema() {
- return SchemaDb::default_schema();
+ public function example() {
+ return SchemaDb::example();
}
/**
diff --git a/classes/SchemaDb.php b/classes/SchemaDb.php
index 4cf77e6..2756026 100644
--- a/classes/SchemaDb.php
+++ b/classes/SchemaDb.php
@@ -2,14 +2,49 @@
class SchemaDb {
/**
- * Return a default example schema.
+ * Return the required database config.
*
- * @see IsisDb::default_schema()
- */
- public function default_schema() {
+ * @return
+ * Array with required config.
+ */
+ public function required() {
$schema = array(
'db' => array(
'name' => 'dbname',
+ ),
+ 'fields' => array(
+ ),
+ );
+
+ return $schema;
+ }
+
+ /**
+ * Return the optional database config.
+ *
+ * @return
+ * Array with optional config.
+ */
+ public function optional() {
+ $schema = array(
+ 'db' => array(
+ 'charset' => 'charset',
+ ),
+ );
+
+ return $schema;
+ }
+
+ /**
+ * Return an example schema.
+ *
+ * @see IsisDb::example()
+ */
+ public function example() {
+ $required = SchemaDb::required();
+ $optional = SchemaDb::optional();
+ $schema = array(
+ 'db' => array(
'charset' => 'charset',
),
'fields' => array(
@@ -26,6 +61,15 @@ class SchemaDb {
),
);
+ return array_merge_recursive($required, $optional, $schema);
+ }
+
+ /**
+ * Check required fields.
+ *
+ * @todo
+ */
+ function check($schema = NULL) {
return $schema;
}
}
diff --git a/index.php b/index.php
index ff40fd8..2da6b69 100644
--- a/index.php
+++ b/index.php
@@ -21,7 +21,7 @@ function cinisis_autoload($class) {
spl_autoload_register("cinisis_autoload");
// Get a db instance.
-$isis = new CinIsis();
+$isis = new CinisisDb();
?>
diff --git a/interface.php b/interface.php
index c2e1e9b..7a40cc2 100644
--- a/interface.php
+++ b/interface.php
@@ -45,5 +45,5 @@ interface IsisDb {
* @return
* Array with a sample database schema.
*/
- public function default_schema();
+ public function example();
}