GenerateDocumentation.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Mpociot\ApiDoc\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Route;
  5. use Mpociot\ApiDoc\ApiDocGenerator;
  6. use Mpociot\Documentarian\Documentarian;
  7. class GenerateDocumentation extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'api:generate
  15. {--output=public/docs : The output path for the generated documentation}
  16. {--routePrefix= : The route prefix to use for generation}
  17. {--routes=* : The route names to use for generation}
  18. {--actAsUserId= : The user ID to use for API response calls}
  19. ';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Generate your API documentation from existing Laravel routes.';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle()
  41. {
  42. $generator = new ApiDocGenerator();
  43. $allowedRoutes = $this->option('routes');
  44. $routePrefix = $this->option('routePrefix');
  45. $actAs = $this->option('actAsUserId');
  46. if ($routePrefix === null && ! count($allowedRoutes)) {
  47. $this->error('You must provide either a route prefix or a route to generate the documentation.');
  48. return false;
  49. }
  50. if ($actAs !== null) {
  51. if (version_compare($this->laravel->version(), '5.2.0', '<')) {
  52. $userModel = config('auth.model');
  53. $user = $userModel::find($actAs);
  54. $this->laravel['auth']->setUser($user);
  55. } else {
  56. $userModel = config('auth.providers.users.model');
  57. $user = $userModel::find($actAs);
  58. $this->laravel['auth']->guard()->setUser($user);
  59. }
  60. }
  61. $routes = Route::getRoutes();
  62. /* @var \Illuminate\Routing\Route $route */
  63. $parsedRoutes = [];
  64. foreach ($routes as $route) {
  65. if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri())) {
  66. $parsedRoutes[] = $generator->processRoute($route);
  67. $this->info('Processed route: '.$route->getUri());
  68. }
  69. }
  70. $this->writeMarkdown($parsedRoutes);
  71. }
  72. /**
  73. * @param $parsedRoutes
  74. */
  75. private function writeMarkdown($parsedRoutes)
  76. {
  77. $outputPath = $this->option('output');
  78. $documentarian = new Documentarian();
  79. $markdown = view('apidoc::documentarian')->with('parsedRoutes', $parsedRoutes);
  80. if (! is_dir($outputPath)) {
  81. $documentarian->create($outputPath);
  82. }
  83. file_put_contents($outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'index.md', $markdown);
  84. $this->info('Wrote index.md to: '.$outputPath);
  85. $this->info('Generating API HTML code');
  86. $documentarian->generate($outputPath);
  87. $this->info('Wrote HTML documentation to: '.$outputPath.'/public/index.html');
  88. }
  89. }