123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- class GenerateCCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'generatec
- {path: /path/to/controller.json}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- //
- global $argv;
- $controllers = json_decode(file_get_contents($argv[2]));
- // Route::get('/my-payments-schedule', 'MyPaymentScheduleController@index');
- // Route::get('/my-teams', 'MyTeamsController@index');
- // Route::get('/my-clients', 'MyClientsController@index');
- // Route::get('/notes', 'NotesController@index');
- // Route::get('/erx', 'ERXController@index');
- // Route::get('/action-items', 'ActionItemsController@index');
- // Route::get('/care-months', 'CareMonthsController@index');
- // Route::get('/bills', 'BillsController@index');
- // Route::get('/transactions', 'TransactionsController@index');
- // Route::get('/med-profile-lines', 'MedProfileLinesController@index');
- // Route::get('/mcp-updates', 'McpUpdatesController@index');
- // Route::get('/ally-updates', 'AllyUpdatesController@index');
- // Route::get('/audit-log', 'AuditLogController@index');
- $routesText = ["/* __SCAFFOLD_ROUTES__ */"];
- $linksText = ["<!-- __SCAFFOLD_LINKS__ -->"];
- foreach ($controllers as $name => $object) {
- if(isset($object->todo)) continue;
- // routes
- $routesText[] = "/* SCAF */// $object->slug CRUD";
- $routesText[] = "/* SCAF */Route::get ('/$object->slug', '{$name}Controller@index');";
- $routesText[] = "/* SCAF */Route::get ('/$object->slug/:id', '{$name}Controller@show');";
- $routesText[] = "/* SCAF */Route::get ('/$object->slug/create', '{$name}Controller@create');";
- $routesText[] = "/* SCAF */Route::get ('/$object->slug/update/:id', '{$name}Controller@update');";
- // left nav links
- $linksText[] =
- '<!-- SCAF --><li class="nav-item"><a href="/' . $object->slug . '" class="nav-link"><i class="nav-icon ' . $object->icon . '"></i><p>' . $object->text . '</p></a></li>';
- // controller
- $template =
- '<?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\_MODEL_;
- class _NAME_Controller extends Controller
- {
- public function index(Request $request)
- {
- $rows = _MODEL_::all();
- return view(\'_SLUG_.index\', compact(\'rows\'));
- }
- public function show(Request $request, $uid)
- {
- $row = _MODEL_::where(\'uid\', $uid)->first();
- return view(\'_SLUG_.show\', compact(\'row\'));
- }
- public function create(Request $request)
- {
- return view(\'_SLUG_.create\');
- }
- public function update(Request $request, $uid)
- {
- $row = _MODEL_::where(\'uid\', $uid)->first();
- return view(\'_SLUG_.update\', compact(\'row\'));
- }
- }
- ';
- // write controller
- $text = str_replace("_NAME_", $name, $template);
- $text = str_replace("_MODEL_", $object->model, $text);
- $text = str_replace("_SLUG_", $object->slug, $text);
- if(!file_exists(app_path("Http/Controllers/{$name}Controller.php"))) {
- file_put_contents(app_path("Http/Controllers/{$name}Controller.php"), $text);
- echo "Generated " . app_path("Http/Controllers/{$name}Controller.php") . "\n";
- }
- }
- // write routes
- $text = [];
- $file = fopen(base_path("routes/web.php"), "r");
- while (!feof($file)) {
- $line = rtrim(fgets($file));
- if(strpos($line, "/* SCAF */") === FALSE) {
- $text[] = $line;
- }
- }
- $text = implode("\n", $text);
- fclose($file);
- $text = str_replace("/* __SCAFFOLD_ROUTES__ */", implode("\n", $routesText), $text);
- file_put_contents(base_path("routes/web.php"), $text);
- echo "Updated " . base_path("routes/web.php") . "\n";
- // write left nav links
- $text = [];
- $file = fopen(resource_path("views/layouts/pro-logged-in.blade.php"), "r");
- while (!feof($file)) {
- $line = rtrim(fgets($file));
- if(strpos($line, "<!-- SCAF -->") === FALSE) {
- $text[] = $line;
- }
- }
- $text = implode("\n", $text);
- fclose($file);
- $text = str_replace("<!-- __SCAFFOLD_LINKS__ -->", implode("\n", $linksText), $text);
- file_put_contents(resource_path("views/layouts/pro-logged-in.blade.php"), $text);
- echo "Updated " . resource_path("views/layouts/pro-logged-in.blade.php") . "\n";
- }
- }
|