-
Notifications
You must be signed in to change notification settings - Fork 9
/
firecode
executable file
·244 lines (197 loc) · 4.67 KB
/
firecode
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
#!/usr/local/env php
<?php
class Firecode
{
private function __construct() {}
private function __clone() {}
/**
* @param $argc
* @param $argv
* @return int
*/
public static function run($argc, $argv)
{
$argc -= 1;
array_shift($argv);
if (in_array($argc, [0, 1, 2]) || (isset($argv[0]) && $argv[0] === "help")) {
return static::usage();
}
$action = $argv[0];
$param = $argv[1];
$name = $argv[2];
$status = 0;
switch ($action) {
case 'make':
switch ($param) {
case '--model':
case '-m':
static::createModel($name);
break;
case '--controller':
case '-c':
$status = static::createController($name);
break;
case '--middleware':
case '-r':
$status = static::createMiddleware($name);
break;
default:
$status = static::usage();
break;
}
break;
case 'migrate':
$status = 0;
echo "\033[1;32mIn dev\033[00m";
break;
case 'help':
$status = static::usage();
break;
default:
$status = static::usage();
break;
}
return $status;
}
/**
* @param $middlewareName
* @return int
*/
private static function createMiddleware($middlewareName)
{
if (file_exists("app/Http/Middleware/$middlewareName.php")) {
echo "\033[1;31mMiddleware <\033[1;33m@\033[1;31m$middlewareName\033[00m\033[1;31m> already exist.\033[00m";
return 1;
}
$middlewareTemplate = <<< CM
<?php
namespace App\Http\Middleware;
use Closure;
use Snoop\Http\Request;
use Snoop\Database\Database as DB;
class {$middlewareName}
{
/**
* Fonction de lancement du middleware.
*
* @param Request \$req
* @param Closure \$next
*
* @return mixed
*/
public function handler(Request \$req, Closure \$next = null)
{
// do something here
}
}
CM;
file_put_contents("app/Http/Middleware/$middlewareName.php", $middlewareTemplate);
echo "\033[1;32mMiddleware \033[00m@@{$middlewareName}\033[1;32m Created.\033[00m";
return 0;
}
/**
* @param $controllerName
* @return int
*/
private static function createController($controllerName)
{
if (file_exists("app/Http/Controller/$controllerName.php")) {
echo "\033[1;31mController <\033[1;33m@\033[1;31m$controllerName\033[00m\033[1;31m> already exist.\033[00m";
return 1;
}
$controllerTemplate = <<< CC
<?php
namespace App\Http\Controller;
use Exception;
use Snoop\Http\Request;
use Snoop\Database\Database as DB;
use App\Provider\BaseController as Controller;
class {$controllerName} extends Controller
{
/**
* Start point
*
* @param Request \$req
* @return mixed
*/
public function index(Request \$req)
{
// do something here
return response("Hello world", 304);
}
/**
* Delete data
*
* @param Request \$req
* @return mixed
*/
public function delete(Request \$req)
{
// do something here
}
/**
* Add information
*
* @param Request \$req
* @param Response \$res
*/
public function add(Request \$req)
{
// do something here
// eg. insert("insert into your_table values(1, 'name')");
}
/**
* get all information
*
* @param Request \$req
* @return mixed
*/
public function get(Request \$req)
{
// do something here.
// e.g table("your_table")->get() // retourne une liste
}
}
CC;
file_put_contents("app/Http/Controller/$controllerName.php", $controllerTemplate);
echo "\033[1;32mController \033[00m@@{$controllerName}\033[1;32m Created.\033[00m";
return 0;
}
/**
* @param $modelName
* @return int
*/
private static function createModel($modelName)
{
if (file_exists("app/Http/Model/$modelName.php")) {
echo "\033[1;33mModel <\033[1;33m@\033[1;31m$modelName\033[00m\033[1;31m> already exist.\033[00m";
return 1;
}
echo "\033[1;32mModel \033[00m@@{$modelName}\033[1;32m Created.\033[00m";
return 0;
}
/**
* @return int
*/
private static function usage()
{
$usage = <<< USAGE
firecode usage: php firecode :command :option :stdout-file-name
\033[1;31mcommand\033[00m:
\033[1;32mmake\033[00m create a user class
option:
\033[1;33m--controller\033[00m | \033[1;32m-c\033[00m for create controller
\033[1;33m--model\033[00m | \033[1;32m-m\033[00m for create model
\033[1;33m--middleware\033[00m | \033[1;32m-r\033[00m for create middleware
\033[1;32mmigrate\033[00m create migration of user model
option:
\033[1;33m--up\033[00m To create migration \033[1;31m(in building)\033[00m
\033[1;33m--down\033[00m To drop migration \033[1;31m(in building)\033[00m
\033[1;33m--truncate\033[00m To truncate table \033[1;31m(in building)\033[00m
USAGE;
echo $usage;
return 1;
}
}
$s = Firecode::run($argc, $argv);
exit($s);