Add modelGenerator tool to create the model files based on an existing database
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3698 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
135
tools/modelGenerator.php
Executable file
135
tools/modelGenerator.php
Executable file
@@ -0,0 +1,135 @@
|
|||||||
|
#!/usr/bin/php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once ("domframework/getopts.php");
|
||||||
|
require_once ("domframework/dblayeroo.php");
|
||||||
|
|
||||||
|
/** Generate the model files from an existing database
|
||||||
|
* Provide the DSN to connect to the database, and the program will generate
|
||||||
|
* the models/table.php files
|
||||||
|
*/
|
||||||
|
class modelGenerator
|
||||||
|
{
|
||||||
|
/** The DSN provided by the user
|
||||||
|
*/
|
||||||
|
private $dsn;
|
||||||
|
|
||||||
|
/** The modelsDir provided by the user
|
||||||
|
*/
|
||||||
|
private $modelsDir;
|
||||||
|
|
||||||
|
/** The dblayer object
|
||||||
|
*/
|
||||||
|
private $db;
|
||||||
|
|
||||||
|
/** The constructor
|
||||||
|
*/
|
||||||
|
public function __construct ($dsn, $modelsDir)
|
||||||
|
{
|
||||||
|
$this->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);
|
||||||
Reference in New Issue
Block a user