GenerateDocumentation.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 phpDocumentor\Reflection\DocBlock;
  7. use Symfony\Component\Process\Process;
  8. class GenerateDocumentation extends Command
  9. {
  10. /**
  11. * The Whiteboard repository URL
  12. */
  13. const WHITEBOARD_REPOSITORY = 'https://github.com/mpociot/whiteboard.git';
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'api:generate
  20. {--output=public/docs : The output path for the generated documentation}
  21. {--routePrefix= : The route prefix to use for generation}
  22. {--routes=* : The route names to use for generation}
  23. {--actAsUserId= : The user ID to use for API response calls}
  24. ';
  25. /**
  26. * The console command description.
  27. *
  28. * @var string
  29. */
  30. protected $description = 'Command description';
  31. /**
  32. * Create a new command instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. }
  40. /**
  41. * Execute the console command.
  42. *
  43. * @return mixed
  44. */
  45. public function handle()
  46. {
  47. $generator = new ApiDocGenerator();
  48. $allowedRoutes = $this->option('routes');
  49. $routePrefix = $this->option('routePrefix');
  50. $actAs = $this->option('actAsUserId');
  51. if ($routePrefix === null && !count($allowedRoutes)) {
  52. $this->error('You must provide either a route prefix or a route to generate the documentation.');
  53. return false;
  54. }
  55. if ($actAs !== null) {
  56. if (version_compare($this->laravel->version(),'5.2.0', '<')) {
  57. $userModel = config('auth.model');
  58. $user = $userModel::find($actAs);
  59. $this->laravel['auth']->setUser($user);
  60. } else {
  61. $userModel = config('auth.providers.users.model');
  62. $user = $userModel::find($actAs);
  63. $this->laravel['auth']->guard()->setUser($user);
  64. }
  65. }
  66. $routes = Route::getRoutes();
  67. /** @var \Illuminate\Routing\Route $route */
  68. $parsedRoutes = [];
  69. foreach ($routes as $route) {
  70. if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri())) {
  71. $parsedRoutes[] = $generator->processRoute($route);
  72. $this->info('Processed route: ' . $route->getUri());
  73. }
  74. }
  75. $this->writeMarkdown($parsedRoutes);
  76. }
  77. /**
  78. * @param $parsedRoutes
  79. */
  80. private function writeMarkdown($parsedRoutes)
  81. {
  82. $outputPath = $this->option('output');
  83. $markdown = view('apidoc::whiteboard')->with('parsedRoutes', $parsedRoutes);
  84. if (!is_dir($outputPath)) {
  85. $this->cloneWhiteboardRepository();
  86. if ($this->confirm('Would you like to install the NPM dependencies?', true)) {
  87. $process = (new Process('npm set progress=false && npm install', $outputPath))->setTimeout(null);
  88. if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
  89. $process->setTty(true);
  90. }
  91. $process->run(function ($type, $line) {
  92. $this->info($line);
  93. });
  94. }
  95. }
  96. file_put_contents($outputPath . DIRECTORY_SEPARATOR . 'source' . DIRECTORY_SEPARATOR . 'index.md', $markdown);
  97. $this->info('Wrote index.md to: ' . $outputPath);
  98. $this->info('Generating API HTML code');
  99. $process = (new Process('npm run-script generate', $outputPath))->setTimeout(null);
  100. if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
  101. $process->setTty(true);
  102. }
  103. $process->run(function ($type, $line) {
  104. $this->info($line);
  105. });
  106. $this->info('Wrote HTML documentation to: ' . $outputPath . '/public/index.html');
  107. }
  108. /**
  109. * Clone the Whiteboard nodejs repository
  110. */
  111. private function cloneWhiteboardRepository()
  112. {
  113. $outputPath = $this->option('output');
  114. mkdir($outputPath, 0777, true);
  115. // Clone whiteboard
  116. $this->info('Cloning whiteboard repository.');
  117. $process = (new Process('git clone ' . self::WHITEBOARD_REPOSITORY . ' ' . $outputPath))->setTimeout(null);
  118. if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
  119. $process->setTty(true);
  120. }
  121. $process->run(function ($type, $line) {
  122. $this->info($line);
  123. });
  124. }
  125. }