-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
createClass.php
91 lines (79 loc) · 3.29 KB
/
createClass.php
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
<?php
require_once 'vendor/autoload.php';
use App\Database\TableCreator;
header('Content-Type: application/json');
try {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = $_POST;
if (!isset($data['className'], $data['propertyCount'])) {
throw new Exception('Nome da classe é obrigatória e ao menos uma propriedade!');
}
$className = ucfirst($data['className']);
$propertyCount = $data['propertyCount'];
$properties = [];
$useDateTime = false;
for ($i = 0; $i < $propertyCount; $i++) {
if (!isset($data["propName{$i}"], $data["propType{$i}"])) {
throw new Exception("Nome e tipo da propriedade são obrigatórios {$i}!");
}
$properties[] = [
'name' => $data["propName{$i}"],
'type' => $data["propType{$i}"],
];
if ($data["propType{$i}"] === 'datetime') {
$useDateTime = true;
}
}
$classTemplate = "<?php\n\nnamespace App\Model;\n\n";
if ($useDateTime) {
$classTemplate .= "use DateTime;\n\n";
}
$classTemplate .="class $className {\n";
foreach ($properties as $property) {
$classTemplate .= " private {$property['type']} \${$property['name']};\n";
}
foreach ($properties as $property) {
$camelCasePropName = ucfirst($property['name']);
$classTemplate .= "\n public function get$camelCasePropName() {\n";
$classTemplate .= " return \$this->{$property['name']};\n }\n";
$classTemplate .= "\n public function set$camelCasePropName({$property['type']} \${$property['name']}) {\n";
$classTemplate .= " \$this->{$property['name']} = \${$property['name']};\n";
$classTemplate .= " return \$this;\n }\n";
}
$classTemplate .= "}\n";
$dir = "Backend/Model";
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$dir2 = "Backend/Controller";
if (!is_dir($dir2)) {
mkdir($dir2, 0777, true);
}
$dir3 = "Backend/Routes";
if (!is_dir($dir3)) {
mkdir($dir3, 0777, true);
}
file_put_contents("$dir/$className.php", $classTemplate);
$make = new TableCreator();
require_once "$dir/$className.php";
$fullClassName = "App\\Model\\$className";
$classeMigrate = new $fullClassName();
$make->createTableFromModel($classeMigrate,$data['tipo']);
$make->createController($classeMigrate);
$make->createTests($classeMigrate);
$make->createRoute($classeMigrate);
if(isset($data['spa']) && $data['spa'] === 'true'){
$make->createJsComponents($className, $properties);
}else{
$make->createJsClasses($className, $properties);
$make->createHtmlForm($className, $properties);
$make->createHtmlTemplate($className);
}
echo json_encode(['success' => true]);
} else {
throw new Exception('Invalid request method!');
}
} catch (Exception $e) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}