diff --git a/composer.json b/composer.json index 39f8379..0b8e4dd 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require-dev": { - "php": ">=7.0", + "php": ">=7.1", "orchestra/testbench": "~4.0", "phpunit/phpunit": "^8.0|^9.0", "laravel/passport": "^8.0" diff --git a/src/GenerateSwaggerDoc.php b/src/GenerateSwaggerDoc.php index 22ca2a7..6b10802 100644 --- a/src/GenerateSwaggerDoc.php +++ b/src/GenerateSwaggerDoc.php @@ -6,6 +6,14 @@ class GenerateSwaggerDoc extends Command { + protected $responseGenerator; + + public function __construct(Responses\ResponseGeneratorInterface $responseGenerator = null) + { + parent::__construct(); + $this->responseGenerator = $responseGenerator; + } + /** * The name and signature of the console command. * @@ -34,7 +42,7 @@ public function handle() $filter = $this->option('filter') ?: null; $file = $this->option('output') ?: null; - $docs = (new Generator($config, $filter))->generate(); + $docs = (new Generator($config, $filter, $this->responseGenerator))->generate(); $formattedDocs = (new FormatterManager($docs)) ->setFormat($this->option('format')) diff --git a/src/Generator.php b/src/Generator.php index 804bad3..39b7f0f 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -15,16 +15,18 @@ class Generator protected $config; protected $routeFilter; + protected $responseGenerator; protected $docs; protected $route; protected $method; protected $docParser; protected $hasSecurityDefinitions; - public function __construct($config, $routeFilter = null) + public function __construct($config, $routeFilter = null, Responses\ResponseGeneratorInterface $responseGenerator = null) { $this->config = $config; $this->routeFilter = $routeFilter; + $this->responseGenerator = $responseGenerator ?: new Responses\ResponseGenerator(); $this->docParser = DocBlockFactory::createInstance(); $this->hasSecurityDefinitions = false; } @@ -137,13 +139,9 @@ protected function generatePath() 'summary' => $summary, 'description' => $description, 'deprecated' => $isDeprecated, - 'responses' => [ - '200' => [ - 'description' => 'OK', - ], - ], ]; + $this->addResponseDefinition(); $this->addActionParameters(); if ($this->hasSecurityDefinitions) { @@ -151,6 +149,13 @@ protected function generatePath() } } + protected function addResponseDefinition() + { + $actionInstance = $this->getActionClassInstance(); + $responses = $this->responseGenerator->getResponses($this->route->uri(), $this->method, $actionInstance); + $this->docs['paths'][$this->route->uri()][$this->method]['responses'] = $responses; + } + protected function addActionParameters() { $rules = $this->getFormRules() ?: []; diff --git a/src/Responses/ResponseGenerator.php b/src/Responses/ResponseGenerator.php new file mode 100644 index 0000000..cfd39fa --- /dev/null +++ b/src/Responses/ResponseGenerator.php @@ -0,0 +1,18 @@ + [ + 'description' => 'OK', + ], + ]; + } +} diff --git a/src/Responses/ResponseGeneratorInterface.php b/src/Responses/ResponseGeneratorInterface.php new file mode 100644 index 0000000..0a416bf --- /dev/null +++ b/src/Responses/ResponseGeneratorInterface.php @@ -0,0 +1,14 @@ +responseGenerator = $this->app->make(ResponseGeneratorInterface::class); + } catch (BindingResolutionException $e) { + $this->responseGenerator = null; + } + $this->generator = new Generator( - $this->config = config('laravel-swagger') + $this->config = config('laravel-swagger'), + null, + $this->responseGenerator ); } @@ -197,6 +209,9 @@ public function testRouteData($paths) Please read the documentation for more information EOD; + //Allow running tests on windows + $expectedPostDescription = str_replace(PHP_EOL, "\n", $expectedPostDescription); + $this->assertArrayHasKey('summary', $paths['/users']['get']); $this->assertArrayHasKey('description', $paths['/users']['get']); $this->assertArrayHasKey('responses', $paths['/users']['get']); @@ -282,7 +297,8 @@ public function testFiltersRoutes($routeFilter, $expectedRoutes) { $this->generator = new Generator( $this->config, - $routeFilter + $routeFilter, + $this->responseGenerator ); $docs = $this->generator->generate(); @@ -306,6 +322,6 @@ private function getDocsWithNewConfig(array $config) { $config = array_merge($this->config, $config); - return (new Generator($config))->generate(); + return (new Generator($config, null, $this->responseGenerator))->generate(); } }