GenerateTreeCommand.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. class GenerateTreeCommand extends Command
  6. {
  7. private $routesFile = null;
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'generatetree
  14. {path: /path/to/tree.txt}';
  15. /**
  16. * Execute the console command.
  17. *
  18. * @return mixed
  19. */
  20. public function handle()
  21. {
  22. $lines = ['<?php', '', 'use Illuminate\Support\Facades\Route;', '', ''];
  23. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines));
  24. $sideLinks = [];
  25. global $argv;
  26. $file = fopen($argv[2], "r");
  27. $lines = [];
  28. while (!feof($file)) {
  29. $line = rtrim(fgets($file));
  30. if(trim($line) !== '') {
  31. $lines[] = $line;
  32. }
  33. }
  34. fclose($file);
  35. $currentRoot = "";
  36. $currentController = new GenController();
  37. $currentSubController = new GenController();
  38. $currentSubType = "";
  39. $currentView = "";
  40. $currentMethod = null;
  41. foreach ($lines as $line) {
  42. // skip comments
  43. if(trim($line)[0] === '#') continue;
  44. $lineType = null;
  45. // no leading space - root specifier
  46. if($line[0] !== ' ') {
  47. $currentRoot = strtolower(trim($line));
  48. }
  49. else {
  50. $ls = $this->numLS($line);
  51. $line = trim($line);
  52. $tokens = explode("|", $line);
  53. $line = $tokens[0];
  54. $dbTable = null;
  55. if(count($tokens) >= 2) {
  56. $dbTable = $tokens[1];
  57. }
  58. $hasAdd = in_array("add", $tokens);
  59. $hasView = in_array("view", $tokens);
  60. switch($ls) {
  61. case 4: // top level controller OR top level controller action
  62. // top level controller-action
  63. if(strpos($line, "/") !== FALSE) {
  64. if(!empty($currentController)) {
  65. $method = explode("/", $line)[1];
  66. $newMethod = $currentController->addMethod($method, "/" . $line);
  67. // create _SINGLE_ controller if view
  68. if($method === "view") {
  69. $currentSubController = new GenController($currentRoot, $currentController->name . "_SINGLE");
  70. $currentSubController->dbTable = $currentController->dbTable;
  71. $currentSubController->parentRoute = "/" . $line;
  72. $currentSubController->parentControllerName = $currentController->name;
  73. $currentSubController->sub = true;
  74. $newMethod->redirect = "/" . $line . "/SUB_dashboard";
  75. }
  76. $currentMethod = $newMethod;
  77. }
  78. }
  79. // new top level controller
  80. else if(empty($currentController) || $line !== $currentController->name) {
  81. if(!empty($currentController)) {
  82. $currentController->save();
  83. }
  84. $currentController = new GenController($currentRoot, $line);
  85. $currentController->dbTable = $dbTable;
  86. $currentController->hasAdd = $hasAdd;
  87. $currentController->hasView = $hasView;
  88. $currentController->addMethod("index", "/$line");
  89. if(!empty($currentSubController)) {
  90. $currentSubController->save();
  91. }
  92. $currentSubType = '';
  93. $currentSubController = new GenController();
  94. $sideLinks[] = "<li class='nav-item'><a href='/{$currentController->name}' " .
  95. "class='nav-link " .
  96. "{{ (isset(request()->route()->getController()->selfName) && strpos(request()->route()->getController()->selfName, '{$currentController->name}') === 0 ? 'active' : '') }}" . " '>" .
  97. "<i class='nav-icon fa fa-user'></i>" .
  98. "<p>" . $currentController->snakeToTitleCase($currentController->name) . "</p>" .
  99. "</a></li>";
  100. }
  101. break;
  102. case 8: // sub-type declaration
  103. if($line === 'ACTIONS' || $line === 'SUB') {
  104. $currentSubType = $line;
  105. }
  106. else if (!empty($currentMethod) && $currentMethod->name === 'add_new') { // this is a field in add_new
  107. $currentMethod->data[] = $line;
  108. }
  109. break;
  110. case 12: // ACTIONS | SUB | add_new fields
  111. if($currentSubType === 'ACTIONS') {
  112. $currentMethod = $currentSubController->addMethod(
  113. "ACTION_" . $line,
  114. "/ACTION_" . $line
  115. );
  116. $currentMethod->type = 'action';
  117. $currentMethod->data = [];
  118. }
  119. else if($currentSubType === 'SUB') {
  120. $currentMethod = $currentSubController->addMethod(
  121. "SUB_" . $line,
  122. "/SUB_" . $line
  123. );
  124. $currentMethod->type = 'sub';
  125. $currentMethod->data = [];
  126. }
  127. break;
  128. case 16: // data for actions and subs
  129. if(!empty($currentMethod)) {
  130. $currentMethod->data[] = $line;
  131. }
  132. break;
  133. }
  134. }
  135. }
  136. // do any pending saves
  137. if(!empty($currentSubController)) {
  138. $currentSubController->save();
  139. }
  140. if(!empty($currentController)) {
  141. $currentController->save();
  142. }
  143. echo "Saved " . base_path("routes/generated.php") . "\n";
  144. // save side links
  145. file_put_contents(resource_path("views/layouts/generated-links.blade.php"), implode("\n", $sideLinks));
  146. echo "Saved " . resource_path("views/layouts/generated-links.blade.php") . "\n";
  147. }
  148. private function numLS($line) {
  149. $count = 0;
  150. for ($i=0; $i<strlen($line); $i++) {
  151. if($line[$i] !== ' ') break;
  152. $count++;
  153. }
  154. return $count;
  155. }
  156. }
  157. class GenController {
  158. public $root;
  159. public $saved = false;
  160. public $name;
  161. public $methods;
  162. public $parentRoute = "";
  163. public $dbTable = null;
  164. public $hasAdd = false;
  165. public $hasView = false;
  166. public $sub = false;
  167. public $parentControllerName = '';
  168. public $subLinksSaved = false;
  169. public $actionLinksSaved = false;
  170. public function __construct($root = null, $name = null)
  171. {
  172. $this->root = $root;
  173. $this->name = $name;
  174. $this->methods = [];
  175. }
  176. public function addMethod($method, $route) {
  177. if($this->parentRoute) {
  178. $route = $this->parentRoute . $route;
  179. }
  180. $method = new GenControllerMethod($method, $route);
  181. $this->methods[] = $method;
  182. return $method;
  183. }
  184. public function save() {
  185. if(!$this->saved && !empty($this->root) && !empty($this->name)) {
  186. $this->saveController();
  187. $this->saveRoutes();
  188. $this->saved = true;
  189. // $this->log();
  190. }
  191. }
  192. public function saveController() {
  193. $text = file_get_contents(base_path('generatecv/tree-templates/controller.template.php'));
  194. $code = [];
  195. foreach ($this->methods as $method) {
  196. $code[] = "";
  197. $code[] = "\t// GET {$method->route}";
  198. $code[] = "\t" . 'public function ' . $method->name . '(Request $request' . ($method->hasUID ? ', $uid' : '') . ') {';
  199. if($method->redirect) {
  200. $target = str_replace('{uid}', '$uid', $method->redirect);
  201. $code[] = "\t\t" . 'return redirect("' . $target . '");';
  202. }
  203. else {
  204. if($method->hasUID) {
  205. $code[] = "\t\t\$record = DB::table('{$this->dbTable}')->where('uid', \$uid)->first();";
  206. $input = ["'record'"];
  207. // if sub-index controller, load subRecords
  208. if($method->type === 'sub' && count($method->data)) {
  209. $dbParts = explode("=", $method->data[0]);
  210. $localField = $dbParts[0];
  211. $dbParts = explode(".", $dbParts[1]);
  212. $foreignTable = $dbParts[0];
  213. $foreignField = $dbParts[1];
  214. $code[] = "\t\t\$subRecords = DB::table('$foreignTable')->where('$foreignField', \$record->$localField)->get();";
  215. $input[] = "'subRecords'";
  216. }
  217. $code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', " .
  218. "compact(" . implode(", ", $input) . "));";
  219. }
  220. else {
  221. $code[] = "\t\t\$records = DB::table('{$this->dbTable}')->get();";
  222. $code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', " .
  223. "compact('records'));";
  224. }
  225. }
  226. $this->saveView($this, $method);
  227. $code[] = "\t}";
  228. }
  229. $text = str_replace("_NAME_", "{$this->name}_Controller", $text);
  230. $text = str_replace("// __METHODS__", implode("\n", $code), $text);
  231. file_put_contents(app_path("Http/Controllers/{$this->name}_Controller.php"), $text);
  232. echo "Generated " . app_path("Http/Controllers/{$this->name}_Controller.php") . "\n";
  233. }
  234. public function saveView(GenController $controller, GenControllerMethod $method) {
  235. if($controller->sub) {
  236. $controller->saveSubLinks($controller, $method);
  237. $controller->saveActionLinks($controller, $method);
  238. if($method->type === 'action') {
  239. $this->saveSubActionView($controller, $method);
  240. }
  241. else if($method->type === 'sub' && count($method->data)) {
  242. $this->saveSubIndexView($controller, $method);
  243. }
  244. else {
  245. $this->saveSubDefaultView($controller, $method);
  246. }
  247. }
  248. else {
  249. if($method->name === 'view' && $controller->hasView) {
  250. $this->saveShowView($controller, $method);
  251. }
  252. else if($method->name === 'index') {
  253. $this->saveIndexView($controller, $method);
  254. }
  255. else if($method->name === 'add_new' && $controller->hasAdd) {
  256. $this->saveAddNewView($controller, $method);
  257. }
  258. }
  259. }
  260. public function saveIndexView(GenController $controller, GenControllerMethod $method)
  261. {
  262. $text = file_get_contents(base_path('generatecv/tree-templates/index.template.blade.php'));
  263. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  264. if($controller->hasAdd) {
  265. $addLink = "<a class='btn btn-primary btn-sm' href='/{$controller->name}/add_new'>" .
  266. "<i class='fa fa-plus-circle' aria-hidden='true'></i> Add New</a>";
  267. $text = str_replace("<!-- _ADD_NEW_LINK_ -->", $addLink, $text);
  268. }
  269. $columns = DB::getSchemaBuilder()->getColumnListing($controller->dbTable);
  270. $ths = [];
  271. $tds = [];
  272. foreach ($columns as $column) {
  273. $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
  274. $tds[] = "<td>" .
  275. ($controller->hasView && $column === 'uid' ? '<a href="/' . $controller->name . '/view/<?= $record->uid ?>">' : '') .
  276. "<?= \$record->$column ?>" .
  277. ($controller->hasView && $column === 'uid' ? '</a>' : '') .
  278. "</td>";
  279. }
  280. $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
  281. $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", implode("\n", $tds), $text);
  282. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  283. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  284. }
  285. public function saveShowView(GenController $controller, GenControllerMethod $method)
  286. {
  287. // delete sub links and action links
  288. if(file_exists(resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php"))) {
  289. unlink(resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php"));
  290. }
  291. if(file_exists(resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php"))) {
  292. unlink(resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php"));
  293. }
  294. $text = file_get_contents(base_path('generatecv/tree-templates/show.template.blade.php'));
  295. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  296. $text = str_replace("_UID_", '<?= $record->uid ?>', $text);
  297. $text = str_replace("_INDEX_ROUTE_", $controller->name . '-index', $text);
  298. $text = str_replace("_SUB_LINKS_VIEW_", "{$controller->root}/{$controller->name}/subs", $text);
  299. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  300. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  301. }
  302. public function saveSubLinks(GenController $controller, GenControllerMethod $method)
  303. {
  304. if ($controller->subLinksSaved) return;
  305. $subLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php");
  306. $subLinks = [];
  307. foreach ($controller->methods as $meth) {
  308. if (strpos($meth->name, "SUB_") !== 0) continue;
  309. $display = $this->snakeToTitleCase(substr($meth->name, 4));
  310. $subLinks[] = "<a " .
  311. "href='/{$controller->parentControllerName}/view/<?= \$record->uid ?>/{$meth->name}' " .
  312. "class='d-block p-3 py-2 border-bottom " .
  313. "{{ request()->route()->getActionMethod() === '{$meth->name}' ? 'bg-secondary text-white' : '' }}" .
  314. ($meth->name === 'SUB_dashboard' ? "{{ strpos(request()->route()->getActionMethod(), 'ACTION_') === 0 ? 'bg-secondary text-white' : '' }}" : "")
  315. . "'>$display</a>";
  316. }
  317. file_put_contents($subLinksView, implode("\n", $subLinks));
  318. echo "Generated " . $subLinksView . "\n";
  319. $controller->subLinksSaved = true;
  320. }
  321. public function saveActionLinks(GenController $controller, GenControllerMethod $method)
  322. {
  323. if ($controller->actionLinksSaved) return;
  324. $actionLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php");
  325. $actionLinks = [];
  326. foreach ($controller->methods as $meth) {
  327. if (strpos($meth->name, "ACTION_") !== 0) continue;
  328. $display = $this->camelToTitleCase(substr($meth->name, 7));
  329. $actionLinks[] = "<a " .
  330. "href='/{$controller->parentControllerName}/view/<?= \$record->uid ?>/{$meth->name}' " .
  331. "class='d-block btn btn-sm btn-default mb-3'>$display</a>";
  332. }
  333. file_put_contents($actionLinksView, implode("\n", $actionLinks));
  334. echo "Generated " . $actionLinksView . "\n";
  335. $controller->actionLinksSaved = true;
  336. }
  337. public function saveSubDefaultView(GenController $controller, GenControllerMethod $method)
  338. {
  339. $text = file_get_contents(base_path('generatecv/tree-templates/sub.template.blade.php'));
  340. $text = str_replace("_LAYOUT_", "{$controller->root}.{$controller->parentControllerName}.view", $text);
  341. $text = $this->generateSubContent($controller, $method, $text);
  342. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  343. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  344. }
  345. public function saveSubActionView(GenController $controller, GenControllerMethod $method) {
  346. $text = file_get_contents(base_path('generatecv/tree-templates/sub-action.template.blade.php'));
  347. $text = str_replace("_LAYOUT_", "{$controller->root}.{$controller->parentControllerName}.view", $text);
  348. $text = str_replace("_NAME_", $this->camelToTitleCase($this->snakeToTitleCase($method->name)), $text);
  349. $text = str_replace("_BACK_ROUTE_", "{$controller->parentControllerName}-view", $text);
  350. $text = str_replace("_API_", "/api/{$controller->dbTable}/" . substr($method->name, 7), $text);
  351. $text = str_replace("_RETURN_ROUTE_", "{$controller->name}-{$method->name}", $text);
  352. $fields = [];
  353. if(count($method->data)) {
  354. foreach ($method->data as $field) {
  355. $fields[] = $this->generateFormField($field);
  356. }
  357. }
  358. $text = str_replace("<!-- _SCAFFOLD_FIELDS_ -->", implode("\n", $fields), $text);
  359. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  360. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  361. }
  362. public function saveSubIndexView(GenController $controller, GenControllerMethod $method) {
  363. $text = file_get_contents(base_path('generatecv/tree-templates/sub-index.template.blade.php'));
  364. $text = str_replace("_LAYOUT_", "{$controller->root}.{$controller->parentControllerName}.view", $text);
  365. $text = str_replace("_NAME_", $this->camelToTitleCase($this->snakeToTitleCase($method->name)), $text);
  366. /*if($controller->hasAdd) {
  367. $addLink = "<a class='btn btn-primary btn-sm' href='/{$controller->name}/add_new'>" .
  368. "<i class='fa fa-plus-circle' aria-hidden='true'></i> Add New</a>";
  369. $text = str_replace("<!-- _ADD_NEW_LINK_ -->", $addLink, $text);
  370. }*/
  371. $dbParts = explode("=", $method->data[0]);
  372. $dbParts = explode(".", $dbParts[1]);
  373. $table = $dbParts[0];
  374. $columns = DB::getSchemaBuilder()->getColumnListing($table);
  375. $ths = [];
  376. $tds = [];
  377. foreach ($columns as $column) {
  378. $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
  379. $tds[] = "<td><?= \$subRecord->$column ?></td>";
  380. }
  381. $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
  382. $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", implode("\n", $tds), $text);
  383. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  384. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  385. }
  386. public function generateSubContent(GenController $controller, GenControllerMethod $method, $text) {
  387. if($method->name === 'SUB_dashboard') {
  388. $html = file_get_contents(base_path('generatecv/tree-templates/dashboard.template.blade.php'));
  389. $text = str_replace("_SUB_VIEW_", $html, $text);
  390. $text = str_replace("_ACTION_LINKS_VIEW_", "{$controller->root}/{$controller->parentControllerName}/actions", $text);
  391. }
  392. else {
  393. $text = str_replace("_SUB_VIEW_",
  394. "<h4 class='py-3 border-bottom'>" .
  395. $this->camelToTitleCase($this->snakeToTitleCase($method->name)) . "</h4>" .
  396. "Controller: <b>{$controller->name}</b><br>" .
  397. "Action: <b>{$method->name}()</b><br>" .
  398. "View: <b>{$controller->root}/{$controller->name}/{$method->name}.blade.php</b><br>",
  399. $text);
  400. }
  401. return $text;
  402. }
  403. public function saveAddNewView(GenController $controller, GenControllerMethod $method)
  404. {
  405. $text = file_get_contents(base_path('generatecv/tree-templates/add_new.template.blade.php'));
  406. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  407. $text = str_replace("_API_", "/api/{$controller->dbTable}/create", $text);
  408. $text = str_replace("_BACK_ROUTE_", "{$controller->name}-index", $text);
  409. $text = str_replace("_RETURN_ROUTE_", "{$controller->name}-add_new", $text);
  410. $columns = $method->data;
  411. $fields = [];
  412. foreach ($columns as $column) {
  413. $fields[] = $this->generateFormField($column);
  414. }
  415. $text = str_replace("<!-- _SCAFFOLD_FIELDS_ -->", implode("\n", $fields), $text);
  416. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  417. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  418. }
  419. public function saveRoutes() {
  420. $lines = ["// --- {$this->root}: {$this->name} --- //"];
  421. foreach ($this->methods as $method) {
  422. // FORMAT:
  423. // Route::get('/foo/bar/{uid}', 'FooController@bar')->name('foo-action');
  424. $lines[] = "Route::get('{$method->route}', '{$this->name}_Controller@{$method->name}')->name('{$this->name}-{$method->name}');";
  425. }
  426. $lines[] = '';
  427. $lines[] = '';
  428. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
  429. }
  430. public function log() {
  431. $this->w('');
  432. $this->w("Controller: app/Http/Controllers/{$this->name}_Controller");
  433. $this->w("Table: {$this->dbTable}");
  434. $this->w('---------------------------------------------');
  435. foreach ($this->methods as $method) {
  436. $this->w('Rout: ' . $method->route, 1);
  437. $this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
  438. if(!empty($method->data)) $this->w('Data: ' . implode(", ", $method->data), 1);
  439. if(!$method->redirect) {
  440. $this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
  441. }
  442. else {
  443. $this->w('Redi: ' . $method->redirect, 1);
  444. }
  445. $this->w('---------------------------------------------');
  446. }
  447. }
  448. public function w($line, $level = -1) {
  449. for($i=0; $i<$level; $i++) echo "\t";
  450. echo "$line\n";
  451. }
  452. public function snakeToTitleCase($text) {
  453. $text = preg_replace("/^(SUB|ACTION)_/", "", $text);
  454. return ucwords(str_replace("_", " ", $text));
  455. }
  456. public function camelToTitleCase($text) {
  457. $text = preg_replace("/^(SUB|ACTION)_/", "", $text);
  458. $text = preg_replace("/([a-z])([A-Z0-9])/", "$1 $2", $text);
  459. return ucwords($text);
  460. }
  461. public function snakeToCamelCase($text) {
  462. $text = ucwords(str_replace("_", " ", $text));
  463. $text = str_replace(" ", "", $text);
  464. $text[0] = strtolower($text[0]);
  465. return $text;
  466. }
  467. private function file_force_contents($dir, $contents){
  468. $dir = str_replace("\\", "/", $dir);
  469. $dir = str_replace( '//', '/', $dir);
  470. $parts = explode('/', $dir);
  471. $file = array_pop($parts);
  472. $dir = '';
  473. foreach($parts as $part) {
  474. if($part[strlen($part) - 1] !== ':') {
  475. if(!is_dir($dir .= "/$part")) mkdir($dir);
  476. }
  477. }
  478. file_put_contents("$dir/$file", $contents);
  479. }
  480. private function generateFormField($line) {
  481. $tokens = explode(":", $line);
  482. $name = $tokens[0];
  483. $display = $name;
  484. $dotPos = strpos($name, ".");
  485. if($dotPos !== FALSE) {
  486. $display = substr($name, $dotPos + 1);
  487. }
  488. $type = "text";
  489. $options = [];
  490. $loadingCode = '';
  491. if(count($tokens) > 1) {
  492. $type = $tokens[1];
  493. switch ($type) {
  494. case "select":
  495. $options = explode(",", $tokens[2]);
  496. break;
  497. case "record":
  498. $options['table'] = $tokens[2];
  499. $parts = explode(",", $tokens[3]);
  500. $options['valueField'] = $parts[0];
  501. $options['displayField'] = $parts[1];
  502. break;
  503. }
  504. }
  505. $code[] = "<div class='form-group mb-3'>";
  506. $code[] = "<label class='control-label'>{$this->camelToTitleCase($this->snakeToTitleCase($display))}</label>";
  507. switch ($type) {
  508. case "select":
  509. $code[] = "<select class='form-control' name='$name'>";
  510. $code[] = "<option value=''>-- Select --</option>";
  511. foreach ($options as $o) {
  512. $code[] = "<option value='$o'>$o</option>";
  513. }
  514. $code[] = "</select>";
  515. break;
  516. case "record":
  517. $code[] = "<select class='form-control' name='$name'>";
  518. $code[] = "<option value=''>-- Select --</option>";
  519. $code[] = "<?php \$dbOptions = \Illuminate\Support\Facades\DB::table('{$options['table']}')->get(); ?>";
  520. $code[] = "<?php foreach(\$dbOptions as \$o): ?>";
  521. $code[] = "<option value='<?= \$o->{$options['valueField']} ?>'><?= \$o->{$options['displayField']} ?> (<?= \$o->{$options['valueField']} ?>)</option>";
  522. $code[] = "<?php endforeach; ?>";
  523. $code[] = "</select>";
  524. break;
  525. default:
  526. $code[] = "<input class='form-control' type='$type' name='$name'>";
  527. }
  528. $code[] = "</div>";
  529. return implode("\n", $code);
  530. }
  531. }
  532. class GenControllerMethod {
  533. public $name;
  534. public $route;
  535. public $hasUID = false;
  536. public $redirect = false;
  537. public $type = '';
  538. public $data = [];
  539. public function __construct($name, $route)
  540. {
  541. $this->name = $name;
  542. $this->route = $route;
  543. if(strpos($this->route, "{uid}") !== FALSE) {
  544. $this->hasUID = true;
  545. }
  546. }
  547. }