123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace Mpociot\ApiDoc\Commands;
- use ReflectionClass;
- use ReflectionException;
- use Illuminate\Routing\Route;
- use Illuminate\Console\Command;
- use Mpociot\ApiDoc\Tools\Flags;
- use Mpociot\ApiDoc\Tools\Utils;
- use Mpociot\Reflection\DocBlock;
- use Illuminate\Support\Collection;
- use Mpociot\ApiDoc\Writing\Writer;
- use Illuminate\Support\Facades\URL;
- use Mpociot\ApiDoc\Extracting\Generator;
- use Mpociot\ApiDoc\Matching\RouteMatcher;
- use Mpociot\ApiDoc\Tools\DocumentationConfig;
- class GenerateDocumentation extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'apidoc:generate
- {--force : Force rewriting of existing routes}
- ';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Generate your API documentation from existing Laravel routes.';
- /**
- * @var DocumentationConfig
- */
- private $docConfig;
- /**
- * @var string
- */
- private $baseUrl;
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return void
- */
- public function handle()
- {
- // Using a global static variable here, so fuck off if you don't like it.
- // Also, the --verbose option is included with all Artisan commands.
- Flags::$shouldBeVerbose = $this->option('verbose');
- $this->docConfig = new DocumentationConfig(config('apidoc'));
- $this->baseUrl = $this->docConfig->get('base_url') ?? config('app.url');
- URL::forceRootUrl($this->baseUrl);
- $routeMatcher = new RouteMatcher($this->docConfig->get('routes'), $this->docConfig->get('router'));
- $routes = $routeMatcher->getRoutes();
- $generator = new Generator($this->docConfig);
- $parsedRoutes = $this->processRoutes($generator, $routes);
- $groupedRoutes = collect($parsedRoutes)
- ->groupBy('metadata.groupName')
- ->sortBy(static function ($group) {
- /* @var $group Collection */
- return $group->first()['metadata']['groupName'];
- }, SORT_NATURAL);
- $writer = new Writer(
- $groupedRoutes,
- $this->option('force'),
- $this,
- $this->docConfig
- );
- $writer->writeDocs();
- }
- /**
- * @param \Mpociot\ApiDoc\Extracting\Generator $generator
- * @param array $routes
- *
- * @return array
- */
- private function processRoutes(Generator $generator, array $routes)
- {
- $parsedRoutes = [];
- foreach ($routes as $routeItem) {
- $route = $routeItem['route'];
- /** @var Route $route */
- $messageFormat = '%s route: [%s] %s';
- $routeMethods = implode(',', $generator->getMethods($route));
- $routePath = $generator->getUri($route);
- if (! $this->isValidRoute($route) || ! $this->isRouteVisibleForDocumentation($route->getAction())) {
- $this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath));
- continue;
- }
- try {
- $parsedRoutes[] = $generator->processRoute($route, $routeItem['apply'] ?? []);
- $this->info(sprintf($messageFormat, 'Processed', $routeMethods, $routePath));
- } catch (\Exception $exception) {
- $this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath).' - '.$exception->getMessage());
- }
- }
- return $parsedRoutes;
- }
- /**
- * @param Route $route
- *
- * @return bool
- */
- private function isValidRoute(Route $route)
- {
- $action = Utils::getRouteClassAndMethodNames($route->getAction());
- if (is_array($action)) {
- $action = implode('@', $action);
- }
- return ! is_callable($action) && ! is_null($action);
- }
- /**
- * @param array $action
- *
- * @throws ReflectionException
- *
- * @return bool
- */
- private function isRouteVisibleForDocumentation(array $action)
- {
- list($class, $method) = Utils::getRouteClassAndMethodNames($action);
- $reflection = new ReflectionClass($class);
- if (! $reflection->hasMethod($method)) {
- return false;
- }
- $comment = $reflection->getMethod($method)->getDocComment();
- if ($comment) {
- $phpdoc = new DocBlock($comment);
- return collect($phpdoc->getTags())
- ->filter(function ($tag) {
- return $tag->getName() === 'hideFromAPIDocumentation';
- })
- ->isEmpty();
- }
- return true;
- }
- }
|