-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_controller
More file actions
207 lines (146 loc) · 4.58 KB
/
create_controller
File metadata and controls
207 lines (146 loc) · 4.58 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
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
<?php
//parent directories
define('DS',DIRECTORY_SEPARATOR);
define('ROOT_PATH',dirname(__FILE__).DS);
define('APP',ROOT_PATH.'app'.DS);
define('PUBLIC_PATH',ROOT_PATH.'public'.DS);
require_once 'app/config/config_constants.php';
require_once 'vendor/autoload.php';
if(php_sapi_name() =='cli')
{
if(isset($argv[1]))
{
$controller_name = $argv[1];
$is_resoruce = isset($argv[2])?$argv[2]:'';
$model = isset($argv[3])?$argv[3]:'';
clearstatcache();
if(!file_exists(CONTROLLERS.$controller_name.'.php'))
{
if($is_resoruce =='resource' && $model !=='model')
{
file_put_contents(CONTROLLERS.$controller_name.'.php',getConrolerResourceString($controller_name));
die('Resource controller created '.$controller_name.' successfully');
}
elseif($is_resoruce =='resource' && $model =='model')
{
file_put_contents(CONTROLLERS.$controller_name.'.php',getConrolerResourceString($controller_name));
file_put_contents(MODELS.getModelString($controller_name,true).'.php',getModelString($controller_name));
die('Resource controller and model '.getModelString($controller_name,true).' created '.$controller_name.' successfully');
}
elseif($controller_name && $is_resoruce =='model')
{
file_put_contents(CONTROLLERS.$controller_name.'.php',getConrolerString($controller_name));
file_put_contents(MODELS.getModelString($controller_name,true).'.php',getModelString($controller_name));
die('controller and model '.getModelString($controller_name,true).' created '.$controller_name.' successfully');
}
else
{
file_put_contents(CONTROLLERS.$controller_name.'.php',getConrolerString($controller_name));
die('controller created '.$controller_name.' successfully');
}
}
else
{
die("$controller_name already craeted before");
}
}
else
{
die('no data input found');
}
}
else
{
die('');
}
function getConrolerString($controller)
{
$string ='<?php
namespace App\Controllers;
use App\Core\Controller;
use Style\Style;
class '.$controller.' extends Controller
{
public function __construct()
{
//middleware_array,except_methods array(optional)
//$this->middleware([\'test\']);
}
public function home()
{
// $request->validate([\'username\'=>\'required|min:2\']);
// $model = new model(12);
//$model->columns[\'model_title\']=\'updated\';
//$model->columns[\'model_price\']=2500000;
//$model->create([\'model_title\'=>\'coding model\']); //for mass assignment
//$model->save(); //for insert
//$model->amend();// for update
//$model->purge(); //for delete
//$model->deleteSoft(12); //for soft delete
}
}';
return $string;
}
function getConrolerResourceString($controller)
{
$string ='<?php
namespace App\Controllers;
use App\Core\Controller;
use Style\Style;
class '.$controller.' extends Controller
{
public function __construct()
{
//middleware_array,except_methods array(optional)
//$this->middleware([\'test\']);
}
public function index()
{
// $request->validate([\'username\'=>\'required|min:2\']);
// $model = new model(12);
//$model->columns[\'model_title\']=\'updated\';
//$model->columns[\'model_price\']=2500000;
//$model->create([\'model_title\'=>\'coding model\']); //for mass assignment
//$model->save(); //for insert
//$model->amend();// for update
//$model->purge(); //for delete
//$model->deleteSoft(12); //for soft delete
}
public function create()
{
}
public function store()
{
}
public function show($id)
{
}
public function edit($id)
{
}
public function update()
{
}
public function destroy($id)
{
}
}';
return $string;
}
function getModelString($model,$return_name=false)
{
$model=preg_replace('#Controller|Controllers|controller|controllers#','',$model);
if($return_name)
{
return $model;
}
$string ='<?php
namespace App\Models;
use App\Core\Model;
class '.$model.' extends Model
{
//you have to set table attributes her $id,$column_2,$column_3 ...
//you should set table name attribute or we can predict it like User to be users ,UserCategory user_categories
}';
return $string;
}