Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

207 Zeilen
5.0KB

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Codes\UserRole;
  4. use App\Http\Controllers\Web\WebController;
  5. use Exception;
  6. use Illuminate\Routing\Route as RoutingRoute;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\Route;
  9. use Illuminate\Support\Str;
  10. use LogicException;
  11. class RouteListCsv extends BaseCommand
  12. {
  13. /**
  14. * コントローラー説明の個別定義
  15. * 主にミドルウェア側のコントローラーに適用
  16. */
  17. const CONTROLLER_DESCRIPTION = [
  18. \Laravel\Sanctum\Http\Controllers\CsrfCookieController::class => "csrf対策用",
  19. ];
  20. /**
  21. * The name and signature of the console command.
  22. *
  23. * @var string
  24. */
  25. protected $signature = 'route:csv {--tsv}';
  26. /**
  27. * The console command description.
  28. *
  29. * @var string
  30. */
  31. protected $description = 'ルート一覧をcsv標準出力する';
  32. private Context $context;
  33. private $fp = null;
  34. private string $separator;
  35. private int $rowIndex = 0;
  36. /**
  37. * Create a new command instance.
  38. *
  39. * @return void
  40. */
  41. public function __construct()
  42. {
  43. $this->outputInfoForBase = false;
  44. parent::__construct();
  45. }
  46. /**
  47. * Execute the console command.
  48. *
  49. * @return int
  50. */
  51. protected function service(): int
  52. {
  53. $routes = Route::getRoutes();
  54. $routes->getRoutes();
  55. $this->separator = $this->getSeparator();
  56. try {
  57. $this->fp = fopen(base_path("routes.csv"), "w");
  58. $this->putHeader();
  59. foreach ($routes->getRoutes() as $r) {
  60. if (Str::startsWith($r->uri(), ["_", "clockwork"])) {
  61. continue;
  62. }
  63. $this->put($r);
  64. }
  65. } catch (Exception $e) {
  66. $this->outputError($e->getMessage());
  67. if ($this->fp !== null) {
  68. fclose($this->fp);
  69. $this->fp = null;
  70. }
  71. return self::RESULTCODE_FAILED;
  72. }
  73. fclose($this->fp);
  74. return self::RESULTCODE_SUCCESS;
  75. }
  76. private function putHeader()
  77. {
  78. fputcsv($this->fp, [
  79. "#",
  80. "NAME",
  81. "説明",
  82. "メソッド",
  83. "URI",
  84. "NONE",
  85. "NORMAL",
  86. "コントローラー",
  87. ], $this->separator);
  88. }
  89. private function put(RoutingRoute $route)
  90. {
  91. if ($this->fp === null) {
  92. throw new LogicException("FP null");
  93. }
  94. $controller = $route->getController();
  95. fputcsv($this->fp, [
  96. $this->rowIndex,
  97. $this->getControllerName($controller),
  98. $this->getControllerDescription($controller),
  99. $this->getMethods($route),
  100. $route->uri(),
  101. $this->getRoleAuth($controller, UserRole::NONE),
  102. $this->getRoleAuth($controller, UserRole::NORMAL),
  103. $controller::class,
  104. ], $this->separator);
  105. $this->rowIndex++;
  106. }
  107. private function getMethods(RoutingRoute $route)
  108. {
  109. return implode('|', $route->methods());
  110. }
  111. private function getRoleAuth($controller, UserRole $role): string
  112. {
  113. $ret = true;
  114. if ($controller instanceof WebController) {
  115. $ret = $controller->canAccess($role);
  116. }
  117. return $ret ? "〇" : "-";
  118. }
  119. private function getControllerName($controller): string
  120. {
  121. if ($controller instanceof WebController) {
  122. return $controller->name();
  123. } else if (
  124. method_exists($controller, "name") &&
  125. is_callable([$controller, "name"])
  126. ) {
  127. $ret = $controller->name();
  128. return is_string($ret) ? $ret : "-";
  129. } else {
  130. // コントローラ名から名前空間、メソッド名、等を除去
  131. return Str::of($controller::class)
  132. ->afterLast("\\")
  133. ->beforeLast("@")
  134. ->replace("Controller", "");
  135. }
  136. }
  137. private function getControllerDescription($controller): string
  138. {
  139. if ($controller instanceof WebController) {
  140. return $controller->description();
  141. } else if (
  142. method_exists($controller, "description") &&
  143. is_callable([$controller, "description"])
  144. ) {
  145. $ret = $controller->description();
  146. return is_string($ret) ? $ret : "-";
  147. } else if (Arr::has(self::CONTROLLER_DESCRIPTION, $controller::class)) {
  148. return self::CONTROLLER_DESCRIPTION[$controller::class];
  149. } else {
  150. return "-";
  151. }
  152. }
  153. private function outputSjisFile()
  154. {
  155. $contents = file_get_contents(base_path("routes.csv"));
  156. file_put_contents(
  157. base_path("routes_sjis.csv"),
  158. mb_convert_encoding(
  159. $contents,
  160. "SJIS",
  161. "UTF8"
  162. )
  163. );
  164. }
  165. private function getSeparator(): string
  166. {
  167. return $this->option("tsv") ? "\t" : ",";
  168. }
  169. }