GenerateTreeCommand.php 30 KB

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