Merge pull request #3 from mpociot/master

Update from base
This commit is contained in:
Shalvah A
2018-09-12 02:06:38 +01:00
committed by GitHub
6 changed files with 63 additions and 103 deletions

View File

@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
## [2.1.3] - 11th September, 2018
### Fixed
- Parse `@response` tags regardless of HTTP method (https://github.com/mpociot/laravel-apidoc-generator/pull/318)
## [2.1.2] - 10th September, 2018
### Fixed
- Set correct HTTP method when parsing FormRequest (https://github.com/mpociot/laravel-apidoc-generator/pull/314)

View File

@@ -43,7 +43,61 @@ abstract class AbstractGenerator
*
* @return array
*/
abstract public function processRoute($route, $bindings = [], $withResponse = true);
public function processRoute($route, $bindings = [], $headers = [], $withResponse = true)
{
$routeDomain = $route->domain();
$routeAction = $route->getAction();
$routeGroup = $this->getRouteGroup($routeAction['uses']);
$routeDescription = $this->getRouteDescription($routeAction['uses']);
$showresponse = null;
// set correct route domain
$headers[] = "HTTP_HOST: {$routeDomain}";
$headers[] = "SERVER_NAME: {$routeDomain}";
$content = '';
$response = null;
$docblockResponse = $this->getDocblockResponse($routeDescription['tags']);
if ($docblockResponse) {
// we have a response from the docblock ( @response )
$response = $docblockResponse;
$showresponse = true;
$content = $response->getContent();
}
if (! $response) {
$transformerResponse = $this->getTransformerResponse($routeDescription['tags']);
if ($transformerResponse) {
// we have a transformer response from the docblock ( @transformer || @transformercollection )
$response = $transformerResponse;
$showresponse = true;
$content = $response->getContent();
}
}
if (! $response && $withResponse) {
try {
$response = $this->getRouteResponse($route, $bindings, $headers);
if ($response->headers->get('Content-Type') === 'application/json') {
$content = json_decode($response->getContent(), JSON_PRETTY_PRINT);
} else {
$content = $response->getContent();
}
} catch (\Exception $e) {
dump("Couldn't get response for route: ".implode(',', $this->getMethods($route)).'] '.$route->uri().'', $e);
}
}
return $this->getParameters([
'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))),
'resource' => $routeGroup,
'title' => $routeDescription['short'],
'description' => $routeDescription['long'],
'methods' => $this->getMethods($route),
'uri' => $this->getUri($route),
'parameters' => [],
'response' => $content,
'showresponse' => $showresponse,
], $routeAction, $bindings);
}
/**
* Prepares / Disables route middlewares.
@@ -179,7 +233,7 @@ abstract class AbstractGenerator
/**
* @param \Illuminate\Routing\Route $route
*
* @return string
* @return array
*/
protected function getRouteDescription($route)
{

View File

@@ -2,45 +2,8 @@
namespace Mpociot\ApiDoc\Generators;
use Exception;
class DingoGenerator extends AbstractGenerator
{
/**
* @param \Illuminate\Routing\Route $route
* @param array $bindings
* @param array $headers
* @param bool $withResponse
*
* @return array
*/
public function processRoute($route, $bindings = [], $headers = [], $withResponse = true)
{
$response = '';
if ($withResponse) {
try {
$response = $this->getRouteResponse($route, $bindings, $headers);
} catch (Exception $e) {
}
}
$routeAction = $route->getAction();
$routeGroup = $this->getRouteGroup($routeAction['uses']);
$routeDescription = $this->getRouteDescription($routeAction['uses']);
return $this->getParameters([
'id' => md5($route->uri().':'.implode($route->getMethods())),
'resource' => $routeGroup,
'title' => $routeDescription['short'],
'description' => $routeDescription['long'],
'methods' => $route->getMethods(),
'uri' => $route->uri(),
'parameters' => [],
'response' => $response,
], $routeAction, $bindings);
}
/**
* Prepares / Disables route middlewares.
*

View File

@@ -53,67 +53,6 @@ class LaravelGenerator extends AbstractGenerator
return array_diff($methods, ['HEAD']);
}
/**
* @param \Illuminate\Routing\Route $route
* @param array $bindings
* @param array $headers
* @param bool $withResponse
*
* @return array
*/
public function processRoute($route, $bindings = [], $headers = [], $withResponse = true)
{
$content = '';
$routeDomain = $route->domain();
$routeAction = $route->getAction();
$routeGroup = $this->getRouteGroup($routeAction['uses']);
$routeDescription = $this->getRouteDescription($routeAction['uses']);
$showresponse = null;
// set correct route domain
$headers[] = "HTTP_HOST: {$routeDomain}";
$headers[] = "SERVER_NAME: {$routeDomain}";
if ($withResponse) {
$response = null;
$docblockResponse = $this->getDocblockResponse($routeDescription['tags']);
if ($docblockResponse) {
// we have a response from the docblock ( @response )
$response = $docblockResponse;
$showresponse = true;
}
if (! $response) {
$transformerResponse = $this->getTransformerResponse($routeDescription['tags']);
if ($transformerResponse) {
// we have a transformer response from the docblock ( @transformer || @transformercollection )
$response = $transformerResponse;
$showresponse = true;
}
}
if (! $response) {
$response = $this->getRouteResponse($route, $bindings, $headers);
}
if ($response->headers->get('Content-Type') === 'application/json') {
$content = json_decode($response->getContent(), JSON_PRETTY_PRINT);
} else {
$content = $response->getContent();
}
}
return $this->getParameters([
'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))),
'resource' => $routeGroup,
'title' => $routeDescription['short'],
'description' => $routeDescription['long'],
'methods' => $this->getMethods($route),
'uri' => $this->getUri($route),
'parameters' => [],
'response' => $content,
'showresponse' => $showresponse,
], $routeAction, $bindings);
}
/**
* Prepares / Disables route middlewares.
*

View File

@@ -350,12 +350,12 @@ class ApiDocGeneratorTest extends TestCase
public function testCanParseResponseTag()
{
RouteFacade::post('/responseTag', TestController::class.'@responseTag');
$route = new Route(['GET'], '/responseTag', ['uses' => TestController::class.'@responseTag']);
$route = new Route(['POST'], '/responseTag', ['uses' => TestController::class.'@responseTag']);
$parsed = $this->generator->processRoute($route);
$this->assertTrue(is_array($parsed));
$this->assertArrayHasKey('showresponse', $parsed);
$this->assertTrue($parsed['showresponse']);
$this->assertSame($parsed['response'], "{\n data: [],\n}");
$this->assertJsonStringEqualsJsonString(json_decode($parsed['response'], true), '{ "data": []}');
}
public function testCanParseTransformerTag()

View File

@@ -79,7 +79,7 @@ class TestController extends Controller
/**
* @response {
* data: [],
* "data": []
*}
*/
public function responseTag()