Last active
August 29, 2015 14:24
-
-
Save mithereal/79915ca296559eaa19df to your computer and use it in GitHub Desktop.
This will convert your mysqli schema to chicago boss base app, create models, controllers, and views based on the schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -q | |
<?php | |
### This will convert your mysql schema to chicago boss base app, create models, controllers, and views based on the schema | |
### place under the app root ex /appname/mysqlitoboss.php | |
### run from cli ex./mysqlitoboss.php or php -f mysqlitoboss.php | |
/* Define STDIN in case if it is not already defined by PHP for some reason */ | |
if(!defined("STDIN")) { | |
define("STDIN", fopen('php://stdin','r')); | |
} | |
echo "What is your appname (enter below):\n"; | |
$appname = fread(STDIN, 80); // Read up to 80 characters or a newline | |
echo "What is your host (enter below):\n"; | |
$hostname = fread(STDIN, 80); // Read up to 80 characters or a newline | |
echo " What is your database (enter below):\n"; | |
$database = fread(STDIN, 80); // Read up to 80 characters or a newline | |
echo "What is your username (enter below):\n"; | |
$username = fread(STDIN, 80); // Read up to 80 characters or a newline | |
echo "What is your password (enter below):\n"; | |
$password = fread(STDIN, 80); // Read up to 80 characters or a newline | |
echo "What is the tablename (enter below):\n"; | |
$tablename = fread(STDIN, 80); // Read up to 80 characters or a newline | |
$con = mysqli_connect($hostname,$username,$password,$tablename); | |
// Check connection | |
if (mysqli_connect_errno()) | |
{ | |
echo "Failed to connect to MySQL: " . mysqli_connect_error(); | |
} | |
$s_sql="SHOW TABLES from " . $tablename; | |
$result=mysqli_query($con,$s_sql); | |
// Fetch all | |
$schema=mysqli_fetch_all($result,MYSQLI_ASSOC); | |
// Free result set | |
mysqli_free_result($result); | |
foreach($schema as $table){ | |
$modelname = $table['Tables_in_'.$tablename]; | |
$t_sql="SHOW COLUMNS FROM `".$modelname."`"; | |
$fields = null; | |
$prop_fields = null; | |
$result=mysqli_query($con,$t_sql); | |
$columns=mysqli_fetch_all($result,MYSQLI_ASSOC); | |
foreach($columns as $c){ | |
$prop_fields[] =ucfirst ($c['Field']); | |
$fields[]=$c['Field']; | |
} | |
$proplist = implode($prop_fields,','); | |
$attribs = implode($fields,','); | |
switch ($modelname) { | |
case "user": | |
$modelname = 'app_user'; | |
break; | |
case "group": | |
$modelname = 'app_group'; | |
break; | |
case "file": | |
$modelname = 'app_file'; | |
break; | |
default: | |
} | |
$file = fopen("./src/model/".$modelname.".erl", "w") or die("Unable to open file!"); | |
$txt = "-module(".$modelname.", [".$proplist."]).\n"; | |
$txt .= "-compile(export_all).\n"; | |
$txt .= "-table(['".$modelname."']).\n"; | |
fwrite($file, $txt); | |
fclose($file); | |
if(!file_exists("./src/controller/".$appname."_".$modelname."_controller.erl")){ | |
$file = fopen("./src/controller/".$appname."_".$modelname."_controller.erl", "w"); | |
$txt = "-module(".$appname."_".$modelname."_controller, [Req]).\n"; | |
$txt .= "-compile(export_all).\n"; | |
$txt .= "\n"; | |
$txt .= "list('GET', []) ->\n"; | |
$txt .=" " . ucfirst($modelname) . "s = boss_db:find(".$modelname.", []),\n"; | |
$txt .=' {ok, [{'. $modelname .', '. ucfirst($modelname).'s}]}.'; | |
fwrite($file, $txt); | |
fclose($file); | |
} | |
if(!file_exists("./src/view/".$modelname)){ | |
mkdir("./src/view/".$modelname, 0755); | |
} | |
if(!file_exists("./src/view/".$modelname."/list.erl")){ | |
$file = fopen("./src/view/".$modelname."/list.erl", "w"); | |
$txt = "<ol>\n{% for ".$modelname." in ".$modelname."s %}\n"; | |
foreach($prop_fields as $field){ | |
$txt .= "<li>{{ ".$modelname.".".$field." }}</li>\n"; | |
} | |
$txt .="{% endfor %}\n"; | |
$txt .="</ol>\n"; | |
fwrite($file, $txt); | |
fclose($file); | |
} | |
if(!file_exists("./src/view/".$modelname."/form.erl")){ | |
$file = fopen("./src/view/".$modelname."/form.erl", "w"); | |
$txt = "<ol>\n{% for ".$modelname." in ".$modelname."s %}\n"; | |
foreach($prop_fields as $field){ | |
$txt .= "<li>{{ ".$modelname.".".$field." }}</li>\n"; | |
} | |
$txt .="{% endfor %}\n"; | |
$txt .="</ol>\n"; | |
fwrite($file, $txt); | |
fclose($file); | |
} | |
} | |
// Free result set | |
mysqli_free_result($result); | |
mysqli_close($con); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment