-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnofamework.php
More file actions
156 lines (122 loc) · 5.38 KB
/
nofamework.php
File metadata and controls
156 lines (122 loc) · 5.38 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/php
_ _ ______ _
| \ | | | ____| | |
| \| | ___ | |__ __ _ _ __ ___ _____ _____ _ __| | __
| . ` |/ _ \| __| / _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
| |\ | (_) | | | (_| | | | | | | __/\ V V / (_) | | | <
|_| \_|\___/|_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
<?php
/**
* This script is used to generate code for you.
* - We have generated... functions. These create files for you.
* - We have added... functions. These alter currently existing files.
*/
if (isset($argv[1])) {
$input = strtolower($argv[1]);
switch($input) {
case "generate": if(isset($argv[2])) { generateScaffold(lcfirst($argv[2])); } // AdminProduct could be the input, soo only lower the first character.
else { echo" [name] Please give in a name as a second argument too. \n"; } break;
case "seed": seedDB(); break;
case "routes": showRoutes(); break;
default: printHelp();
}
} else { printHelp(); }
echo("\n"); // To fix the layout we just add an enter at the end.
/** Print explanation of ho to use the tool */
function printHelp() {
echo "you can use the following functions
generate [name] - generates a scaffold
seed - seeds the database
routes - shows all SuperGlobal route paths \n";
}
/** Sho all SuperGlobal Routes that will be generated by the routes file. */
function showRoutes() {
$string = file_get_contents(__DIR__."/config/routes.php");
var_dump(eval("
require(__DIR__.'/config/config.php');
require(__DIR__.'/core/helpers.php');
require(__DIR__.'/core/database/database.php');
require(__DIR__.'/core/router/router.php');
\$router = new Router();
\$router->echoGenerateGlobalConstant = true;
?>".$string));
}
/** Seed the database */
function seedDB() {
eval("
define('CONSOLE_MESSAGES_ON', false);
require(__DIR__.'/config/config.php');
require(__DIR__.'/core/helpers.php');
require(__DIR__.'/core/database/database.php');
\$DB = new Database();
echo' ^^ Ignore what you see here ^^\n\n';
\$DB->seed();
echo'\n';
");
}
/** Create the full MVC package */
function generateScaffold($name) {
generateModel($name);
generateController($name);
generateViews($name);
addRoutes($name);
addSeeds($name);
seedDB();
}
/** Create a new class in the /app/models folder. */
function generateModel($name) {
createFile("/nofamework/generateModel.txt.php", "/app/models/".$name.".php", $name);
}
/** Create a new Controller width the INDEX, SHOW, NEW, EDIT functions in the /app/models folder. */
function generateController($name) {
createFile("/nofamework/generateController.txt.php", "/app/controllers/".$name."Controller.php", $name);
}
/** Create new INDEX, SHOW, NEW, EDIT views in a sepparate folder that is then placed in /app/views/. */
function generateViews($name) {
mkdir(__DIR__."/app/views/".$name, 0755);
createFile("/nofamework/generateViewIndex.txt.php", "/app/views/".$name."/".$name."_index.php", $name);
createFile("/nofamework/generateViewShow.txt.php", "/app/views/".$name."/".$name."_show.php", $name);
createFile("/nofamework/generateViewNew.txt.php", "/app/views/".$name."/".$name."_new.php", $name);
createFile("/nofamework/generateViewEdit.txt.php", "/app/views/".$name."/".$name."_edit.php", $name);
}
/** Add our crud routes to the /config/routes.php file */
function addRoutes($name) { // TODO: as soon as the resource is implemented in the router we can simply do this
$Name = ucfirst($name);
$names = $name."s";
$string = " \n \n
// ".$Name." crud
\$router->get('/$names', '".$Name."Controller#index');
\$router->get('/$names/:ID/show', '".$Name."Controller#show');
\$router->get('/$names/new', '".$Name."Controller#new');
\$router->get('/$names/:ID/edit', '".$Name."Controller#edit');
\$router->post('/$names/create', '".$Name."Controller#create');
\$router->post('/$names/:ID/update', '".$Name."Controller#update');
\$router->post('/$names/:ID/delete', '".$Name."Controller#delete');";
echo "\t \e[32m + NEW ROUTES \e[0m \n";
file_put_contents(__DIR__."/config/routes.php", $string, FILE_APPEND);
}
/** Add a new Table to our seeds file and add some test data. */
function addSeeds($name) {
$Name = ucfirst($name);
$NAMES = strtoupper($name)."s";
$Names = $Name."s";
$string = "\n \n
// ".$NAMES."
\$DB->execute(\"CREATE TABLE ".$Names."(
ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR( 100 ) NOT NULL);\");
\$DB->execute(\"INSERT INTO ".$Names." (name) VALUES ('test1'), ('test2')\");";
echo "\t \e[32m + NEW SEEDS \e[0m \n";
file_put_contents(__DIR__."/config/seeds.php", $string, FILE_APPEND);
}
/** Create a new file at the given $URI that has the text from $text inside of it */
function createFile($fromURL, $toURL, $name) {
$Name = ucfirst($name);
$NAME = strtoupper($name);
$names = $name."s";
$Names = $Name."s";
echo "\t \e[32m + ".$toURL." \e[0m \n";
$file = file_get_contents(__DIR__.$fromURL);
$string = eval("return '".$file."';"); // pure meta programming right here
file_put_contents(__DIR__.$toURL, $string);
}