-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerator.php
More file actions
132 lines (106 loc) · 3.36 KB
/
generator.php
File metadata and controls
132 lines (106 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
require("./vendor/autoload.php");
use Medoo\Medoo;
class Generators {
public $db;
public function __construct() {
$setting = $this->settings();
$this->db = new Medoo([
'type' => 'mysql',
'host' => $setting['db.host'],
'database' => $setting['db.name'],
'username' => $setting['db.user'],
'password' => $setting['db.password'],
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
]);
}
private function settings(): array {
$file = __DIR__."/.setting";
$fopen = fopen($file, "r");
$arr = [];
while(($buffer = fgets($fopen, 4096)) !== false) {
preg_match_all("/([^ =]+)/", $buffer, $match);
if(count($match) > 1) {
$key = trim($match[0][0]);
$value = trim($match[0][1]);
$arr[$key] = $value;
}
}
return $arr;
}
private function filterNames(string $name): string {
$className = $name;
// replace _ to spacing
$className = str_replace("_", " ", $className);
// remove s
if(substr($name, -1) == "s") {
$className = substr($name, 0, strlen($name) - 1);
}
// make it camel case
$className = ucwords($className);
$className = str_replace(" ", "", $className);
return $className;
}
public function createModel(string $tableName) {
$setting = $this->settings();
$name = $this->filterNames($tableName);
$dbname = $setting['db.name'];
$query = "SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = '$dbname' AND table_name = '$tableName'";
$result = $this->db->query($query)->fetchAll();
$declaration = "";
$rules = "";
$index = 0;
foreach ($result as $item) {
$fieldName = lcfirst($this->filterNames($item['COLUMN_NAME']));
$type = ($item['DATA_TYPE'] != "int") ? "string" : "int";
$declaration .= 'public '.$type.' $'.$fieldName.';'."\n\t";
$rules .= '\''.$fieldName.'\' => "'.$item['COLUMN_NAME'].'",'."\n\t\t";
if($index > 0) {
$rules .= "\t";
}
$index++;
}
$str = '<?php
namespace App\Applications\Models;
use App\Application\Cores\Model;
class '.$name.' extends Model {
'.$declaration.'
public function toJSON(): array {
return get_object_vars($this);
}
public function tableName(): string {
return "'.$tableName.'";
}
public function rules(): array {
return [
'.$rules.'
];
}
}';
$file = fopen(__DIR__."/src/Applications/Models/".$name.".php", "w+");
if($file) {
$put = fputs($file, $str);
fclose($file);
if($put) {
echo "Successfully added";
} else {
echo "Nothing happen";
}
} else {
echo "Failed creating file";
}
}
}
if (isset($argc)) {
$type = $argv[1];
$name = $argv[2];
if(count($argv) == 3) {
if($type == "model") {
$generator = new Generators();
$generator->createModel($name);
}
} else {
echo "Failed";
}
}