Pārlūkot izejas kodu

Using route grouping in generated routes

Vijayakrishnan Krishnan 5 gadi atpakaļ
vecāks
revīzija
edd93a9ef4
1 mainītis faili ar 84 papildinājumiem un 0 dzēšanām
  1. 84 0
      app/Console/Commands/GenerateTreeCommand.php

+ 84 - 0
app/Console/Commands/GenerateTreeCommand.php

@@ -322,6 +322,7 @@ class GenerateTreeCommand extends Command
         file_put_contents(resource_path("views/layouts/generated-links.blade.php"), implode("\n", $sideLinks));
         echo "Saved " . resource_path("views/layouts/generated-links.blade.php") . "\n";
 
+        $this->humanizeRoutes();
     }
 
     private function numLS($line) {
@@ -333,6 +334,89 @@ class GenerateTreeCommand extends Command
         return $count;
     }
 
+    private function humanizeRoutes() {
+        $path = base_path("routes/generated.php");
+        $file = fopen($path, "r");
+        $lines = [];
+
+        $groups = [];
+        $currentGroup = FALSE;
+        $comment = '';
+        $prefix = '';
+
+        while (!feof($file)) {
+            $line = rtrim(fgets($file));
+
+            if($line === "") {
+                if($currentGroup !== FALSE) {
+                    $groups[] = $currentGroup;
+                    $currentGroup = FALSE;
+                }
+            }
+            else if(strpos($line, "// --- ") === 0) {
+                $comment = $line;
+            }
+            else if(strpos($line, "Route::get('") === 0) {
+
+                $parts = explode("'", $line);
+                $route = $parts[1];
+                $action = $parts[3];
+                $name = $parts[5];
+
+                if($currentGroup === FALSE) {
+                    $prefix = $route;
+                    if (($uidPos = strpos($route, "/{uid}/")) !== FALSE) {
+                        $prefix = substr($route, 0, $uidPos + 6);
+                    }
+                }
+
+                $route = substr($route, strlen($prefix));
+                if(strlen($route) && $route[0] === "/") {
+                    $route = substr($route, 1);
+                }
+
+                if($currentGroup === FALSE) {
+                    $currentGroup = [
+                        "comment" => $comment,
+                        "prefix" => $prefix,
+                        "routes" => [
+                            [
+                                "route" => $route,
+                                "action" => $action,
+                                "name" => $name
+                            ]
+                        ]
+                    ];
+                }
+                else {
+                    $currentGroup["routes"][] = [
+                        "route" => $route,
+                        "action" => $action,
+                        "name" => $name
+                    ];
+                }
+            }
+        }
+
+        fclose($file);
+
+        // write the routes
+        $lines = ["<?php", "", "use Illuminate\Support\Facades\Route;", ""];
+        foreach ($groups as $group) {
+            $lines[] = $group["comment"];
+            $lines[] = "Route::prefix('{$group["prefix"]}')->group(function () {";
+            foreach ($group["routes"] as $route) {
+                $lines[] = "\tRoute::get('{$route["route"]}', '{$route["action"]}')->name('{$route["name"]}');";
+            }
+            $lines[] = "});";
+            $lines[] = "";
+        }
+
+        file_put_contents($path, implode("\n", $lines));
+
+        echo "Cleaned " . base_path("routes/generated.php") . "\n";
+
+    }
 }
 
 class GenController {