123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- class GenerateTreeCommand extends Command
- {
- private $routesFile = null;
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'generatetree
- {path: /path/to/tree.txt}';
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $lines = ['<?php', '', 'use Illuminate\Support\Facades\Route;', '', ''];
- file_put_contents(base_path("routes/generated.php"), implode("\n", $lines));
- global $argv;
- $file = fopen($argv[2], "r");
- $lines = [];
- while (!feof($file)) {
- $line = rtrim(fgets($file));
- if(trim($line) !== '') {
- $lines[] = $line;
- }
- }
- fclose($file);
- $currentRoot = "";
- $currentController = new GenController();
- $currentSubController = new GenController();
- $currentSubType = "";
- $currentView = "";
- foreach ($lines as $line) {
- $lineType = null;
- // no leading space - root specifier
- if($line[0] !== ' ') {
- $currentRoot = trim($line);
- }
- else {
- $ls = $this->numLS($line);
- $line = trim($line);
- $tokens = explode("|", $line);
- $line = $tokens[0];
- $dbTable = null;
- if(count($tokens) === 2) {
- $dbTable = $tokens[1];
- }
- switch($ls) {
- case 4: // top level controller OR top level controller action
- // top level controller-action
- if(strpos($line, "/") !== FALSE) {
- if(!empty($currentController)) {
- $method = explode("/", $line)[1];
- $newMethod = $currentController->addMethod($method, "/" . $line);
- // create _SINGLE_ controller if view
- if($method === "view") {
- $currentSubController = new GenController($currentRoot, $currentController->name . "_SINGLE");
- $currentSubController->dbTable = $currentController->dbTable;
- $currentSubController->parentRoute = "/" . $line;
- $newMethod->redirect = "/" . $line . "/SUB_dashboard";
- }
- }
- }
- // new top level controller
- else if(empty($currentController) || $line !== $currentController->name) {
- if(!empty($currentController)) {
- $currentController->save();
- }
- $currentController = new GenController($currentRoot, $line);
- $currentController->dbTable = $dbTable;
- $currentController->addMethod("index", "/$line");
- if(!empty($currentSubController)) {
- $currentSubController->save();
- }
- $currentSubType = '';
- $currentSubController = new GenController();
- }
- break;
- case 8: // sub-type declaration
- $currentSubType = $line;
- break;
- case 12: // subs and actions
- if($currentSubType === 'ACTIONS') {
- $currentSubController->addMethod(
- "ACTION_" . $line,
- "/ACTION_" . $line
- );
- }
- else if($currentSubType === 'SUB') {
- $currentSubController->addMethod(
- "SUB_" . $line,
- "/SUB_" . $line
- );
- }
- break;
- }
- }
- }
- // do any pending saves
- if(!empty($currentSubController)) {
- $currentSubController->save();
- }
- if(!empty($currentController)) {
- $currentController->save();
- }
- echo "Saved " . base_path("routes/generated.php") . "\n";
- }
- private function numLS($line) {
- $count = 0;
- for ($i=0; $i<strlen($line); $i++) {
- if($line[$i] !== ' ') break;
- $count++;
- }
- return $count;
- }
- private function initRoutesFile() {
- }
- }
- class GenController {
- public $root;
- public $saved = false;
- public $name;
- public $methods;
- public $parentRoute = "";
- public $single = false;
- public $dbTable = null;
- public function __construct($root = null, $name = null)
- {
- $this->root = $root;
- $this->name = $name;
- $this->methods = [];
- }
- public function addMethod($method, $route) {
- if($this->parentRoute) {
- $route = $this->parentRoute . $route;
- }
- $method = new GenControllerMethod($method, $route);
- $this->methods[] = $method;
- return $method;
- }
- public function save() {
- if(!$this->saved && !empty($this->root) && !empty($this->name)) {
- $this->saveController();
- $this->saveRoutes();
- $this->saved = true;
- $this->log();
- }
- }
- public function saveController() {
- $text = file_get_contents(base_path('generatecv/tree-templates/controller.template.php'));
- $code = [];
- foreach ($this->methods as $method) {
- $code[] = "";
- $code[] = "\t// GET {$method->route}";
- $code[] = "\t" . 'public function ' . $method->name . '(Request $request' . ($method->hasUID ? ', $uid' : '') . ') {';
- if($method->redirect) {
- $code[] = "\t\treturn redirect('$method->redirect');";
- }
- else {
- if($method->hasUID) {
- $code[] = "\t\t\$record = DB::table('{$this->dbTable}')->where('uid', \$uid)->first();";
- $code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', compact('record'));";
- }
- else {
- $code[] = "\t\t\$records = DB::table('{$this->dbTable}')->get();";
- $code[] = "\t\treturn view('{$this->root}/{$this->name}/{$method->name}', compact('records'));";
- }
- }
- $code[] = "\t}";
- }
- $text = str_replace("_NAME_", "{$this->name}_Controller", $text);
- $text = str_replace("// __METHODS__", implode("\n", $code), $text);
- file_put_contents(app_path("Http/Controllers/{$this->name}_Controller.php"), $text);
- echo "Generated " . app_path("Http/Controllers/{$this->name}_Controller.php") . "\n";
- }
- public function saveRoutes() {
- $lines = ["// --- {$this->root}: {$this->name} --- //"];
- foreach ($this->methods as $method) {
- // FORMAT:
- // Route::get('/foo/bar/{uid}', 'FooController@bar')->name('foo-action');
- $lines[] = "Route::get('{$method->route}', '{$this->name}_Controller@{$method->name}')->name('{$this->name}-{$method->name}');";
- }
- $lines[] = '';
- $lines[] = '';
- file_put_contents(base_path("routes/generated.php"), implode("\n", $lines), FILE_APPEND);
- }
- public function log() {
- $this->w('');
- $this->w("Controller: app/Http/Controllers/{$this->name}_Controller");
- $this->w("Table: {$this->dbTable}");
- $this->w('---------------------------------------------');
- foreach ($this->methods as $method) {
- $this->w('Rout: ' . $method->route, 1);
- $this->w('Meth: ' . $method->name . '($request' . ($method->hasUID ? ', $uid' : '') . ')', 1);
- if(!$method->redirect) {
- $this->w('View: ' . resource_path("views/{$this->root}/{$this->name}/{$method->name}.blade.php"), 1);
- }
- else {
- $this->w('Redi: ' . $method->redirect, 1);
- }
- $this->w('---------------------------------------------');
- }
- }
- public function w($line, $level = -1) {
- for($i=0; $i<$level; $i++) echo "\t";
- echo "$line\n";
- }
- }
- class GenControllerMethod {
- public $name;
- public $route;
- public $hasUID = false;
- public $redirect = false;
- public function __construct($name, $route)
- {
- $this->name = $name;
- $this->route = $route;
- if(strpos($this->route, "{uid}") !== FALSE) {
- $this->hasUID = true;
- }
- }
- }
|