From 42ef3046cfdea1456569f3961d05cdd116d4d11d Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 19 May 2017 07:09:48 +0000 Subject: [PATCH] modelGenerator: add the -u, -p and -n options git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3701 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- tools/modelGenerator.php | 67 +++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/tools/modelGenerator.php b/tools/modelGenerator.php index 7ee2dbd..ca0d4f5 100755 --- a/tools/modelGenerator.php +++ b/tools/modelGenerator.php @@ -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"; - $f .= "namespace models;\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);