generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from dedoc/143-add-ability-to-manually-regist…
…er-docs-routes-or-some-other-way-to-customize-routes Ability to configure more API versions docs, customize docs domains and paths
- Loading branch information
Showing
10 changed files
with
229 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,7 @@ | ||
<?php | ||
|
||
use Dedoc\Scramble\Http\Middleware\RestrictedDocsAccess; | ||
use Illuminate\Support\Facades\Route; | ||
use Dedoc\Scramble\Scramble; | ||
|
||
Route::middleware(config('scramble.middleware', [RestrictedDocsAccess::class]))->group(function () { | ||
Route::get('docs/api.json', function (Dedoc\Scramble\Generator $generator) { | ||
return response()->json($generator(), options: JSON_PRETTY_PRINT); | ||
})->name('scramble.docs.index'); | ||
Scramble::registerUiRoute(path: 'docs/api')->name('scramble.docs.ui'); | ||
|
||
Route::view('docs/api', 'scramble::docs')->name('scramble.docs.api'); | ||
}); | ||
Scramble::registerJsonSpecificationRoute(path: 'docs/api.json')->name('scramble.docs.document'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Console\Commands; | ||
|
||
use Dedoc\Scramble\Generator; | ||
use Dedoc\Scramble\Scramble; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\File; | ||
|
||
class ExportDocumentation extends Command | ||
{ | ||
protected $signature = 'scramble:export | ||
{--path= : The path to save the exported JSON file} | ||
{--api=default : The API to export a documentation for} | ||
'; | ||
|
||
protected $description = 'Export the OpenAPI document to a JSON file.'; | ||
|
||
public function handle(Generator $generator): void | ||
{ | ||
$config = Scramble::getGeneratorConfig($api = $this->option('api')); | ||
|
||
$specification = json_encode($generator($config)); | ||
|
||
/** @var string $filename */ | ||
$filename = $this->option('path') ?? $config->get('export_path', 'api'.($api === 'default' ? '' : "-$api").'.json'); | ||
|
||
File::put($filename, $specification); | ||
|
||
$this->info("OpenAPI document exported to {$filename}."); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble; | ||
|
||
use Closure; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
|
||
class GeneratorConfig | ||
{ | ||
public function __construct( | ||
private array $config = [], | ||
private ?Closure $routeResolver = null, | ||
private ?Closure $afterOpenApiGenerated = null, | ||
) { | ||
$this->routeResolver = $this->routeResolver ?? function (Route $route) { | ||
$expectedDomain = $this->get('api_domain'); | ||
|
||
return Str::startsWith($route->uri, $this->get('api_path', 'api')) | ||
&& (! $expectedDomain || $route->getDomain() === $expectedDomain); | ||
}; | ||
} | ||
|
||
public function config(array $config) | ||
{ | ||
$this->config = $config; | ||
|
||
return $this; | ||
} | ||
|
||
public function routes(?Closure $routeResolver = null) | ||
{ | ||
if (count(func_get_args()) === 0) { | ||
return $this->routeResolver; | ||
} | ||
|
||
$this->routeResolver = $routeResolver; | ||
|
||
return $this; | ||
} | ||
|
||
public function afterOpenApiGenerated(?Closure $afterOpenApiGenerated = null) | ||
{ | ||
if (count(func_get_args()) === 0) { | ||
return $this->afterOpenApiGenerated; | ||
} | ||
|
||
$this->afterOpenApiGenerated = $afterOpenApiGenerated; | ||
|
||
return $this; | ||
} | ||
|
||
public function get(string $key, mixed $default = null) | ||
{ | ||
return Arr::get($this->config, $key, $default); | ||
} | ||
} |
Oops, something went wrong.