1 | <?php namespace Hafiz\Commands\Controller; |
||
2 | |||
3 | use CodeIgniter\CLI\BaseCommand; |
||
4 | use CodeIgniter\CLI\CLI; |
||
5 | use Config\Services; |
||
6 | use ReflectionClass; |
||
7 | use ReflectionException; |
||
8 | |||
9 | /** |
||
10 | * Creates a new Controller file. |
||
11 | * @package CodeIgniter\Commands |
||
12 | * @extend BaseCommand |
||
13 | */ |
||
14 | class Generate extends BaseCommand |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * The group command is heading under all |
||
19 | * commands will be listed |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $group = 'CI4-Recharge'; |
||
23 | |||
24 | /** |
||
25 | * The Command's name |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $name = 'generate:controller'; |
||
29 | |||
30 | /** |
||
31 | * The Command's short description |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $description = 'Creates a controller from Model.'; |
||
35 | |||
36 | /** |
||
37 | * The Command's usage |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $usage = 'generate:controller [controller_name] [Options]'; |
||
41 | |||
42 | /** |
||
43 | * The Command's Arguments |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $arguments = [ |
||
47 | 'controller_name' => 'The Controller file name' |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * The Command's Options |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $options = [ |
||
55 | '-n' => 'Set controller namespace', |
||
56 | '-b' => 'Set controller base/extends class', |
||
57 | '-s' => 'Set controller sub-directory after namespace', |
||
58 | '-rest' => 'Set controller sub-directory after namespace', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * Creates a new Controller file with the current timestamp. |
||
63 | * @param array $params |
||
64 | * @return void |
||
65 | * @throws ReflectionException |
||
66 | */ |
||
67 | public function run(array $params = []) |
||
68 | { |
||
69 | helper(['inflector', 'filesystem']); |
||
70 | |||
71 | $name = array_shift($params); |
||
72 | |||
73 | if (empty($name)) { |
||
74 | $name = CLI::prompt(lang('Recharge.nameController')); |
||
75 | } |
||
76 | |||
77 | if (empty($name)) { |
||
78 | CLI::error(lang('Recharge.badControllerName')); |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | //Class Namespace |
||
83 | $ns = $params['-n'] ?? CLI::getOption('n'); |
||
84 | |||
85 | //Extends Base Class |
||
86 | $base = $params['-b'] ?? CLI::getOption('b'); |
||
87 | |||
88 | //Sub directory after Namespace |
||
89 | $sub = $params['-s'] ?? CLI::getOption('s'); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
90 | |||
91 | //Rest Controller Style |
||
92 | $is_rest = CLI::getOption('rest'); |
||
93 | |||
94 | /** @var string real path $homepath */ |
||
95 | $homepath = APPPATH; |
||
96 | |||
97 | $package = "App"; |
||
98 | $baseNameSpace = 'use App\Controllers\BaseController;'; |
||
99 | $parentController = 'BaseController'; |
||
100 | |||
101 | //Finding Namespace Location |
||
102 | if (!empty($ns)) { |
||
103 | // Get all namespaces |
||
104 | $namespaces = Services::autoloader()->getNamespace(); |
||
105 | |||
106 | foreach ($namespaces as $namespace => $path) { |
||
107 | if ($namespace === $ns) { |
||
108 | $homepath = realpath(reset($path)); |
||
109 | $package = $ns; |
||
110 | break; |
||
111 | } |
||
112 | } |
||
113 | } else { |
||
114 | $ns = "App"; |
||
115 | } |
||
116 | |||
117 | //Finding Base Class |
||
118 | if (!empty($base)) { |
||
119 | // Get all namespaces |
||
120 | $namespaces = Services::autoloader()->getNamespace(); |
||
121 | |||
122 | foreach ($namespaces as $namespace => $path) { |
||
123 | if ($namespace == "App" || $namespace == $ns) { |
||
124 | $full_path = realpath(reset($path)) . "/Controllers/" . $base . ".php"; |
||
125 | if (file_exists($full_path)) { |
||
126 | $tempObj = new ReflectionClass($namespace . "\Controllers\\" . $base); |
||
127 | $baseNameSpace = 'use ' . $tempObj->getName() . ";"; |
||
128 | $package = $ns; |
||
129 | break; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } else { |
||
134 | $base = $parentController; |
||
135 | } |
||
136 | |||
137 | // Always use UTC/GMT so global teams can work together |
||
138 | $fileName = pascalize($name); |
||
139 | |||
140 | // full path |
||
141 | $path = $homepath . '/Controllers/' . $fileName . '.php'; |
||
142 | |||
143 | // Class name should be Pascal case |
||
144 | $name = pascalize($name); |
||
145 | $date = date("d F, Y h:i:s A"); |
||
146 | |||
147 | //Basic Controller Template |
||
148 | $basicTemplate = <<<EOD |
||
149 | <?php namespace $ns\Controllers; |
||
150 | |||
151 | $baseNameSpace |
||
152 | |||
153 | /** |
||
154 | * @class $name |
||
155 | * @author CI-Recharge |
||
156 | * @package $package |
||
157 | * @extend $base |
||
158 | * @created $date |
||
159 | */ |
||
160 | |||
161 | |||
162 | class $name extends $base |
||
163 | { |
||
164 | /** |
||
165 | * $name constructor |
||
166 | */ |
||
167 | public function __construct() |
||
168 | { |
||
169 | |||
170 | } |
||
171 | |||
172 | public function index() |
||
173 | { |
||
174 | echo 'Hello World!'; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | EOD; |
||
179 | |||
180 | //REST Controller Template |
||
181 | $restTemplate = <<<EOD |
||
182 | <?php namespace $ns\Controllers; |
||
183 | |||
184 | $baseNameSpace |
||
185 | use CodeIgniter\RESTful\ResourceController; |
||
186 | |||
187 | /** |
||
188 | * @class $name |
||
189 | * @author CI-Recharge |
||
190 | * @package $package |
||
191 | * @extends ResourceController |
||
192 | * @created $date |
||
193 | */ |
||
194 | |||
195 | |||
196 | class $name extends ResourceController |
||
197 | { |
||
198 | /** |
||
199 | * $name constructor |
||
200 | */ |
||
201 | public function __construct() |
||
202 | { |
||
203 | |||
204 | } |
||
205 | /** |
||
206 | * @return array|string |
||
207 | */ |
||
208 | public function index() |
||
209 | { |
||
210 | |||
211 | return view(''); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @return array|string |
||
216 | */ |
||
217 | public function create() |
||
218 | { |
||
219 | |||
220 | return view(''); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return array|string |
||
225 | */ |
||
226 | public function store() |
||
227 | { |
||
228 | |||
229 | return ; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param int|null \$id |
||
234 | * @return array|string |
||
235 | */ |
||
236 | public function show(int \$id = null){ |
||
237 | |||
238 | return view(''); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param int|null \$id |
||
243 | * @return array|string |
||
244 | */ |
||
245 | public function edit(int \$id = null) |
||
246 | { |
||
247 | |||
248 | return view(''); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param int|null \$id |
||
253 | * @return array|string |
||
254 | */ |
||
255 | public function update(int \$id = null) |
||
256 | { |
||
257 | |||
258 | return ; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param int|null \$id |
||
263 | * @return array|string |
||
264 | */ |
||
265 | public function delete(int \$id = null) |
||
266 | { |
||
267 | |||
268 | return ; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | EOD; |
||
273 | |||
274 | $template = ($is_rest == true) ? $restTemplate : $basicTemplate; |
||
275 | |||
276 | if (!write_file($path, $template)) { |
||
277 | CLI::error(lang('Recharge.writeError', [$path])); |
||
278 | return; |
||
279 | } |
||
280 | |||
281 | CLI::write('Created file: ' . CLI::color(str_replace($homepath, $ns, $path), 'green')); |
||
282 | } |
||
283 | |||
284 | } |
||
285 |