123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
- namespace Mpociot\ApiDoc\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Routing\Route;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\URL;
- use Mpociot\ApiDoc\Extracting\Generator;
- use Mpociot\ApiDoc\Matching\RouteMatcher\Match;
- use Mpociot\ApiDoc\Matching\RouteMatcherInterface;
- use Mpociot\ApiDoc\Tools\DocumentationConfig;
- use Mpociot\ApiDoc\Tools\Flags;
- use Mpociot\ApiDoc\Tools\Utils;
- use Mpociot\ApiDoc\Writing\Writer;
- use Mpociot\Reflection\DocBlock;
- use ReflectionClass;
- use ReflectionException;
- 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;
- /**
- * Execute the console command.
- *
- * @param RouteMatcherInterface $routeMatcher
- *
- * @return void
- */
- public function handle(RouteMatcherInterface $routeMatcher)
- {
- // 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);
- $routes = $routeMatcher->getRoutes($this->docConfig->get('routes'), $this->docConfig->get('router'));
- $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(
- $this,
- $this->docConfig,
- $this->option('force')
- );
- $writer->writeDocs($groupedRoutes);
- }
- /**
- * @param \Mpociot\ApiDoc\Extracting\Generator $generator
- * @param Match[] $routes
- *
- * @throws \ReflectionException
- *
- * @return array
- */
- private function processRoutes(Generator $generator, array $routes)
- {
- $parsedRoutes = [];
- foreach ($routes as $routeItem) {
- $route = $routeItem->getRoute();
- /** @var Route $route */
- $messageFormat = '%s route: [%s] %s';
- $routeMethods = implode(',', $generator->getMethods($route));
- $routePath = $generator->getUri($route);
- $routeControllerAndMethod = Utils::getRouteClassAndMethodNames($route->getAction());
- if (! $this->isValidRoute($routeControllerAndMethod)) {
- $this->warn(sprintf($messageFormat, 'Skipping invalid', $routeMethods, $routePath));
- continue;
- }
- if (! $this->doesControllerMethodExist($routeControllerAndMethod)) {
- $this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . ': Controller method does not exist.');
- continue;
- }
- if (! $this->isRouteVisibleForDocumentation($routeControllerAndMethod)) {
- $this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . ': @hideFromAPIDocumentation was specified.');
- continue;
- }
- try {
- $parsedRoutes[] = $generator->processRoute($route, $routeItem->getRules());
- $this->info(sprintf($messageFormat, 'Processed', $routeMethods, $routePath));
- } catch (\Exception $exception) {
- $this->warn(sprintf($messageFormat, 'Skipping', $routeMethods, $routePath) . '- Exception ' . get_class($exception) . ' encountered : ' . $exception->getMessage());
- }
- }
- return $parsedRoutes;
- }
- /**
- * @param array $routeControllerAndMethod
- *
- * @return bool
- */
- private function isValidRoute(array $routeControllerAndMethod = null)
- {
- if (is_array($routeControllerAndMethod)) {
- [$classOrObject, $method] = $routeControllerAndMethod;
- if (Utils::isInvokableObject($classOrObject)) {
- return true;
- }
- $routeControllerAndMethod = $classOrObject . '@' . $method;
- }
- return ! is_callable($routeControllerAndMethod) && ! is_null($routeControllerAndMethod);
- }
- /**
- * @param array $routeControllerAndMethod
- *
- * @throws ReflectionException
- *
- * @return bool
- */
- private function doesControllerMethodExist(array $routeControllerAndMethod)
- {
- [$class, $method] = $routeControllerAndMethod;
- $reflection = new ReflectionClass($class);
- if (! $reflection->hasMethod($method)) {
- return false;
- }
- return true;
- }
- /**
- * @param array $routeControllerAndMethod
- *
- * @throws ReflectionException
- *
- * @return bool
- */
- private function isRouteVisibleForDocumentation(array $routeControllerAndMethod)
- {
- $comment = Utils::reflectRouteMethod($routeControllerAndMethod)->getDocComment();
- if ($comment) {
- $phpdoc = new DocBlock($comment);
- return collect($phpdoc->getTags())
- ->filter(function ($tag) {
- return $tag->getName() === 'hideFromAPIDocumentation';
- })
- ->isEmpty();
- }
- return true;
- }
- }
|