modelGenerator: add the -u, -p and -n options

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3701 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-05-19 07:09:48 +00:00
parent 9c33e4b868
commit 42ef3046cf

View File

@@ -18,17 +18,52 @@ class modelGenerator
*/
private $modelsDir;
/** The namespace name provided by the user
*/
private $namespaceName;
/** The dblayer object
*/
private $db;
/** The constructor
*/
public function __construct ($dsn, $modelsDir)
public function __construct ()
{
$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");
$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->db = new \dblayeroo ($dsn);
$this->namespaceName = $getopts->get ("Namespace");
$this->db = new \dblayeroo ($dsn,
$getopts->get ("Username"),
$getopts->get ("Password"));
}
/** List all the existing tables in the database
@@ -53,7 +88,10 @@ class modelGenerator
$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";
@@ -108,28 +146,7 @@ class modelGenerator
}
}
$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);
$modelGenerator = new modelGenerator ();
$tables = $modelGenerator->listTables ();
foreach ($tables as $tableName)
$modelGenerator->createModel ($tableName);