GenerateTreeCommand.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. global $argv;
  25. $file = fopen($argv[2], "r");
  26. $lines = [];
  27. while (!feof($file)) {
  28. $line = rtrim(fgets($file));
  29. if(trim($line) !== '') {
  30. $lines[] = $line;
  31. }
  32. }
  33. fclose($file);
  34. $currentRoot = "";
  35. $currentController = new GenController();
  36. $currentSubController = new GenController();
  37. $currentSubType = "";
  38. $currentView = "";
  39. foreach ($lines as $line) {
  40. $lineType = null;
  41. // no leading space - root specifier
  42. if($line[0] !== ' ') {
  43. $currentRoot = trim($line);
  44. }
  45. else {
  46. $ls = $this->numLS($line);
  47. $line = trim($line);
  48. $tokens = explode("|", $line);
  49. $line = $tokens[0];
  50. $dbTable = null;
  51. if(count($tokens) === 2) {
  52. $dbTable = $tokens[1];
  53. }
  54. switch($ls) {
  55. case 4: // top level controller OR top level controller action
  56. // top level controller-action
  57. if(strpos($line, "/") !== FALSE) {
  58. if(!empty($currentController)) {
  59. $method = explode("/", $line)[1];
  60. $newMethod = $currentController->addMethod($method, "/" . $line);
  61. // create _SINGLE_ controller if view
  62. if($method === "view") {
  63. $currentSubController = new GenController($currentRoot, $currentController->name . "_SINGLE");
  64. $currentSubController->dbTable = $currentController->dbTable;
  65. $currentSubController->parentRoute = "/" . $line;
  66. $currentSubController->sub = true;
  67. $newMethod->redirect = "/" . $line . "/SUB_dashboard";
  68. }
  69. }
  70. }
  71. // new top level controller
  72. else if(empty($currentController) || $line !== $currentController->name) {
  73. if(!empty($currentController)) {
  74. $currentController->save();
  75. }
  76. $currentController = new GenController($currentRoot, $line);
  77. $currentController->dbTable = $dbTable;
  78. $currentController->addMethod("index", "/$line");
  79. if(!empty($currentSubController)) {
  80. $currentSubController->save();
  81. }
  82. $currentSubType = '';
  83. $currentSubController = new GenController();
  84. }
  85. break;
  86. case 8: // sub-type declaration
  87. $currentSubType = $line;
  88. break;
  89. case 12: // subs and actions
  90. if($currentSubType === 'ACTIONS') {
  91. $currentSubController->addMethod(
  92. "ACTION_" . $line,
  93. "/ACTION_" . $line
  94. );
  95. }
  96. else if($currentSubType === 'SUB') {
  97. $currentSubController->addMethod(
  98. "SUB_" . $line,
  99. "/SUB_" . $line
  100. );
  101. }
  102. break;
  103. }
  104. }
  105. }
  106. // do any pending saves
  107. if(!empty($currentSubController)) {
  108. $currentSubController->save();
  109. }
  110. if(!empty($currentController)) {
  111. $currentController->save();
  112. }
  113. echo "Saved " . base_path("routes/generated.php") . "\n";
  114. }
  115. private function numLS($line) {
  116. $count = 0;
  117. for ($i=0; $i<strlen($line); $i++) {
  118. if($line[$i] !== ' ') break;
  119. $count++;
  120. }
  121. return $count;
  122. }
  123. }
  124. class GenController {
  125. public $root;
  126. public $saved = false;
  127. public $name;
  128. public $methods;
  129. public $parentRoute = "";
  130. public $dbTable = null;
  131. public $sub = false;
  132. public function __construct($root = null, $name = null)
  133. {
  134. $this->root = $root;
  135. $this->name = $name;
  136. $this->methods = [];
  137. }
  138. public function addMethod($method, $route) {
  139. if($this->parentRoute) {
  140. $route = $this->parentRoute . $route;
  141. }
  142. $method = new GenControllerMethod($method, $route);
  143. $this->methods[] = $method;
  144. return $method;
  145. }
  146. public function save() {
  147. if(!$this->saved && !empty($this->root) && !empty($this->name)) {
  148. $this->saveController();
  149. $this->saveRoutes();
  150. $this->saved = true;
  151. // $this->log();
  152. }
  153. }
  154. public function saveController() {
  155. $text = file_get_contents(base_path('generatecv/tree-templates/controller.template.php'));
  156. $code = [];
  157. foreach ($this->methods as $method) {
  158. $code[] = "";
  159. $code[] = "\t// GET {$method->route}";
  160. $code[] = "\t" . 'public function ' . $method->name . '(Request $request' . ($method->hasUID ? ', $uid' : '') . ') {';
  161. if($method->redirect) {
  162. $code[] = "\t\treturn redirect('$method->redirect');";
  163. }
  164. else {
  165. if($method->hasUID) {
  166. $code[] = "\t\t\$record = DB::table('{$this->dbTable}')->where('uid', \$uid)->first();";
  167. $code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', compact('record'));";
  168. }
  169. else {
  170. $code[] = "\t\t\$records = DB::table('{$this->dbTable}')->get();";
  171. $code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', compact('records'));";
  172. }
  173. $this->saveView($this, $method);
  174. }
  175. $code[] = "\t}";
  176. }
  177. $text = str_replace("_NAME_", "{$this->name}_Controller", $text);
  178. $text = str_replace("// __METHODS__", implode("\n", $code), $text);
  179. file_put_contents(app_path("Http/Controllers/{$this->name}_Controller.php"), $text);
  180. echo "Generated " . app_path("Http/Controllers/{$this->name}_Controller.php") . "\n";
  181. }
  182. public function saveView(GenController $controller, GenControllerMethod $method) {
  183. /*
  184. $x = DB::getSchemaBuilder()->getColumnListing('client');
  185. print_r($x);
  186. */
  187. if($controller->sub) {
  188. $this->saveSubView($controller, $method);
  189. }
  190. else {
  191. if($method->name === 'view') {
  192. $this->saveShowView($controller, $method);
  193. }
  194. else if($method->name === 'index') {
  195. $this->saveIndexView($controller, $method);
  196. }
  197. else if($method->name === 'add_new') {
  198. $this->saveAddNewView($controller, $method);
  199. }
  200. }
  201. }
  202. public function saveIndexView(GenController $controller, GenControllerMethod $method)
  203. {
  204. $text = file_get_contents(base_path('generatecv/tree-templates/index.template.blade.php'));
  205. $text = str_replace("_NAME_", $controller->name, $text);
  206. $text = str_replace("_ADD_NEW_ROUTE_", $controller->name . '-add_new', $text);
  207. $columns = DB::getSchemaBuilder()->getColumnListing($controller->dbTable);
  208. $ths = [];
  209. $tds = [];
  210. foreach ($columns as $column) {
  211. $ths[] = "<th>{$this->snakeToTitleCase($column)}</th>";
  212. $tds[] = "<td><?= \$record->$column ?></td>";
  213. }
  214. $text = str_replace("<!-- __SCAFFOLD_THS__ -->", implode("\n", $ths), $text);
  215. $text = str_replace("<!-- __SCAFFOLD_TDS__ -->", implode("\n", $tds), $text);
  216. $this->file_force_contents(resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php"), $text);
  217. echo "Generated " . resource_path("views/{$controller->root}/{$controller->name}/{$method->name}.blade.php") . "\n";
  218. }
  219. public function saveShowView(GenController $controller, GenControllerMethod $method)
  220. {
  221. }
  222. public function saveSubView(GenController $controller, GenControllerMethod $method)
  223. {
  224. }
  225. public function saveAddNewView(GenController $controller, GenControllerMethod $method)
  226. {
  227. }
  228. public function saveRoutes() {
  229. $lines = ["// --- {$this->root}: {$this->name} --- //"];
  230. foreach ($this->methods as $method) {
  231. // FORMAT:
  232. // Route::get('/foo/bar/{uid}', 'FooController@bar')->name('foo-action');
  233. $lines[] = "Route::get('{$method->route}', '{$this->name}_Controller@{$method->name}')->name('{$this->name}-{$method->name}');";
  234. }
  235. $lines[] = '';
  236. $lines[] = '';
  237. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
  238. }
  239. public function log() {
  240. $this->w('');
  241. $this->w("Controller: app/Http/Controllers/{$this->name}_Controller");
  242. $this->w("Table: {$this->dbTable}");
  243. $this->w('---------------------------------------------');
  244. foreach ($this->methods as $method) {
  245. $this->w('Rout: ' . $method->route, 1);
  246. $this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
  247. if(!$method->redirect) {
  248. $this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
  249. }
  250. else {
  251. $this->w('Redi: ' . $method->redirect, 1);
  252. }
  253. $this->w('---------------------------------------------');
  254. }
  255. }
  256. public function w($line, $level = -1) {
  257. for($i=0; $i<$level; $i++) echo "\t";
  258. echo "$line\n";
  259. }
  260. private function snakeToTitleCase($text) {
  261. return ucwords(str_replace("_", " ", $text));
  262. }
  263. private function file_force_contents($dir, $contents){
  264. $dir = str_replace("\\", "/", $dir);
  265. $dir = str_replace( '//', '/', $dir);
  266. $parts = explode('/', $dir);
  267. $file = array_pop($parts);
  268. $dir = '';
  269. foreach($parts as $part) {
  270. if($part[strlen($part) - 1] !== ':') {
  271. if(!is_dir($dir .= "/$part")) mkdir($dir);
  272. }
  273. }
  274. file_put_contents("$dir/$file", $contents);
  275. }
  276. }
  277. class GenControllerMethod {
  278. public $name;
  279. public $route;
  280. public $hasUID = false;
  281. public $redirect = false;
  282. public function __construct($name, $route)
  283. {
  284. $this->name = $name;
  285. $this->route = $route;
  286. if(strpos($this->route, "{uid}") !== FALSE) {
  287. $this->hasUID = true;
  288. }
  289. }
  290. }