diff --git a/tools/modelGenerator.php b/tools/modelGenerator.php new file mode 100755 index 0000000..7ee2dbd --- /dev/null +++ b/tools/modelGenerator.php @@ -0,0 +1,135 @@ +#!/usr/bin/php +dsn = $dsn; + $this->modelsDir = $modelsDir; + $this->db = new \dblayeroo ($dsn); + } + + /** List all the existing tables in the database + */ + public function listTables () + { + return $this->db->listTables (); + } + + /** Create the model file from the $tableName + */ + public function createModel ($tableName) + { + if (file_exists ($this->modelsDir."/$tableName".".php")) + { + file_put_contents ("php://stderr", + "WARNING: File ".$this->modelsDir."/$tableName".".php already exists. ". + "SKIP\n"); + return; + } + echo "Create File ".$this->modelsDir."/$tableName".".php\n"; + $schema = $this->db->getTableSchema ($tableName); + $f = ""; + $f .= "<"."?"."php\n\n"; + $f .= "namespace models;\n\n"; + $f .= "require_once (\"domframework/dblayeroo.php\");\n\n"; + $f .= "/** The model for $tableName table in the database\n */\n"; + $f .= "class $tableName extends \dbalyeroo\n"; + $f .= "{\n"; + $f .= " /** The constructor connect to database\n */\n"; + $f .= " public function __construct ()\n {\n"; + $f .= " // The table name\n"; + $f .= " \$this->table (\"$tableName\");\n\n"; + $f .= " // The fields name and parameters\n"; + $f .= " \$this->fields (array (\n"; + foreach ($schema["fields"] as $field=>$params) + { + $f .= " \"$field\" => array (\"".implode ("\", \"", $params). + "\"),\n"; + } + $f .= " ));\n\n"; + $f .= " // The primary key\n"; + $f .= " \$this->primary (\"".$schema["primary"]."\");\n\n"; + if (count ($schema["unique"])) + { + $f .= " // The unique constraints\n"; + $f .= " \$this->unique (array (\n"; + foreach ($schema["unique"] as $unique) + { + if (is_array ($unique)) + $f .= " array (\"".implode ("\", \"", $unique)."\"),\n"; + else + $f .= " array (\"$unique\"),\n"; + } + $f .= " ));\n\n"; + } + $f .= " // The titles\n"; + $f .= " \$this->titles = array (\n"; + foreach ($schema["fields"] as $field=>$params) + { + $f .= " \"$field\" => _(\"$field\"),\n"; + } + $f .= " );\n\n"; + if (count ($schema["foreign"])) + { + $f .= " // The foreign constraints\n"; + $f .= " \$this->foreign = array (\n"; + foreach ($schema["foreign"] as $field=>$params) + $f .= " \"$field\" => array (\"". implode ("\", \"", $params). + "\"),\n"; + $f .= " );\n"; + } + + $f .= " }\n"; + $f .= "}\n"; + file_put_contents ($this->modelsDir."/$tableName".".php", $f); + } +} + +$getopts = new \getopts (); +$getopts->add ("Help", "?h", "help", "Help of the software"); +$getopts->add ("DSN", "", "dsn:", "DSN - Data Source Name", "dsn"); +$getopts->add ("Output", "o:", "output:", "Output directory", "dir"); +if ($getopts->get ("Help")) + echo $getopts->help (); +if ($getopts->get ("DSN") === false) +{ + file_put_contents ("php://stderr", "ERROR: The DSN is not provided\n"); + exit (1); +} +$modelsDir = $getopts->get ("Output"); +if ($modelsDir === false) + $modelsDir = "./models"; +if (! file_exists ($modelsDir) || ! is_dir ($modelsDir)) +{ + file_put_contents ("php://stderr", + "ERROR: The modelsDir '$modelsDir' doesn't exists or is not a directory\n"); + exit (1); +} + +$modelGenerator = new modelGenerator ($getopts->get ("DSN"), $modelsDir); +$tables = $modelGenerator->listTables (); +foreach ($tables as $tableName) + $modelGenerator->createModel ($tableName);