領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.1KB

  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. "S",
  85. "C",
  86. "A",
  87. "L",
  88. "N",
  89. "コントローラー",
  90. ], $this->separator);
  91. }
  92. private function put(RoutingRoute $route)
  93. {
  94. if ($this->fp === null) {
  95. throw new LogicException("FP null");
  96. }
  97. $controller = $route->getController();
  98. fputcsv($this->fp, [
  99. $this->rowIndex,
  100. $this->getControllerName($controller),
  101. $this->getControllerDescription($controller),
  102. $this->getMethods($route),
  103. $route->uri(),
  104. $this->getRoleAuth($controller, UserRole::NORMAL_ADMIN),
  105. $this->getRoleAuth($controller, UserRole::CONTRACT_ADMIN),
  106. $this->getRoleAuth($controller, UserRole::SUPER_ADMIN),
  107. $controller::class,
  108. ], $this->separator);
  109. $this->rowIndex++;
  110. }
  111. private function getMethods(RoutingRoute $route)
  112. {
  113. return implode('|', $route->methods());
  114. }
  115. private function getRoleAuth($controller, UserRole $role): string
  116. {
  117. $ret = true;
  118. if ($controller instanceof WebController) {
  119. $ret = $controller->canAccess($role);
  120. }
  121. return $ret ? "〇" : "-";
  122. }
  123. private function getControllerName($controller): string
  124. {
  125. if ($controller instanceof WebController) {
  126. return $controller->name();
  127. } else if (
  128. method_exists($controller, "name") &&
  129. is_callable([$controller, "name"])
  130. ) {
  131. $ret = $controller->name();
  132. return is_string($ret) ? $ret : "-";
  133. } else {
  134. // コントローラ名から名前空間、メソッド名、等を除去
  135. return Str::of($controller::class)
  136. ->afterLast("\\")
  137. ->beforeLast("@")
  138. ->replace("Controller", "");
  139. }
  140. }
  141. private function getControllerDescription($controller): string
  142. {
  143. if ($controller instanceof WebController) {
  144. return $controller->description();
  145. } else if (
  146. method_exists($controller, "description") &&
  147. is_callable([$controller, "description"])
  148. ) {
  149. $ret = $controller->description();
  150. return is_string($ret) ? $ret : "-";
  151. } else if (Arr::has(self::CONTROLLER_DESCRIPTION, $controller::class)) {
  152. return self::CONTROLLER_DESCRIPTION[$controller::class];
  153. } else {
  154. return "-";
  155. }
  156. }
  157. private function outputSjisFile()
  158. {
  159. $contents = file_get_contents(base_path("routes.csv"));
  160. file_put_contents(
  161. base_path("routes_sjis.csv"),
  162. mb_convert_encoding(
  163. $contents,
  164. "SJIS",
  165. "UTF8"
  166. )
  167. );
  168. }
  169. private function getSeparator(): string
  170. {
  171. return $this->option("tsv") ? "\t" : ",";
  172. }
  173. }