-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.inc
146 lines (124 loc) · 3.47 KB
/
functions.inc
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
<?php
/**
* Functions.
*
* @author Florian Sauter <floonweb@gmail.com>
* @version 0.1
* @package crudy
*/
/**
* Returns the stdin input from the cli.
* @param string $question
* @param mixed $defaultValue
*
* @return the user input or the passed default value if the user input was empty.
*/
function getUserInput($question, $defaultValue = '') {
say($question, 'purple', false);
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$input = trim($line);
if(empty($input) && empty($defaultValue) === false) {
return $defaultValue;
} else {
return $input;
}
}
/**
* Returns the a selected database name.
* @param Database $db
* @return the name of the database.
*/
function getDatabaseFromUserInput(Database $db) {
$databases = $db->getDatabases();
print_r($databases);
$input = getUserInput('Please enter the index of the database you wish to use: ');
if(!is_numeric($input) || $input < 0 || $input >= count($databases)) {
abort();
}
$database = $databases[$input];
return $database;
}
/**
* Returns the a selected table name.
* @param Database $db
* @return the name of the table.
*/
function getTableFromUserInput(Database $db) {
$tables = $db->getTables();
print_r($tables);
$input = getUserInput('Please enter the index of the table you wish to use: ');
if(!is_numeric($input) || $input < 0 || $input >= count($tables)) {
abort();
}
$table = $tables[$input];
return $table;
}
/**
* Prints a message.
*
* @param string $message
* @param string $color
* @param string $newLine
*/
function say($message, $color = null, $newLine = true) {
echo ColorCLI::getColoredString($message, $color);
if($newLine) {
br();
}
}
function br() {
echo "\n";
}
function hr() {
echo "--------------------------------------------------------\n";
}
function abort($message = "\nABORTING!") {
say($message, 'red');
exit;
}
function displayHelp() {
say('Try something like:');
br();
say('./crudy');
say('./crudy s:localhost');
say('./crudy s:localhost u:root');
say('./crudy s:localhost u:root p:root');
say('./crudy s:localhost u:root p:root d:mydatabase');
say('./crudy s:localhost u:root p:root d:mydatabase t:mytable');
say('./crudy s:localhost u:root p:root d:mydatabase t:mytable tpl:path/to/a/templatefile');
say('./crudy s:localhost u:root p:root d:mydatabase t:mytable tpl:path/to/a/templatefile o:folder/to/generate/the/model/');
say('./crudy s:localhost u:root p:root d:mydatabase t:mytable tpl:path/to/a/templatefile o:folder/to/generate/the/model/$.php v:1');
br();
say('You can omit as many arguments as you desire.');
br();
say('s = server');
say('u = user');
say('p = password');
say('d = database');
say('t = table');
say('tpl = template file to use [path/to/a/templatefile]');
say('o = output (target) folder using [path/to/generate/the/my/folder]');
say('v = verbose [1|true]');
br();
exit;
}
/**
* Generates the model.
* @param string $templateFile
* @param string $className
* @param string[] $fields
* @return the generated model class string.
*/
function generateModel($templateFile, $className, $fields) {
$fieldArray = array();
foreach($fields as $fieldName) {
$fieldArray[] = array('name' => $fieldName, 'uname' => ucfirst($fieldName));
}
$template = new Template($templateFile);
$template->setVar('className', $className);
$template->setVar('fields', $fieldArray);
$template->setVar('getterFields', $fieldArray);
$template->setVar('setterFields', $fieldArray);
return $template->toString();
}