GenerateTreeCommand.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class GenerateTreeCommand extends Command
  5. {
  6. private $routesFile = null;
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'generatetree
  13. {path: /path/to/tree.txt}';
  14. /**
  15. * Execute the console command.
  16. *
  17. * @return mixed
  18. */
  19. public function handle()
  20. {
  21. $lines = ['<?php', '', 'use Illuminate\Support\Facades\Route;', '', ''];
  22. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines));
  23. global $argv;
  24. $file = fopen($argv[2], "r");
  25. $lines = [];
  26. while (!feof($file)) {
  27. $line = rtrim(fgets($file));
  28. if(trim($line) !== '') {
  29. $lines[] = $line;
  30. }
  31. }
  32. fclose($file);
  33. $currentRoot = "";
  34. $currentController = new GenController();
  35. $currentSubController = new GenController();
  36. $currentSubType = "";
  37. $currentView = "";
  38. foreach ($lines as $line) {
  39. $lineType = null;
  40. // no leading space - root specifier
  41. if($line[0] !== ' ') {
  42. $currentRoot = trim($line);
  43. }
  44. else {
  45. $ls = $this->numLS($line);
  46. $line = trim($line);
  47. switch($ls) {
  48. case 4: // top level controller OR top level controller action
  49. // top level controller-action
  50. if(strpos($line, "/") !== FALSE) {
  51. if(!empty($currentController)) {
  52. $method = explode("/", $line)[1];
  53. $newMethod = $currentController->addMethod($method, "/" . $line);
  54. // create _SINGLE_ controller if view
  55. if($method === "view") {
  56. $currentSubController = new GenController($currentRoot, $currentController->name . "_SINGLE");
  57. $currentSubController->parentRoute = "/" . $line;
  58. $newMethod->redirect = "/" . $line . "/SUB_dashboard";
  59. }
  60. }
  61. }
  62. // new top level controller
  63. else if(empty($currentController) || $line !== $currentController->name) {
  64. if(!empty($currentController)) {
  65. $currentController->save();
  66. }
  67. $currentController = new GenController($currentRoot, $line);
  68. $currentController->addMethod("index", "/$line");
  69. if(!empty($currentSubController)) {
  70. $currentSubController->save();
  71. }
  72. $currentSubType = '';
  73. $currentSubController = new GenController();
  74. }
  75. break;
  76. case 8: // sub-type declaration
  77. $currentSubType = $line;
  78. break;
  79. case 12: // subs and actions
  80. if($currentSubType === 'ACTIONS') {
  81. $currentSubController->addMethod(
  82. "ACTION_" . $line,
  83. "/ACTION_" . $line
  84. );
  85. }
  86. else if($currentSubType === 'SUB') {
  87. $currentSubController->addMethod(
  88. "SUB_" . $line,
  89. "/SUB_" . $line
  90. );
  91. }
  92. break;
  93. }
  94. }
  95. }
  96. // do any pending saves
  97. if(!empty($currentSubController)) {
  98. $currentSubController->save();
  99. }
  100. if(!empty($currentController)) {
  101. $currentController->save();
  102. }
  103. }
  104. private function numLS($line) {
  105. $count = 0;
  106. for ($i=0; $i<strlen($line); $i++) {
  107. if($line[$i] !== ' ') break;
  108. $count++;
  109. }
  110. return $count;
  111. }
  112. private function initRoutesFile() {
  113. }
  114. }
  115. class GenController {
  116. public $root;
  117. public $saved = false;
  118. public $name;
  119. public $methods;
  120. public $parentRoute = "";
  121. public function __construct($root = null, $name = null)
  122. {
  123. $this->root = $root;
  124. $this->name = $name;
  125. $this->methods = [];
  126. }
  127. public function addMethod($method, $route) {
  128. if($this->parentRoute) {
  129. $route = $this->parentRoute . $route;
  130. }
  131. $method = new GenControllerMethod($method, $route);
  132. $this->methods[] = $method;
  133. return $method;
  134. }
  135. public function save() {
  136. if(!$this->saved && !empty($this->root) && !empty($this->name)) {
  137. // todo: save controller
  138. // todo: save views
  139. // save routes
  140. $lines = ["// --- {$this->root}: {$this->name} --- //"];
  141. foreach ($this->methods as $method) {
  142. // FORMAT:
  143. // Route::get('/foo/bar/{uid}', 'FooController@bar')->name('foo-action');
  144. $lines[] = "Route::get('{$method->route}', '{$this->name}_Controller@{$method->name}')->name('{$this->name}-{$method->name}');";
  145. }
  146. $lines[] = '';
  147. $lines[] = '';
  148. file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
  149. // echo "Saved " . base_path("routes/generated.php") . "\n";
  150. $this->saved = true;
  151. // $this->log();
  152. }
  153. }
  154. public function log() {
  155. $this->w('');
  156. $this->w("Controller: app/Http/Controllers/{$this->name}_Controller");
  157. $this->w('---------------------------------------------');
  158. foreach ($this->methods as $method) {
  159. $this->w('Rout: ' . $method->route, 1);
  160. $this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
  161. if(!$method->redirect) {
  162. $this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
  163. }
  164. else {
  165. $this->w('Redi: ' . $method->redirect, 1);
  166. }
  167. $this->w('---------------------------------------------');
  168. }
  169. }
  170. public function w($line, $level = -1) {
  171. for($i=0; $i<$level; $i++) echo "\t";
  172. echo "$line\n";
  173. }
  174. }
  175. class GenControllerMethod {
  176. public $name;
  177. public $route;
  178. public $hasUID = false;
  179. public $redirect = false;
  180. public function __construct($name, $route)
  181. {
  182. $this->name = $name;
  183. $this->route = $route;
  184. if(strpos($this->route, ":uid") !== FALSE) {
  185. $this->hasUID = true;
  186. }
  187. }
  188. }