Update "tools" directory to be compliant with the php-cs-fixer

This commit is contained in:
2023-04-13 21:53:13 +02:00
parent ba83207264
commit e719b3e11c
5 changed files with 791 additions and 715 deletions

View File

@@ -1,187 +1,227 @@
#!/usr/bin/php
<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
require_once (__DIR__."/../src/Getopts.php");
require_once (__DIR__."/../src/Dblayeroo.php");
/**
* DomFramework
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
*/
/** 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
require_once(__DIR__ . "/../src/Getopts.php");
require_once(__DIR__ . "/../src/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 DSN provided by the user
*/
private $dsn;
/** The modelsDir provided by the user
*/
private $modelsDir;
/**
* The modelsDir provided by the user
*/
private $modelsDir;
/** The namespace name provided by the user
*/
private $namespaceName;
/**
* The namespace name provided by the user
*/
private $namespaceName;
/** The dblayer object
*/
private $db;
/**
* The dblayer object
*/
private $db;
/** The constructor
*/
public function __construct ()
{
$getopts = new Domframework\Getopts ();
$getopts->add ("Help", "?h", "help", "Help of the software");
$getopts->add ("DSN", "", "dsn:", "DSN - Data Source Name\n".
" Exemple : --dsn \"mysql:dbname=DBNAME\"",
"dsn");
$getopts->add ("Output", "o:", "output:",
"Output directory, ./models by default", "dir");
$getopts->add ("Namespace", "n:", "namespace:",
"The namespace (if '', not added), models by default",
"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)
/**
* The constructor
*/
public function __construct()
{
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);
$getopts = new Domframework\Getopts();
$getopts->add("Help", "?h", "help", "Help of the software");
$getopts->add(
"DSN",
"",
"dsn:",
"DSN - Data Source Name\n" .
" Exemple : --dsn \"mysql:dbname=DBNAME\"",
"dsn"
);
$getopts->add(
"Output",
"o:",
"output:",
"Output directory, ./models by default",
"dir"
);
$getopts->add(
"Namespace",
"n:",
"namespace:",
"The namespace (if '', not added), models by default",
"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");
try {
$this->db = new Domframework\Dblayeroo(
$dsn,
$getopts->get("Username"),
$getopts->get("Password")
);
} catch (\Exception $e) {
file_put_contents("php://stderr", $e->getMessage() . "\n");
exit(3);
}
}
$this->dsn = $dsn;
$this->modelsDir = $modelsDir;
$this->namespaceName = $getopts->get ("Namespace");
try
/**
* List all the existing tables in the database
*/
public function listTables()
{
$this->db = new Domframework\Dblayeroo ($dsn,
$getopts->get ("Username"),
$getopts->get ("Password"));
}
catch (\Exception $e)
{
file_put_contents ("php://stderr", $e->getMessage ()."\n");
exit (3);
}
}
/** 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 \dblayeroo\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";
if (count ($schema["unique"]))
{
$f .= "\n";
$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";
}
$f .= "\n";
$f .= " // The titles\n";
$f .= " \$this->titles (array (\n";
foreach ($schema["fields"] as $field=>$params)
{
$f .= " \"$field\" => _(\"$field\"),\n";
}
$f .= " ));\n";
if (count ($schema["foreign"]))
{
$f .= "\n";
$f .= " // The foreign constraints\n";
$f .= " \$this->foreign (array (\n";
foreach ($schema["foreign"] as $field=>$params)
$f .= " \"$field\" => array (\n \"".
implode ("\", \"", $params).
"\"),\n";
$f .= " ));\n";
$f .= "\n";
$f .= " // And the associated objects\n";
if ($this->namespaceName === false)
$path = "\\models\\";
elseif ($this->namespaceName !== "")
$path = "\\$this->namespaceName\\";
else
$path = "";
foreach ($schema["foreign"] as $field=>$params)
{
$objName = $params[0];
$f .= " \$this->foreign_$objName = new $path$objName ();\n";
$f .= " \$this->setForeignObj (\$this->foreign_$objName);\n";
}
return $this->db->listTables();
}
$f .= " }\n";
$f .= "}\n";
file_put_contents ($this->modelsDir."/$tableName".".php", $f);
}
/**
* 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 \dblayeroo\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";
if (count($schema["unique"])) {
$f .= "\n";
$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";
}
$f .= "\n";
$f .= " // The titles\n";
$f .= " \$this->titles (array (\n";
foreach ($schema["fields"] as $field => $params) {
$f .= " \"$field\" => _(\"$field\"),\n";
}
$f .= " ));\n";
if (count($schema["foreign"])) {
$f .= "\n";
$f .= " // The foreign constraints\n";
$f .= " \$this->foreign (array (\n";
foreach ($schema["foreign"] as $field => $params) {
$f .= " \"$field\" => array (\n \"" .
implode("\", \"", $params) .
"\"),\n";
}
$f .= " ));\n";
$f .= "\n";
$f .= " // And the associated objects\n";
if ($this->namespaceName === false) {
$path = "\\models\\";
} elseif ($this->namespaceName !== "") {
$path = "\\$this->namespaceName\\";
} else {
$path = "";
}
foreach ($schema["foreign"] as $field => $params) {
$objName = $params[0];
$f .= " \$this->foreign_$objName = new $path$objName ();\n";
$f .= " \$this->setForeignObj (\$this->foreign_$objName);\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);
$modelGenerator = new ModelGenerator();
$tables = $modelGenerator->listTables();
foreach ($tables as $tableName) {
$modelGenerator->createModel($tableName);
}