This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
/
generator.php
528 lines (428 loc) · 15.5 KB
/
generator.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
ini_set("display_errors", 0);
$set_namespace_models = 'models';
$set_namespace_controllers = 'controllers';
$set_dbModel = 'DatabaseModel';
$set_dbAddress = 'localhost';
$set_dbType = 'mysql';
$set_dbName = 'database';
$set_dbUser = 'username';
$set_dbPass = 'password';
////////////////////////////////////////////////////////////////////////////////
$set_tableName = 'users';
$set_rows = array(
'id' => 'int_unique_AI',
'name' => 'string',
'surname' => 'string',
'email' => 'string_unique',
'password' => 'string',
'phone' => 'string_unique',
'dni' => 'string_unique',
'birthday' => 'string',
'tipo' => 'int',
);
createTableModel($set_namespace_models, $set_dbModel, $set_tableName, $set_rows);
createTableController($set_namespace_controllers, $set_tableName);
////////////////////////////////////////////////////////////////////////////////
$set_tableName = 'saldo';
$set_rows = array(
'id_user' => 'int_unique',
'saldo' => 'int',
);
createTableModel($set_namespace_models, $set_dbModel, $set_tableName, $set_rows);
createTableController($set_namespace_controllers, $set_tableName);
////////////////////////////////////////////////////////////////////////////////
$set_tableName = 'token';
$set_rows = array(
'id' => 'int_unique_AI',
'user' => 'int',
'token' => 'string',
);
createTableModel($set_namespace_models, $set_dbModel, $set_tableName, $set_rows);
createTableController($set_namespace_controllers, $set_tableName);
////////////////////////////////////////////////////////////////////////////////
$set_tableName = 'salas';
$set_rows = array(
'id' => 'int_unique_AI',
'nombre' => 'string',
'foto' => 'string',
'descripcion' => 'string',
);
createTableModel($set_namespace_models, $set_dbModel, $set_tableName, $set_rows);
createTableController($set_namespace_controllers, $set_tableName);
////////////////////////////////////////////////////////////////////////////////
$set_tableName = 'reservas';
$set_rows = array(
'id' => 'int_unique_AI',
'sala' => 'int',
'fecha_inicial' => 'string',
'fecha_final' => 'string',
'user' => 'int',
'status' => 'int',
);
createTableModel($set_namespace_models, $set_dbModel, $set_tableName, $set_rows);
createTableController($set_namespace_controllers, $set_tableName);
////////////////////////////////////////////////////////////////////////////////
$set_tableName = 'news';
$set_rows = array(
'id' => 'int_unique_AI',
'title' => 'string',
'description' => 'string',
'icon' => 'string',
'date' => 'string',
);
createTableModel($set_namespace_models, $set_dbModel, $set_tableName, $set_rows);
createTableController($set_namespace_controllers, $set_tableName);
////////////////////////////////////////////////////////////////////////////////
$set_tableName = 'fiestas';
$set_rows = array(
'id' => 'int_unique_AI',
'fecha_inicial' => 'string',
'fecha_final' => 'string',
);
createTableModel($set_namespace_models, $set_dbModel, $set_tableName, $set_rows);
createTableController($set_namespace_controllers, $set_tableName);
////////////////////////////////////////////////////////////////////////////////
$set_tableName = 'compras';
$set_rows = array(
'id' => 'int_unique_AI',
'id_user' => 'string',
'id_producto' => 'int',
'cantidad' => 'int',
'precio' => 'int',
'saldo_utilizado' => 'int',
'fecha' => 'string',
);
createTableModel($set_namespace_models, $set_dbModel, $set_tableName, $set_rows);
createTableController($set_namespace_controllers, $set_tableName);
////////////////////////////////////////////////////////////////////////////////
createDatabaseModel($set_namespace_models, $set_dbModel);
createLogger($set_namespace_models);
createConfig($set_namespace_models, $set_namespace_controllers, $set_dbAddress, $set_dbType, $set_dbName, $set_dbUser, $set_dbPass);
createSanitizer($set_namespace_models);
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// GENERATING MODEL FILES //////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
function createTableModel($namespace_model, $dbModel, $tableName, $rows) {
$file = '<?php
class '.ucfirst($tableName).'Model extends '.$dbModel.' {';
foreach($rows as $key => $value){
$file .= '
private $'.$key.';';
}
foreach($rows as $key => $value){
$file .= '
/**
* @return string
*/
function get'.ucfirst($key).'() {
return $this->'.$key.';
}
/**
* @param string $'.$key.'
*/
function set'.ucfirst($key).'(string $'.$key.') {
$this->'.$key.' = $'.$key.';
}';
}
$file .= '
/**
* If the $identifier is not defined, this function will INSERT using the values defined in the object.
*
* If it\'s defined, this function will UPDATE using the values defined in the object, and the condition will be the $identifier.
* You can define the $condition too.
*
* Ex. save(array(\'id\' => 1));
*
* Ex. save(array(\'id\' => 1, \'status\' => 0), \'OR\');
*
*
* @param array $identifier Not required
* @param string $condition Not required, default value "AND"
*
* @return int Return the number of columns affected
*
*/
function save(array $identifier = null, string $condition = \'AND\') {
if($identifier){
$where = \'\';
foreach ($identifier as $key => $value){
(is_int($value)) ? $where .= $key.\' = \'.$value.\' \'.$condition.\' \' : $where .= $key.\' = "\'.$value.\'" \'.$condition.\' \';
}
return $this->update(\''.$tableName.'\', array(';
$string = '';
foreach($rows as $key => $value){
$string .= '
\''.$key.'\' => $this->get'.ucfirst($key).'(),';
}
$file .= rtrim($string,',');
$file .= '
), rtrim($where, $condition." "));
} else {
return $this->insert(\''.$tableName.'\', array(';
$string = '';
foreach($rows as $key => $value){
if(explode('_', $value)[2] !== 'AI'){
$string .= '
\''.$key.'\' => $this->get'.ucfirst($key).'(),';
}
}
$file .= rtrim($string,',');
$file .= '
));
}
}';
$file .= '
/**
* @param string $fields Not required, default value \'*\'
* @param string $order Not required, default value ""
* @param int $limit Not required, default value null
* @param int $offset Not required, default value null
* @return array Return a global array with every row in a single array
*/
function find($fields = \'*\', $order = "", $limit = null, $offset = null) {
$pdo = $this->select(\''.$tableName.'\', $where = "", $fields, $order, $limit, $offset);
return $pdo->fetchAll(PDO::FETCH_ASSOC);
}';
foreach($rows as $key => $value){
$file .= '
/**
* @param '.explode('_', $value)[0].' $'.$key.' Required
* @param string $fields Not required, default value \'*\'
* @param string $order Not required, default value ""
* @param int $limit Not required, default value null
* @param int $offset Not required, default value null
* @return array Return a single array with the values
*/
function findBy'.ucfirst($key).'($'.$key.', $fields = \'*\', $order = "", $limit = null, $offset = null) {
return $this->select(\''.$tableName.'\', \''.$key.' = \'.$'.$key.', $fields, $order, $limit, $offset)';
if(isset(explode('_', $value)[1]) && (explode('_', $value)[1] == 'unique')){
$file .= '->fetch(PDO::FETCH_ASSOC);';
}else{
$file .= '->fetchAll(PDO::FETCH_ASSOC);';
}
$file .= '
}';
}
$file .= '
/**
* @param array $identifier Not required
* @param string $condition Not required, default value "AND"
*
* Ex. findBy(array(\'id\' => 1));
*
* Ex. findBy(array(\'id\' => 1, \'status\' => 0), \'OR\');
*
*
* @param string $fields Not required, default value \'*\'
* @param string $order Not required, default value ""
* @param int $limit Not required, default value null
* @param int $offset Not required, default value null
* @return array Return a global array with every row in a single array
*/
function findBy(array $identifier, $condition = "AND" , $fields = \'*\', $order = "", $limit = null, $offset = null) {
$where = \'\';
foreach ($identifier as $key => $value){
(is_int($value)) ? $where .= $key.\' = \'.$value.\' \'.$condition.\' \' : $where .= $key.\' = "\'.$value.\'" \'.$condition.\' \';
}
return $this->select(\''.$tableName.'\', rtrim($where, $condition." "), $fields, $order, $limit, $offset)->fetchAll(PDO::FETCH_ASSOC);
}';
$file .= '
/**
* @param array $identifier Not required
* @param string $condition Not required, default value "AND"
*
* Ex. destroy(array(\'id\' => 1));
*
* Ex. destroy(array(\'id\' => 1, \'status\' => 0), \'OR\');
*
*
* @return int Return the number of columns affected
*/
function destroy(array $identifier, $condition = "AND") {
$where = \'\';
foreach ($identifier as $key => $value){
(is_int($value)) ? $where .= $key.\' = \'.$value.\' \'.$condition.\' \' : $where .= $key.\' = "\'.$value.\'" \'.$condition.\' \';
}
return $this->delete(\''.$tableName.'\', rtrim($where, $condition." "));
}';
$file .= '
/**
* Drop the database
*
* @return boolean True = No errors, False = Something went wrong
*/
function truncate(){
if($this->truncateTable(\''.$tableName.'\') === 0){
return true;
} else {
return false;
}
}
}';
$content = <<<EOF
$file
EOF;
if(!file_exists('./'.$namespace_model)){
mkdir('./'.$namespace_model);
}
file_put_contents('./'.$namespace_model.'/'.ucfirst($tableName).'Model.php', $content);
echo './'.$namespace_model.'/'.ucfirst($tableName).'Model.php Created <br>';
}
function createTableController($namespace_controller, $tableName){
$file = '<?php
class '.ucfirst($tableName).'Controller {
}';
$content = <<<EOF
$file
EOF;
if(!file_exists('./'.$namespace_controller)){
mkdir('./'.$namespace_controller);
}
file_put_contents('./'.$namespace_controller.'/'.ucfirst($tableName).'Controller.php', $content);
echo './'.$namespace_controller.'/'.ucfirst($tableName).'Controller.php Created <br>';
}
function createDatabaseModel($namespace, $dbModel){
$file = '<?php
class '.$dbModel.' {
private $conexion;
public function __construct() {
try {
$this->conexion = new PDO(_TYPE_ . ":dbname=" . _BBDD_ . ";host=" . _HOST_, _USER_, _PASS_);
$this->conexion->query("SET NAMES \'utf8\'");
logger::guardar("Connected to the DDBB");
} catch (PDOException $e) {
echo \'Connection failed: \' . $e->getMessage();
logger::guardar("Error while connecting: " . $e->getMessage());
}
}
public function query(string $query) {
try {
return $this->conexion->query($query)->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
logger::guardar("Error while executing the query: " . $e->getMessage());
}
}
public function select(string $table, $where = "", $fields = \'*\', $order = "", $limit = null, $offset = null){
$query = \'SELECT \' . $fields . \' FROM \' . $table
. (($where) ? \' WHERE \' . $where : "")
. (($order) ? \' ORDER BY \' . $order.\'\' : "")
. (($limit) ? \' LIMIT \' . $limit : "")
. (($offset && $limit) ? \' OFFSET \' . $offset : "");
logger::guardar($query);
return $this->conexion->query($query);
}
public function insert($table, array $data){
$fields = implode(\', \', array_keys($data));
$values = implode(\'", "\', array_values($data));
$query = \'INSERT INTO \' . $table . \' (\' . $fields . \') VALUES ("\' . $values . \'")\';
logger::guardar($query);
return $result = $this->conexion->exec($query);
}
public function update($table, array $data, $where = ""){
$set = array();
foreach ($data as $field => $value) {
if($value){
$value = (is_int($value))? $value : \'"\'.$value.\'"\';
$set[] = $field . \' = \' . $value;
}
}
$set = implode(\', \', $set);
$query = \'UPDATE \' . $table . \' SET \' . $set
. (($where) ? \' WHERE \' . $where : "");
logger::guardar($query);
return $this->conexion->exec($query);
}
public function delete($table, $where = ""){
$query = \'DELETE FROM \' . $table
. (($where) ? \' WHERE \' . $where : "");
logger::guardar($query);
return $this->conexion->exec($query);
}
public function truncateTable($table){
$query = \'TRUNCATE \' . $table;
logger::guardar($query);
return $this->conexion->exec($query);
}
}';
$content = <<<EOF
$file
EOF;
if(!file_exists('./'.$namespace)){
mkdir('./'.$namespace);
}
file_put_contents('./'.$namespace.'/'.ucfirst($dbModel).'.php', $content);
echo './'.$namespace.'/'.ucfirst($dbModel).'.php Created<br>';
echo '';
}
function createLogger($namespace_model){
$file = '<?php
class logger {
public static function guardar($texto) {
if(logging){
$nombre = _LOGS_FOLDER_."Log_".date("d-m-Y").".txt";
$fecha = date("d-m-Y H-i-s: ");
$fopen = fopen($nombre, "a");
fwrite($fopen, $fecha.$texto."\n");
fclose($fopen);
}
}
}';
$content = <<<EOF
$file
EOF;
if(!file_exists('./'.$namespace_model)){
mkdir('./'.$namespace_model);
}
file_put_contents('./'.$namespace_model.'/logger.php', $content);
echo './'.$namespace_model.'/logger.php Created <br>';
}
function createConfig($namespace_model, $namespace_controller, $dbAddress, $dbType,$dbName, $dbUser, $dbPass){
$file = '<?php
define("_LOGS_FOLDER_", "logs/");
if(file_exists(_LOGS_FOLDER_."logging")){
define("logging", true);
}else{
define("logging", false);
}
if(logging){
error_reporting(E_ALL);
ini_set("display_errors", 1);
}
define("_HOST_", "'.$dbAddress.'");
define("_BBDD_", "'.$dbName.'");
define("_USER_", "'.$dbUser.'");
define("_PASS_", "'.$dbPass.'");
define("_TYPE_", "'.$dbType.'");
spl_autoload_register(function ($class) {
if(strstr($class, \'Controller\')) {
include \'./'.$namespace_controller.'/\'.$class.\'.php\';
} else {
include \'./'.$namespace_model.'/\'.$class.\'.php\';
}
});';
$content = <<<EOF
$file
EOF;
file_put_contents('./config.php', $content);
echo './config.php Created <br>';
}
function createSanitizer($namespace_model){
$file = '<?php
class sanitize {
public static function string(string $string){
return filter_var($string, FILTER_SANITIZE_STRING);
}
public static function email(string $email){
return filter_var($email, FILTER_SANITIZE_EMAIL);
}
}';
$content = <<<EOF
$file
EOF;
if(!file_exists('./'.$namespace_model)){
mkdir('./'.$namespace_model);
}
file_put_contents('./'.$namespace_model.'/sanitize.php', $content);
echo './'.$namespace_model.'/sanitize.php Created <br>';
}