GenerateTreeCommand.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. foreach ($lines as $line) {
  41. $lineType = null;
  42. // no leading space - root specifier
  43. if($line[0] !== ' ') {
  44. $currentRoot = trim($line);
  45. }
  46. else {
  47. $ls = $this->numLS($line);
  48. $line = trim($line);
  49. $tokens = explode("|", $line);
  50. $line = $tokens[0];
  51. $dbTable = null;
  52. if(count($tokens) === 2) {
  53. $dbTable = $tokens[1];
  54. }
  55. switch($ls) {
  56. case 4: // top level controller OR top level controller action
  57. // top level controller-action
  58. if(strpos($line, "/") !== FALSE) {
  59. if(!empty($currentController)) {
  60. $method = explode("/", $line)[1];
  61. $newMethod = $currentController->addMethod($method, "/" . $line);
  62. // create _SINGLE_ controller if view
  63. if($method === "view") {
  64. $currentSubController = new GenController($currentRoot, $currentController->name . "_SINGLE");
  65. $currentSubController->dbTable = $currentController->dbTable;
  66. $currentSubController->parentRoute = "/" . $line;
  67. $currentSubController->parentControllerName = $currentController->name;
  68. $currentSubController->sub = true;
  69. $newMethod->redirect = "/" . $line . "/SUB_dashboard";
  70. }
  71. }
  72. }
  73. // new top level controller
  74. else if(empty($currentController) || $line !== $currentController->name) {
  75. if(!empty($currentController)) {
  76. $currentController->save();
  77. }
  78. $currentController = new GenController($currentRoot, $line);
  79. $currentController->dbTable = $dbTable;
  80. $currentController->addMethod("index", "/$line");
  81. if(!empty($currentSubController)) {
  82. $currentSubController->save();
  83. }
  84. $currentSubType = '';
  85. $currentSubController = new GenController();
  86. $sideLinks[] = "<li class='nav-item'><a href='/{$currentController->name}' " .
  87. "class='nav-link " .
  88. "{{ (isset(request()->route()->getController()->selfName) && strpos(request()->route()->getController()->selfName, '{$currentController->name}') === 0 ? 'active' : '') }}" . " '>" .
  89. "<i class='nav-icon fa fa-user'></i>" .
  90. "<p>" . $currentController->snakeToTitleCase($currentController->name) . "</p>" .
  91. "</a></li>";
  92. }
  93. break;
  94. case 8: // sub-type declaration
  95. $currentSubType = $line;
  96. break;
  97. case 12: // subs and actions
  98. if($currentSubType === 'ACTIONS') {
  99. $currentSubController->addMethod(
  100. "ACTION_" . $line,
  101. "/ACTION_" . $line
  102. );
  103. }
  104. else if($currentSubType === 'SUB') {
  105. $currentSubController->addMethod(
  106. "SUB_" . $line,
  107. "/SUB_" . $line
  108. );
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. // do any pending saves
  115. if(!empty($currentSubController)) {
  116. $currentSubController->save();
  117. }
  118. if(!empty($currentController)) {
  119. $currentController->save();
  120. }
  121. echo "Saved " . base_path("routes/generated.php") . "\n";
  122. // save side links
  123. file_put_contents(resource_path("views/layouts/generated-links.blade.php"), implode("\n", $sideLinks));
  124. echo "Saved " . resource_path("views/layouts/generated-links.blade.php") . "\n";
  125. }
  126. private function numLS($line) {
  127. $count = 0;
  128. for ($i=0; $i<strlen($line); $i++) {
  129. if($line[$i] !== ' ') break;
  130. $count++;
  131. }
  132. return $count;
  133. }
  134. }
  135. class GenController {
  136. public $root;
  137. public $saved = false;
  138. public $name;
  139. public $methods;
  140. public $parentRoute = "";
  141. public $dbTable = null;
  142. public $sub = false;
  143. public $parentControllerName = '';
  144. public function __construct($root = null, $name = null)
  145. {
  146. $this->root = $root;
  147. $this->name = $name;
  148. $this->methods = [];
  149. }
  150. public function addMethod($method, $route) {
  151. if($this->parentRoute) {
  152. $route = $this->parentRoute . $route;
  153. }
  154. $method = new GenControllerMethod($method, $route);
  155. $this->methods[] = $method;
  156. return $method;
  157. }
  158. public function save() {
  159. if(!$this->saved && !empty($this->root) && !empty($this->name)) {
  160. $this->saveController();
  161. $this->saveRoutes();
  162. $this->saved = true;
  163. // $this->log();
  164. }
  165. }
  166. public function saveController() {
  167. $text = file_get_contents(base_path('generatecv/tree-templates/controller.template.php'));
  168. $code = [];
  169. foreach ($this->methods as $method) {
  170. $code[] = "";
  171. $code[] = "\t// GET {$method->route}";
  172. $code[] = "\t" . 'public function ' . $method->name . '(Request $request' . ($method->hasUID ? ', $uid' : '') . ') {';
  173. if($method->redirect) {
  174. $target = str_replace('{uid}', '$uid', $method->redirect);
  175. $code[] = "\t\t" . 'return redirect("' . $target . '");';
  176. }
  177. else {
  178. if($method->hasUID) {
  179. $code[] = "\t\t\$record = DB::table('{$this->dbTable}')->where('uid', \$uid)->first();";
  180. $code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', compact('record'));";
  181. }
  182. else {
  183. $code[] = "\t\t\$records = DB::table('{$this->dbTable}')->get();";
  184. $code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', compact('records'));";
  185. }
  186. }
  187. $this->saveView($this, $method);
  188. $code[] = "\t}";
  189. }
  190. $text = str_replace("_NAME_", "{$this->name}_Controller", $text);
  191. $text = str_replace("// __METHODS__", implode("\n", $code), $text);
  192. file_put_contents(app_path("Http/Controllers/{$this->name}_Controller.php"), $text);
  193. echo "Generated " . app_path("Http/Controllers/{$this->name}_Controller.php") . "\n";
  194. }
  195. public function saveView(GenController $controller, GenControllerMethod $method) {
  196. /*
  197. $x = DB::getSchemaBuilder()->getColumnListing('client');
  198. print_r($x);
  199. */
  200. if($controller->sub) {
  201. $this->saveSubView($controller, $method);
  202. }
  203. else {
  204. if($method->name === 'view') {
  205. $this->saveShowView($controller, $method);
  206. }
  207. else if($method->name === 'index') {
  208. $this->saveIndexView($controller, $method);
  209. }
  210. else if($method->name === 'add_new') {
  211. $this->saveAddNewView($controller, $method);
  212. }
  213. }
  214. }
  215. public function saveIndexView(GenController $controller, GenControllerMethod $method)
  216. {
  217. $text = file_get_contents(base_path('generatecv/tree-templates/index.template.blade.php'));
  218. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  219. $text = str_replace("_ADD_NEW_ROUTE_", $controller->name . '-add_new', $text);
  220. $columns = DB::getSchemaBuilder()->getColumnListing($controller->dbTable);
  221. $ths = [];
  222. $tds = [];
  223. foreach ($columns as $column) {
  224. $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
  225. $tds[] = "<td>" .
  226. ($column === 'uid' ? '<a href="/' . $controller->name . '/view/<?= $record->uid ?>">' : '') .
  227. "<?= \$record->$column ?>" .
  228. ($column === 'uid' ? '</a>' : '') .
  229. "</td>";
  230. }
  231. $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
  232. $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", implode("\n", $tds), $text);
  233. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  234. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  235. }
  236. public function saveShowView(GenController $controller, GenControllerMethod $method)
  237. {
  238. // delete sub links and action links
  239. if(file_exists(resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php"))) {
  240. unlink(resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php"));
  241. }
  242. if(file_exists(resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php"))) {
  243. unlink(resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php"));
  244. }
  245. $text = file_get_contents(base_path('generatecv/tree-templates/show.template.blade.php'));
  246. $text = str_replace("_NAME_", $this->snakeToTitleCase($controller->name), $text);
  247. $text = str_replace("_UID_", '<?= $record->uid ?>', $text);
  248. $text = str_replace("_INDEX_ROUTE_", $controller->name . '-index', $text);
  249. $text = str_replace("_SUB_LINKS_VIEW_", "{$controller->root}/{$controller->name}/subs", $text);
  250. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  251. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  252. }
  253. public function saveSubView(GenController $controller, GenControllerMethod $method)
  254. {
  255. // generate sub links if not existing
  256. $subLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/subs.blade.php");
  257. //if(!file_exists($subLinksView)) {
  258. $subLinks = [];
  259. foreach ($controller->methods as $meth) {
  260. if(strpos($meth->name, "SUB_") !== 0) continue;
  261. $display = $this->snakeToTitleCase(substr($meth->name, 4));
  262. $subLinks[] = "<a " .
  263. "href='/{$controller->parentControllerName}/view/<?= \$record->uid ?>/{$meth->name}' " .
  264. "class='d-block p-3 py-2 border-bottom " .
  265. "{{ request()->route()->getActionMethod() === '{$meth->name}' ? 'bg-secondary text-white' : '' }}"
  266. . "'>$display</a>";
  267. }
  268. file_put_contents($subLinksView, implode("\n", $subLinks));
  269. // echo "Generated " . $subLinksView . "\n";
  270. //}
  271. // generate action links if not existing
  272. $actionLinksView = resource_path("views/{$controller->root}/{$controller->parentControllerName}/actions.blade.php");
  273. //if(!file_exists($actionLinksView)) {
  274. $actionLinks = [];
  275. foreach ($controller->methods as $meth) {
  276. if(strpos($meth->name, "ACTION_") !== 0) continue;
  277. $display = $this->camelToTitleCase(substr($meth->name, 7));
  278. $actionLinks[] = "<a " .
  279. "href='/{$controller->parentControllerName}/view/<?= \$record->uid ?>/{$meth->name}' " .
  280. "class='d-block btn btn-sm btn-default mb-3'>$display</a>";
  281. }
  282. file_put_contents($actionLinksView, implode("\n", $actionLinks));
  283. // echo "Generated " . $actionLinksView . "\n";
  284. //}
  285. $text = file_get_contents(base_path('generatecv/tree-templates/sub.template.blade.php'));
  286. $text = str_replace("_LAYOUT_", "{$controller->root}.{$controller->parentControllerName}.view", $text);
  287. $text = $this->generateSubContent($controller, $method, $text);
  288. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  289. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  290. }
  291. public function generateSubContent(GenController $controller, GenControllerMethod $method, $text) {
  292. if($method->name === 'SUB_dashboard') {
  293. $html = file_get_contents(base_path('generatecv/tree-templates/dashboard.template.blade.php'));
  294. $text = str_replace("_SUB_VIEW_", $html, $text);
  295. $text = str_replace("_ACTION_LINKS_VIEW_", "{$controller->root}/{$controller->parentControllerName}/actions", $text);
  296. }
  297. else {
  298. $text = str_replace("_SUB_VIEW_", "Content from<br><b>{$controller->root}/{$controller->name}/{$method->name}.blade.php</b>", $text);
  299. }
  300. return $text;
  301. }
  302. public function saveAddNewView(GenController $controller, GenControllerMethod $method)
  303. {
  304. }
  305. public function saveRoutes() {
  306. $lines = ["// --- {$this->root}: {$this->name} --- //"];
  307. foreach ($this->methods as $method) {
  308. // FORMAT:
  309. // Route::get('/foo/bar/{uid}', 'FooController@bar')->name('foo-action');
  310. $lines[] = "Route::get('{$method->route}', '{$this->name}_Controller@{$method->name}')->name('{$this->name}-{$method->name}');";
  311. }
  312. $lines[] = '';
  313. $lines[] = '';
  314. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
  315. }
  316. public function log() {
  317. $this->w('');
  318. $this->w("Controller: app/Http/Controllers/{$this->name}_Controller");
  319. $this->w("Table: {$this->dbTable}");
  320. $this->w('---------------------------------------------');
  321. foreach ($this->methods as $method) {
  322. $this->w('Rout: ' . $method->route, 1);
  323. $this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
  324. if(!$method->redirect) {
  325. $this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
  326. }
  327. else {
  328. $this->w('Redi: ' . $method->redirect, 1);
  329. }
  330. $this->w('---------------------------------------------');
  331. }
  332. }
  333. public function w($line, $level = -1) {
  334. for($i=0; $i<$level; $i++) echo "\t";
  335. echo "$line\n";
  336. }
  337. public function snakeToTitleCase($text) {
  338. return ucwords(str_replace("_", " ", $text));
  339. }
  340. public function camelToTitleCase($text) {
  341. $text = preg_replace("/([A-Z])/", " $1", $text);
  342. return ucwords($text);
  343. }
  344. private function file_force_contents($dir, $contents){
  345. $dir = str_replace("\\", "/", $dir);
  346. $dir = str_replace( '//', '/', $dir);
  347. $parts = explode('/', $dir);
  348. $file = array_pop($parts);
  349. $dir = '';
  350. foreach($parts as $part) {
  351. if($part[strlen($part) - 1] !== ':') {
  352. if(!is_dir($dir .= "/$part")) mkdir($dir);
  353. }
  354. }
  355. file_put_contents("$dir/$file", $contents);
  356. }
  357. }
  358. class GenControllerMethod {
  359. public $name;
  360. public $route;
  361. public $hasUID = false;
  362. public $redirect = false;
  363. public function __construct($name, $route)
  364. {
  365. $this->name = $name;
  366. $this->route = $route;
  367. if(strpos($this->route, "{uid}") !== FALSE) {
  368. $this->hasUID = true;
  369. }
  370. }
  371. }