Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.generator.php
More file actions
505 lines (425 loc) · 15.8 KB
/
Copy pathcontroller.generator.php
File metadata and controls
505 lines (425 loc) · 15.8 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?php
/**
* Controller Generator
* Generates a controller with basic CRUD method templates and adds routes
*/
function generateController($controllerName)
{
$controllerName = ucfirst($controllerName);
// Strip 'Controller' suffix if it exists
if (str_ends_with($controllerName, 'Controller')) {
$baseName = substr($controllerName, 0, -10);
} else {
$baseName = $controllerName;
$controllerName = $baseName . 'Controller';
}
$lowerCaseBaseName = strtolower($baseName);
$controllerContent = <<<EOT
<?php
namespace app\\controllers;
use app\\core\\Controller;
use app\\core\\Request;
use app\\core\\Response;
class {$controllerName} extends Controller
{
/**
* Display a listing of the resources
*
* @return void
*/
public function index(): void
{
// Dummy data for demonstration
\$items = [
['id' => 1, 'name' => 'Item 1', 'description' => 'Description for item 1'],
['id' => 2, 'name' => 'Item 2', 'description' => 'Description for item 2'],
['id' => 3, 'name' => 'Item 3', 'description' => 'Description for item 3'],
];
\$this->view('{$lowerCaseBaseName}/index', [
'items' => \$items,
'title' => '{$baseName} Management'
]);
}
/**
* Display the form for creating a new resource
*
* @return void
*/
public function create(): void
{
\$this->view('{$lowerCaseBaseName}/create', [
'title' => 'Create New {$baseName}'
]);
}
/**
* Store a newly created resource
*
* @return void
*/
public function store(): void
{
if (!Request::isPostMethod()) {
\$this->view('errors/405');
return;
}
// Get form data
\$data = \$_POST;
// Process the form submission
// TODO: Add your store logic here
// Redirect to index page
header("Location: /{$lowerCaseBaseName}");
exit;
}
/**
* Display the specified resource
*
* @return void
*/
public function show(): void
{
\$id = Request::input('id');
// Dummy item for demonstration
\$item = [
'id' => \$id,
'name' => 'Sample {$baseName}',
'description' => 'This is a sample {$baseName} with ID ' . \$id,
'created_at' => date('Y-m-d H:i:s')
];
\$this->view('{$lowerCaseBaseName}/show', [
'item' => \$item,
'title' => '{$baseName} Details'
]);
}
/**
* Display the form for editing the resource
*
* @return void
*/
public function edit(): void
{
\$id = Request::input('id');
// Dummy item for demonstration
\$item = [
'id' => \$id,
'name' => 'Sample {$baseName}',
'description' => 'This is a sample {$baseName} with ID ' . \$id,
'created_at' => date('Y-m-d H:i:s')
];
\$this->view('{$lowerCaseBaseName}/edit', [
'item' => \$item,
'title' => 'Edit {$baseName}'
]);
}
/**
* Update the specified resource
*
* @return void
*/
public function update(): void
{
if (!Request::isPostMethod()) {
\$this->view('errors/405');
return;
}
\$id = Request::input('id');
// Get form data
\$data = \$_POST;
// Process the form submission
// TODO: Add your update logic here
// Redirect to show page
header("Location: /{$lowerCaseBaseName}/show?id={\$id}");
exit;
}
/**
* Remove the specified resource
*
* @return void
*/
public function destroy(): void
{
if (!Request::isPostMethod()) {
\$this->view('errors/405');
return;
}
\$id = Request::input('id');
// Process the deletion
// TODO: Add your delete logic here
// Redirect to index page
header("Location: /{$lowerCaseBaseName}");
exit;
}
}
EOT;
// Create directories if they don't exist
if (!is_dir("app/controllers")) {
mkdir("app/controllers", 0755, true);
}
// Create controller file
file_put_contents("app/controllers/{$controllerName}.php", $controllerContent);
echo "{$controllerName} has been created in app/controllers directory.\n";
// Create view directory and files for this controller
createViewsForController($baseName, $lowerCaseBaseName);
// Ensure error views exist
createErrorViews();
// Add routes for this controller
addWebRoutes($baseName, $controllerName, $lowerCaseBaseName);
echo "Controller generation completed successfully.\n";
}
/**
* Add web routes for the controller
*/
function addWebRoutes($baseName, $controllerName, $lowerBaseName) {
$routeFile = "app/routes/web.php";
if (file_exists($routeFile)) {
$content = file_get_contents($routeFile);
// Check if route import exists
if (strpos($content, 'use app\core\{Router}') === false) {
$content = "<?php\n\nuse app\core\{Router};\n\n" . $content;
}
// Check if the routes already exist
if (strpos($content, "Router::resource('{$lowerBaseName}'") === false) {
// Check if Router::resource method exists or we need to add individual routes
if (strpos($content, 'Router::resource') !== false) {
// Router::resource exists, use it
$routeContent = "\n// {$baseName} routes\nRouter::resource('{$lowerBaseName}', '{$controllerName}');\n";
} else {
// Add individual routes
$routeContent = <<<EOT
// {$baseName} routes
Router::get('/{$lowerBaseName}', '{$controllerName}', 'index');
Router::get('/{$lowerBaseName}/create', '{$controllerName}', 'create');
Router::post('/{$lowerBaseName}/store', '{$controllerName}', 'store');
Router::get('/{$lowerBaseName}/show', '{$controllerName}', 'show');
Router::get('/{$lowerBaseName}/edit', '{$controllerName}', 'edit');
Router::post('/{$lowerBaseName}/update', '{$controllerName}', 'update');
Router::post('/{$lowerBaseName}/destroy', '{$controllerName}', 'destroy');
EOT;
}
// Add routes before the last closing tag or at the end
$content = preg_replace('/(\\?>\\s*$)/', $routeContent . "\n$1", $content, 1, $count);
if ($count === 0) {
$content .= $routeContent . "\n";
}
file_put_contents($routeFile, $content);
echo "Routes added to {$routeFile}\n";
// Add resource method to Router class if it doesn't exist
addResourceMethodToRouter();
} else {
echo "Routes for {$baseName} already exist in {$routeFile}\n";
}
} else {
// Create the file
$fileContent = <<<EOT
<?php
use app\core\{Router};
// {$baseName} routes
Router::get('/{$lowerBaseName}', '{$controllerName}', 'index');
Router::get('/{$lowerBaseName}/create', '{$controllerName}', 'create');
Router::post('/{$lowerBaseName}/store', '{$controllerName}', 'store');
Router::get('/{$lowerBaseName}/show', '{$controllerName}', 'show');
Router::get('/{$lowerBaseName}/edit', '{$controllerName}', 'edit');
Router::post('/{$lowerBaseName}/update', '{$controllerName}', 'update');
Router::post('/{$lowerBaseName}/destroy', '{$controllerName}', 'destroy');
EOT;
file_put_contents($routeFile, $fileContent);
echo "Created {$routeFile} with routes for {$baseName}\n";
}
}
/**
* Add resource method to Router class if it doesn't exist
*/
function addResourceMethodToRouter() {
$routerFile = "app/core/Router.php";
if (file_exists($routerFile)) {
$content = file_get_contents($routerFile);
// Check if resource method already exists
if (strpos($content, 'public static function resource') === false) {
// Find the class closure
$pattern = '/class\s+Router\s*\{(.*?)}/s';
preg_match($pattern, $content, $matches);
if (isset($matches[1])) {
// Add resource method to the Router class
$resourceMethod = <<<'EOT'
/**
* Register resource routes for a controller
*
* @param string $name
* @param string $controller
* @return void
*/
public static function resource($name, $controller)
{
self::get('/' . $name, $controller, 'index');
self::get('/' . $name . '/create', $controller, 'create');
self::post('/' . $name . '/store', $controller, 'store');
self::get('/' . $name . '/show', $controller, 'show');
self::get('/' . $name . '/edit', $controller, 'edit');
self::post('/' . $name . '/update', $controller, 'update');
self::post('/' . $name . '/destroy', $controller, 'destroy');
}
EOT;
// Insert the resource method before the last closing brace
$updatedClassContent = preg_replace('/}\s*$/', $resourceMethod . "\n}", $content);
file_put_contents($routerFile, $updatedClassContent);
echo "Added 'resource' method to Router class.\n";
}
}
}
}
function createViewsForController($controllerName, $lowerName)
{
$viewDir = "app/views/{$lowerName}";
if (!is_dir($viewDir)) {
mkdir($viewDir, 0755, true);
}
// Create index view
$indexView = <<<EOT
<div class="container">
<h1><?= \$title ?? '{$controllerName} List' ?></h1>
<div class="actions">
<a href="/{$lowerName}/create" class="btn">Create New {$controllerName}</a>
</div>
<div class="items-list">
<?php if(empty(\$items)): ?>
<p>No {$lowerName}s found.</p>
<?php else: ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach(\$items as \$item): ?>
<tr>
<td><?= \$item['id'] ?></td>
<td><?= \$item['name'] ?? 'Unnamed' ?></td>
<td>
<a href="/{$lowerName}/show?id=<?= \$item['id'] ?>">View</a>
<a href="/{$lowerName}/edit?id=<?= \$item['id'] ?>">Edit</a>
<form action="/{$lowerName}/destroy" method="post" style="display: inline">
<input type="hidden" name="id" value="<?= \$item['id'] ?>">
<button type="submit" onclick="return confirm('Are you sure?')">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
EOT;
file_put_contents("{$viewDir}/index.php", $indexView);
echo "Created view: {$viewDir}/index.php\n";
// Create show view
$showView = <<<EOT
<div class="container">
<h1><?= \$title ?? '{$controllerName} Details' ?></h1>
<div class="actions">
<a href="/{$lowerName}" class="btn">Back to List</a>
<a href="/{$lowerName}/edit?id=<?= \$item['id'] ?>" class="btn">Edit</a>
<form action="/{$lowerName}/destroy" method="post" style="display: inline">
<input type="hidden" name="id" value="<?= \$item['id'] ?>">
<button type="submit" class="btn" onclick="return confirm('Are you sure?')">Delete</button>
</form>
</div>
<div class="details">
<p><strong>ID:</strong> <?= \$item['id'] ?></p>
<p><strong>Name:</strong> <?= \$item['name'] ?? 'Unnamed' ?></p>
<p><strong>Description:</strong> <?= \$item['description'] ?? 'No description' ?></p>
<p><strong>Created:</strong> <?= \$item['created_at'] ?? 'Unknown' ?></p>
</div>
</div>
EOT;
file_put_contents("{$viewDir}/show.php", $showView);
echo "Created view: {$viewDir}/show.php\n";
// Create create view
$createView = <<<EOT
<div class="container">
<h1><?= \$title ?? 'Create New {$controllerName}' ?></h1>
<?php if(isset(\$error)): ?>
<div class="alert alert-danger">
<?= \$error ?>
</div>
<?php endif; ?>
<div class="actions">
<a href="/{$lowerName}" class="btn">Back to List</a>
</div>
<form action="/{$lowerName}/store" method="post">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?= \$data['name'] ?? '' ?>">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description"><?= \$data['description'] ?? '' ?></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn">Create</button>
</div>
</form>
</div>
EOT;
file_put_contents("{$viewDir}/create.php", $createView);
echo "Created view: {$viewDir}/create.php\n";
// Create edit view
$editView = <<<EOT
<div class="container">
<h1><?= \$title ?? 'Edit {$controllerName}' ?></h1>
<?php if(isset(\$error)): ?>
<div class="alert alert-danger">
<?= \$error ?>
</div>
<?php endif; ?>
<div class="actions">
<a href="/{$lowerName}" class="btn">Back to List</a>
<a href="/{$lowerName}/show?id=<?= \$item['id'] ?>" class="btn">Details</a>
</div>
<form action="/{$lowerName}/update" method="post">
<input type="hidden" name="id" value="<?= \$item['id'] ?>">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?= \$data['name'] ?? \$item['name'] ?? '' ?>">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description"><?= \$data['description'] ?? \$item['description'] ?? '' ?></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn">Save</button>
</div>
</form>
</div>
EOT;
file_put_contents("{$viewDir}/edit.php", $editView);
echo "Created view: {$viewDir}/edit.php\n";
}
function createErrorViews()
{
$errorDir = "app/views/errors";
if (!is_dir($errorDir)) {
mkdir($errorDir, 0755, true);
}
$errorViews = [
"400" => "<h1>400 Bad Request</h1><p>The server could not understand the request due to invalid syntax.</p>",
"404" => "<h1>404 Not Found</h1><p>The requested resource could not be found.</p>",
"405" => "<h1>405 Method Not Allowed</h1><p>The request method is not supported for the requested resource.</p>",
"500" => "<h1>500 Internal Server Error</h1><p>The server encountered an unexpected condition that prevented it from fulfilling the request.</p>"
];
foreach ($errorViews as $code => $content) {
if (!file_exists("{$errorDir}/{$code}.php")) {
file_put_contents("{$errorDir}/{$code}.php", $content);
echo "Created error view: {$errorDir}/{$code}.php\n";
}
}
}
// Get the controller name from the terminal argument
$controllerName = $argv[1] ?? '';
if ($controllerName) {
generateController($controllerName);
} else {
echo "Please provide a controller name.\n";
}
You can’t perform that action at this time.
