mirror of
https://github.com/ambieco/scribe.git
synced 2026-04-24 09:04:56 +08:00
Fix style issues, remove per-router generators
This commit is contained in:
@@ -92,10 +92,10 @@ return [
|
||||
*/
|
||||
'response_calls' => [
|
||||
/*
|
||||
* What HTTP methods (GET, POST, etc) should API calls be made for. List the methods here
|
||||
* or use '*' to mean all methods. Set to false to disable API calls.
|
||||
* API calls will be made only for routes in this group matching these HTTP methods (GET, POST, etc).
|
||||
* List the methods here or use '*' to mean all methods. Leave empty to disable API calls.
|
||||
*/
|
||||
'methods' => ['*'],
|
||||
'methods' => ['GET'],
|
||||
|
||||
/*
|
||||
* For URLs which have parameters (/users/{user}, /orders/{id?}),
|
||||
@@ -108,6 +108,8 @@ return [
|
||||
|
||||
/*
|
||||
* Environment variables which should be set for the API call.
|
||||
* This is a good place to ensure that notifications, emails
|
||||
* and other external services arenot triggered during the documentation API calls
|
||||
*/
|
||||
'env' => [
|
||||
'APP_ENV' => 'documentation',
|
||||
@@ -124,7 +126,6 @@ return [
|
||||
// 'key' => 'value',
|
||||
],
|
||||
|
||||
|
||||
/*
|
||||
* Query parameters which should be sent with the API call.
|
||||
*/
|
||||
@@ -132,7 +133,6 @@ return [
|
||||
// 'key' => 'value',
|
||||
],
|
||||
|
||||
|
||||
/*
|
||||
* Body parameters which should be sent with the API call.
|
||||
*/
|
||||
|
||||
@@ -8,11 +8,9 @@ use Illuminate\Console\Command;
|
||||
use Mpociot\Reflection\DocBlock;
|
||||
use Illuminate\Support\Collection;
|
||||
use Mpociot\ApiDoc\Tools\RouteMatcher;
|
||||
use Mpociot\ApiDoc\Generators\Generator;
|
||||
use Mpociot\Documentarian\Documentarian;
|
||||
use Mpociot\ApiDoc\Postman\CollectionWriter;
|
||||
use Mpociot\ApiDoc\Generators\DingoGenerator;
|
||||
use Mpociot\ApiDoc\Generators\LaravelGenerator;
|
||||
use Mpociot\ApiDoc\Generators\AbstractGenerator;
|
||||
|
||||
class GenerateDocumentation extends Command
|
||||
{
|
||||
@@ -47,15 +45,14 @@ class GenerateDocumentation extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$usingDIngoRouter = config('apidoc.router') == 'dingo';
|
||||
if ($usingDIngoRouter) {
|
||||
$usingDingoRouter = config('apidoc.router') == 'dingo';
|
||||
if ($usingDingoRouter) {
|
||||
$routes = $this->routeMatcher->getDingoRoutesToBeDocumented(config('apidoc.routes'));
|
||||
$generator = new DingoGenerator();
|
||||
} else {
|
||||
$routes = $this->routeMatcher->getLaravelRoutesToBeDocumented(config('apidoc.routes'));
|
||||
$generator = new LaravelGenerator();
|
||||
}
|
||||
|
||||
$generator = new Generator();
|
||||
$parsedRoutes = $this->processRoutes($generator, $routes);
|
||||
$parsedRoutes = collect($parsedRoutes)->groupBy('group')
|
||||
->sort(function ($a, $b) {
|
||||
@@ -175,12 +172,12 @@ class GenerateDocumentation extends Command
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractGenerator $generator
|
||||
* @param Generator $generator
|
||||
* @param array $routes
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function processRoutes(AbstractGenerator $generator, array $routes)
|
||||
private function processRoutes(Generator $generator, array $routes)
|
||||
{
|
||||
$parsedRoutes = [];
|
||||
foreach ($routes as $routeItem) {
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Mpociot\ApiDoc\Generators;
|
||||
|
||||
class DingoGenerator extends AbstractGenerator
|
||||
{
|
||||
/**
|
||||
* Prepares / Disables route middlewares.
|
||||
*
|
||||
* @param bool $disable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function prepareMiddleware($disable = true)
|
||||
{
|
||||
// Not needed by Dingo
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
|
||||
{
|
||||
$dispatcher = app('Dingo\Api\Dispatcher')->raw();
|
||||
|
||||
collect($server)->map(function ($key, $value) use ($dispatcher) {
|
||||
$dispatcher->header($value, $key);
|
||||
});
|
||||
|
||||
return call_user_func_array([$dispatcher, strtolower($method)], [$uri]);
|
||||
}
|
||||
}
|
||||
@@ -10,18 +10,8 @@ use Mpociot\Reflection\DocBlock;
|
||||
use Mpociot\Reflection\DocBlock\Tag;
|
||||
use Mpociot\ApiDoc\Tools\ResponseResolver;
|
||||
|
||||
abstract class AbstractGenerator
|
||||
class Generator
|
||||
{
|
||||
/**
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDomain(Route $route)
|
||||
{
|
||||
return $route->domain() == null ? '*' : $route->domain();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Route $route
|
||||
*
|
||||
@@ -76,15 +66,6 @@ abstract class AbstractGenerator
|
||||
return $parsedRoute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares / Disables route middlewares.
|
||||
*
|
||||
* @param bool $disable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function prepareMiddleware($enable = false);
|
||||
|
||||
/**
|
||||
* @param array $tags
|
||||
*
|
||||
@@ -159,7 +140,6 @@ abstract class AbstractGenerator
|
||||
* @param ReflectionMethod $method
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
*/
|
||||
protected function getRouteGroup(ReflectionClass $controller, ReflectionMethod $method)
|
||||
{
|
||||
@@ -1,85 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Mpociot\ApiDoc\Generators;
|
||||
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
|
||||
class LaravelGenerator extends AbstractGenerator
|
||||
{
|
||||
/**
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUri(Route $route)
|
||||
{
|
||||
if (version_compare(app()->version(), '5.4', '<')) {
|
||||
return $route->getUri();
|
||||
}
|
||||
|
||||
return $route->uri();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Route $route
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMethods(Route $route)
|
||||
{
|
||||
if (version_compare(app()->version(), '5.4', '<')) {
|
||||
$methods = $route->getMethods();
|
||||
} else {
|
||||
$methods = $route->methods();
|
||||
}
|
||||
|
||||
return array_diff($methods, ['HEAD']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares / Disables route middlewares.
|
||||
*
|
||||
* @param bool $disable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function prepareMiddleware($enable = true)
|
||||
{
|
||||
App::instance('middleware.disable', ! $enable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the given URI and return the Response.
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $uri
|
||||
* @param array $parameters
|
||||
* @param array $cookies
|
||||
* @param array $files
|
||||
* @param array $server
|
||||
* @param string $content
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
|
||||
{
|
||||
$server = collect([
|
||||
'CONTENT_TYPE' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
])->merge($server)->toArray();
|
||||
|
||||
$request = Request::create(
|
||||
$uri, $method, $parameters,
|
||||
$cookies, $files, $this->transformHeadersToServerVars($server), $content
|
||||
);
|
||||
|
||||
$kernel = App::make('Illuminate\Contracts\Http\Kernel');
|
||||
$response = $kernel->handle($request);
|
||||
|
||||
$kernel->terminate($request, $response);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ class ResponseResolver
|
||||
foreach (static::$strategies as $strategy) {
|
||||
$strategy = new $strategy();
|
||||
$response = $strategy($this->route, $tags, $rulesToApply);
|
||||
if (!is_null($response)) {
|
||||
if (! is_null($response)) {
|
||||
return $this->getResponseContent($response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use Illuminate\Http\Response;
|
||||
use Illuminate\Routing\Route;
|
||||
|
||||
/**
|
||||
* Make a call to the route and retrieve its response
|
||||
* Make a call to the route and retrieve its response.
|
||||
*/
|
||||
class ResponseCallStrategy
|
||||
{
|
||||
@@ -34,7 +34,7 @@ class ResponseCallStrategy
|
||||
|
||||
private function configureEnvironment(array $rulesToApply)
|
||||
{
|
||||
$this->enableDbTransactions();
|
||||
$this->startDbTransaction();
|
||||
$this->setEnvironmentVariables($rulesToApply['env'] ?? []);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class ResponseCallStrategy
|
||||
|
||||
/**
|
||||
* Transform parameters in URLs into real values (/users/{user} -> /users/2).
|
||||
* Uses bindings specified by caller, otherwise just uses '1'
|
||||
* Uses bindings specified by caller, otherwise just uses '1'.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param array $bindings
|
||||
@@ -82,32 +82,27 @@ class ResponseCallStrategy
|
||||
}
|
||||
}
|
||||
|
||||
private function enableDbTransactions()
|
||||
private function startDbTransaction()
|
||||
{
|
||||
try {
|
||||
app('db')->beginTransaction();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function disableDbTransactions()
|
||||
private function endDbTransaction()
|
||||
{
|
||||
try {
|
||||
app('db')->rollBack();
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function finish()
|
||||
{
|
||||
$this->disableDbTransactions();
|
||||
$this->endDbTransaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function callDingoRoute(Request $request)
|
||||
{
|
||||
/** @var Dispatcher $dispatcher */
|
||||
@@ -124,7 +119,7 @@ class ResponseCallStrategy
|
||||
// set URL and query parameters
|
||||
$uri = $request->getRequestUri();
|
||||
$query = $request->getQueryString();
|
||||
if (!empty($query)) {
|
||||
if (! empty($query)) {
|
||||
$uri .= "?$query";
|
||||
}
|
||||
$response = call_user_func_array([$dispatcher, strtolower($request->method())], [$uri]);
|
||||
@@ -209,6 +204,10 @@ class ResponseCallStrategy
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_string($allowedMethods) && $allowedMethods == '*') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (array_search('*', $allowedMethods) !== false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -34,5 +34,4 @@ class ResponseTagStrategy
|
||||
|
||||
return response()->json(json_decode($responseTag->getContent(), true));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class TransformerTagsStrategy
|
||||
protected function getTransformerResponse(array $tags)
|
||||
{
|
||||
try {
|
||||
if(empty($transformerTag = $this->getTransformerTag($tags))) {
|
||||
if (empty($transformerTag = $this->getTransformerTag($tags))) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -83,7 +83,6 @@ class TransformerTagsStrategy
|
||||
}
|
||||
|
||||
return $type;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -48,7 +48,6 @@ class TestResourceController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Mpociot\ApiDoc\Tests\Unit;
|
||||
|
||||
use Dingo\Api\Routing\Router;
|
||||
use Mpociot\ApiDoc\Generators\DingoGenerator;
|
||||
use Mpociot\ApiDoc\Tests\Fixtures\TestController;
|
||||
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
|
||||
|
||||
@@ -21,9 +20,7 @@ class DingoGeneratorTest extends GeneratorTestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->generator = new DingoGenerator();
|
||||
config(['apidoc.router' => 'dingo']);
|
||||
|
||||
}
|
||||
|
||||
public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false)
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
namespace Mpociot\ApiDoc\Tests\Unit;
|
||||
|
||||
use Orchestra\Testbench\TestCase;
|
||||
use Mpociot\ApiDoc\Generators\LaravelGenerator;
|
||||
use Mpociot\ApiDoc\Generators\Generator;
|
||||
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
|
||||
|
||||
abstract class GeneratorTestCase extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Mpociot\ApiDoc\Generators\AbstractGenerator
|
||||
* @var \Mpociot\ApiDoc\Generators\Generator
|
||||
*/
|
||||
protected $generator;
|
||||
|
||||
@@ -26,6 +26,8 @@ abstract class GeneratorTestCase extends TestCase
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->generator = new Generator();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -257,7 +259,7 @@ abstract class GeneratorTestCase extends TestCase
|
||||
],
|
||||
'body' => [
|
||||
'bodyParam' => 'bodyValue',
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
$parsed = $this->generator->processRoute($route, $rules);
|
||||
|
||||
@@ -3,10 +3,9 @@
|
||||
namespace Mpociot\ApiDoc\Tests\Unit;
|
||||
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Facades\Route as RouteFacade;
|
||||
use Mpociot\ApiDoc\Generators\LaravelGenerator;
|
||||
use Mpociot\ApiDoc\Tests\Fixtures\TestController;
|
||||
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
|
||||
use Illuminate\Support\Facades\Route as RouteFacade;
|
||||
|
||||
class LaravelGeneratorTest extends GeneratorTestCase
|
||||
{
|
||||
@@ -17,19 +16,12 @@ class LaravelGeneratorTest extends GeneratorTestCase
|
||||
];
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->generator = new LaravelGenerator();
|
||||
}
|
||||
|
||||
public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false)
|
||||
{
|
||||
if ($register) {
|
||||
return RouteFacade::{$httpMethod}($path, TestController::class . "@$controllerMethod");
|
||||
return RouteFacade::{$httpMethod}($path, TestController::class."@$controllerMethod");
|
||||
} else {
|
||||
return new Route([$httpMethod], $path, ['uses' => TestController::class . "@$controllerMethod"]);
|
||||
return new Route([$httpMethod], $path, ['uses' => TestController::class."@$controllerMethod"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user