#!/usr/bin/php 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"); $getopts->add ("Namespace", "n:", "namespace:", "The namespace (if '', not added)", "namespaceName"); $getopts->add ("Username", "u:", "username:", "Username to connect", "username"); $getopts->add ("Password", "p:", "password:", "Password to connect", "password"); if ($getopts->get ("Help")) die ($getopts->help ()); if ($getopts->get ("DSN") === false) { file_put_contents ("php://stderr", "ERROR: The DSN is not provided\n"); exit (1); } $dsn = $getopts->get ("DSN"); $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); } $this->dsn = $dsn; $this->modelsDir = $modelsDir; $this->namespaceName = $getopts->get ("Namespace"); $this->db = new \dblayeroo ($dsn, $getopts->get ("Username"), $getopts->get ("Password")); } /** List all the existing tables in the database */ public function listTables () { return $this->db->listTables (); } /** Create the model file from the $tableName * @param string $tableName The table to examine */ 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"; if ($this->namespaceName === false) $f .= "namespace models;\n\n"; elseif ($this->namespaceName !== "") $f .= "namespace $this->namespaceName;\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); } } $modelGenerator = new modelGenerator (); $tables = $modelGenerator->listTables (); foreach ($tables as $tableName) $modelGenerator->createModel ($tableName);