"csrf対策用", ]; /** * The name and signature of the console command. * * @var string */ protected $signature = 'route:csv {--tsv}'; /** * The console command description. * * @var string */ protected $description = 'ルート一覧をcsv標準出力する'; private Context $context; private $fp = null; private string $separator; private int $rowIndex = 0; /** * Create a new command instance. * * @return void */ public function __construct() { $this->outputInfoForBase = false; parent::__construct(); } /** * Execute the console command. * * @return int */ protected function service(): int { $routes = Route::getRoutes(); $routes->getRoutes(); $this->separator = $this->getSeparator(); try { $this->fp = fopen(base_path("routes.csv"), "w"); $this->putHeader(); foreach ($routes->getRoutes() as $r) { if (Str::startsWith($r->uri(), ["_", "clockwork"])) { continue; } $this->put($r); } } catch (Exception $e) { $this->outputError($e->getMessage()); if ($this->fp !== null) { fclose($this->fp); $this->fp = null; } return self::RESULTCODE_FAILED; } fclose($this->fp); return self::RESULTCODE_SUCCESS; } private function putHeader() { fputcsv($this->fp, [ "#", "NAME", "説明", "メソッド", "URI", "S", "C", "A", "L", "N", "コントローラー", ], $this->separator); } private function put(RoutingRoute $route) { if ($this->fp === null) { throw new LogicException("FP null"); } $controller = $route->getController(); fputcsv($this->fp, [ $this->rowIndex, $this->getControllerName($controller), $this->getControllerDescription($controller), $this->getMethods($route), $route->uri(), $this->getRoleAuth($controller, UserRole::NORMAL_ADMIN), $this->getRoleAuth($controller, UserRole::CONTRACT_ADMIN), $this->getRoleAuth($controller, UserRole::SUPER_ADMIN), $controller::class, ], $this->separator); $this->rowIndex++; } private function getMethods(RoutingRoute $route) { return implode('|', $route->methods()); } private function getRoleAuth($controller, UserRole $role): string { $ret = true; if ($controller instanceof WebController) { $ret = $controller->canAccess($role); } return $ret ? "〇" : "-"; } private function getControllerName($controller): string { if ($controller instanceof WebController) { return $controller->name(); } else if ( method_exists($controller, "name") && is_callable([$controller, "name"]) ) { $ret = $controller->name(); return is_string($ret) ? $ret : "-"; } else { // コントローラ名から名前空間、メソッド名、等を除去 return Str::of($controller::class) ->afterLast("\\") ->beforeLast("@") ->replace("Controller", ""); } } private function getControllerDescription($controller): string { if ($controller instanceof WebController) { return $controller->description(); } else if ( method_exists($controller, "description") && is_callable([$controller, "description"]) ) { $ret = $controller->description(); return is_string($ret) ? $ret : "-"; } else if (Arr::has(self::CONTROLLER_DESCRIPTION, $controller::class)) { return self::CONTROLLER_DESCRIPTION[$controller::class]; } else { return "-"; } } private function outputSjisFile() { $contents = file_get_contents(base_path("routes.csv")); file_put_contents( base_path("routes_sjis.csv"), mb_convert_encoding( $contents, "SJIS", "UTF8" ) ); } private function getSeparator(): string { return $this->option("tsv") ? "\t" : ","; } }