|
@@ -3,8 +3,11 @@
|
|
|
namespace Mpociot\ApiDoc\Commands;
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
-use Mpociot\ApiDoc\ApiDocGenerator;
|
|
|
+use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
+use Mpociot\ApiDoc\Generators\AbstractGenerator;
|
|
|
+use Mpociot\ApiDoc\Generators\DingoGenerator;
|
|
|
+use Mpociot\ApiDoc\Generators\LaravelGenerator;
|
|
|
use Mpociot\Documentarian\Documentarian;
|
|
|
|
|
|
class GenerateDocumentation extends Command
|
|
@@ -19,6 +22,8 @@ class GenerateDocumentation extends Command
|
|
|
{--routePrefix= : The route prefix to use for generation}
|
|
|
{--routes=* : The route names to use for generation}
|
|
|
{--actAsUserId= : The user ID to use for API response calls}
|
|
|
+ {--router=laravel : The router to be used (Laravel or Dingo)}
|
|
|
+ {--bindings= : Route Model Bindings}
|
|
|
';
|
|
|
|
|
|
/**
|
|
@@ -45,9 +50,16 @@ class GenerateDocumentation extends Command
|
|
|
*/
|
|
|
public function handle()
|
|
|
{
|
|
|
+ if ($this->option('router') === 'laravel') {
|
|
|
+ $generator = new LaravelGenerator();
|
|
|
+ } else {
|
|
|
+ $generator = new DingoGenerator();
|
|
|
+ }
|
|
|
+
|
|
|
$allowedRoutes = $this->option('routes');
|
|
|
$routePrefix = $this->option('routePrefix');
|
|
|
- $actAs = $this->option('actAsUserId');
|
|
|
+
|
|
|
+ $this->setUserToBeImpersonated($this->option('actAsUserId'));
|
|
|
|
|
|
if ($routePrefix === null && ! count($allowedRoutes)) {
|
|
|
$this->error('You must provide either a route prefix or a route to generate the documentation.');
|
|
@@ -55,36 +67,18 @@ class GenerateDocumentation extends Command
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if ($actAs !== null) {
|
|
|
- if (version_compare($this->laravel->version(), '5.2.0', '<')) {
|
|
|
- $userModel = config('auth.model');
|
|
|
- $user = $userModel::find($actAs);
|
|
|
- $this->laravel['auth']->setUser($user);
|
|
|
- } else {
|
|
|
- $userModel = config('auth.providers.users.model');
|
|
|
- $user = $userModel::find($actAs);
|
|
|
- $this->laravel['auth']->guard()->setUser($user);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $routes = Route::getRoutes();
|
|
|
-
|
|
|
- $generator = new ApiDocGenerator();
|
|
|
-
|
|
|
- /* @var \Illuminate\Routing\Route $route */
|
|
|
- $parsedRoutes = [];
|
|
|
- foreach ($routes as $route) {
|
|
|
- if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri())) {
|
|
|
- $parsedRoutes[] = $generator->processRoute($route);
|
|
|
- $this->info('Processed route: '.$route->getUri());
|
|
|
- }
|
|
|
+ if ($this->option('router') === 'laravel') {
|
|
|
+ $parsedRoutes = $this->processLaravelRoutes($generator, $allowedRoutes, $routePrefix);
|
|
|
+ } else {
|
|
|
+ $parsedRoutes = $this->processDingoRoutes($generator, $allowedRoutes, $routePrefix);
|
|
|
}
|
|
|
+ $parsedRoutes = collect($parsedRoutes)->sortBy('resource')->groupBy('resource');
|
|
|
|
|
|
$this->writeMarkdown($parsedRoutes);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param array $parsedRoutes
|
|
|
+ * @param Collection $parsedRoutes
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
@@ -94,7 +88,7 @@ class GenerateDocumentation extends Command
|
|
|
|
|
|
$documentarian = new Documentarian();
|
|
|
|
|
|
- $markdown = view('apidoc::documentarian')->with('parsedRoutes', $parsedRoutes);
|
|
|
+ $markdown = view('apidoc::documentarian')->with('parsedRoutes', $parsedRoutes->all());
|
|
|
|
|
|
if (! is_dir($outputPath)) {
|
|
|
$documentarian->create($outputPath);
|
|
@@ -110,4 +104,92 @@ class GenerateDocumentation extends Command
|
|
|
|
|
|
$this->info('Wrote HTML documentation to: '.$outputPath.'/public/index.html');
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function getBindings()
|
|
|
+ {
|
|
|
+ $bindings = $this->option('bindings');
|
|
|
+ if (empty($bindings)) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $bindings = explode('|', $bindings);
|
|
|
+ $resultBindings = [];
|
|
|
+ foreach ($bindings as $binding) {
|
|
|
+ list($name, $id) = explode(',', $binding);
|
|
|
+ $resultBindings[$name] = $id;
|
|
|
+ }
|
|
|
+ return $resultBindings;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $actAs
|
|
|
+ */
|
|
|
+ private function setUserToBeImpersonated($actAs)
|
|
|
+ {
|
|
|
+ if (!empty($actAs)) {
|
|
|
+ if (version_compare($this->laravel->version(), '5.2.0', '<')) {
|
|
|
+ $userModel = config('auth.model');
|
|
|
+ $user = $userModel::find($actAs);
|
|
|
+ $this->laravel['auth']->setUser($user);
|
|
|
+ } else {
|
|
|
+ $userModel = config('auth.providers.users.model');
|
|
|
+ $user = $userModel::find($actAs);
|
|
|
+ $this->laravel['auth']->guard()->setUser($user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function getRoutes()
|
|
|
+ {
|
|
|
+ if ($this->option('router') === 'laravel') {
|
|
|
+ return Route::getRoutes();
|
|
|
+ } else {
|
|
|
+ return app('Dingo\Api\Routing\Router')->getRoutes()[$this->option('routePrefix')];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param AbstractGenerator $generator
|
|
|
+ * @param $allowedRoutes
|
|
|
+ * @param $routePrefix
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function processLaravelRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix)
|
|
|
+ {
|
|
|
+ $routes = $this->getRoutes();
|
|
|
+ $bindings = $this->getBindings();
|
|
|
+ $parsedRoutes = [];
|
|
|
+ foreach ($routes as $route) {
|
|
|
+ if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri())) {
|
|
|
+ $parsedRoutes[] = $generator->processRoute($route, $bindings);
|
|
|
+ $this->info('Processed route: '.$route->getUri());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $parsedRoutes;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param AbstractGenerator $generator
|
|
|
+ * @param $allowedRoutes
|
|
|
+ * @param $routePrefix
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function processDingoRoutes(AbstractGenerator $generator, $allowedRoutes, $routePrefix)
|
|
|
+ {
|
|
|
+ $routes = $this->getRoutes();
|
|
|
+ $bindings = $this->getBindings();
|
|
|
+ $parsedRoutes = [];
|
|
|
+ foreach ($routes as $route) {
|
|
|
+ if (empty($allowedRoutes) || in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->uri())) {
|
|
|
+ $parsedRoutes[] = $generator->processRoute($route, $bindings);
|
|
|
+ $this->info('Processed route: '.$route->uri());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $parsedRoutes;
|
|
|
+ }
|
|
|
}
|