Skip to content

Commit 3417131

Browse files
authored
Merge pull request #523 from mpociot/update-config
Switch to `base_url` config var for base URL in docs and collection
2 parents fc16f27 + c127319 commit 3417131

File tree

13 files changed

+82
-29
lines changed

13 files changed

+82
-29
lines changed

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
- Add tests on output (deterministic)
44
- Bring `bindings` outside of `response_calls`
55
- Should `routes.*.apply.response_calls.headers` be replaced by `routes.*.apply.headers`?
6+
- Implement debug flag

config/apidoc.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
*/
1414
'router' => 'laravel',
1515

16+
/*
17+
* The base URL to be used in examples and the Postman collection.
18+
* By default, this will be the value of config('app.url').
19+
*/
20+
'base_url' => config('app.url'),
21+
1622
/*
1723
* Generate a Postman collection in addition to HTML docs.
1824
*/

docs/config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ This is the file path where the generated documentation will be written to. Note
88
## `router`
99
The router to use when processing your routes (can be Laravel or Dingo. Defaults to **Laravel**)
1010

11+
## `base_url`
12+
The base URL to be used in examples and the Postman collection. By default, this will be the value of config('app.url').
13+
1114
## `postman`
1215
This package can automatically generate a Postman collection for your routes, along with the documentation. This section is where you can configure (or disable) that.
1316

resources/views/partials/example-requests/bash.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
```bash
2-
curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ trim(config('app.docs_url') ?: config('app.url'), '/')}}/{{ ltrim($route['boundUri'], '/') }}" @if(count($route['headers']))\
2+
curl -X {{$route['methods'][0]}} {{$route['methods'][0] == 'GET' ? '-G ' : ''}}"{{ rtrim($baseUrl, '/')}}/{{ ltrim($route['boundUri'], '/') }}" @if(count($route['headers']))\
33
@foreach($route['headers'] as $header => $value)
44
-H "{{$header}}: {{$value}}"@if(! ($loop->last) || ($loop->last && count($route['bodyParameters']))) \
55
@endif

resources/views/partials/example-requests/javascript.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
```javascript
2-
const url = new URL("{{ rtrim(config('app.docs_url') ?: config('app.url'), '/') }}/{{ ltrim($route['boundUri'], '/') }}");
2+
const url = new URL("{{ rtrim($baseUrl, '/') }}/{{ ltrim($route['boundUri'], '/') }}");
33
@if(count($route['queryParameters']))
44

55
let params = {

resources/views/partials/example-requests/php.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
```php
22

33
$client = new \GuzzleHttp\Client();
4-
$response = $client->{{ strtolower($route['methods'][0]) }}("{{ $route['boundUri'] }}", [
4+
$response = $client->{{ strtolower($route['methods'][0]) }}("{{ rtrim($baseUrl, '/') . '/' . $route['boundUri'] }}", [
55
@if(!empty($route['headers']))
66
'headers' => [
77
@foreach($route['headers'] as $header => $value)

src/Commands/GenerateDocumentation.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Mpociot\ApiDoc\Tools\RouteMatcher;
1515
use Mpociot\Documentarian\Documentarian;
1616
use Mpociot\ApiDoc\Postman\CollectionWriter;
17+
use Mpociot\ApiDoc\Tools\DocumentationConfig;
1718

1819
class GenerateDocumentation extends Command
1920
{
@@ -35,6 +36,11 @@ class GenerateDocumentation extends Command
3536

3637
private $routeMatcher;
3738

39+
/**
40+
* @var DocumentationConfig
41+
*/
42+
private $docConfig;
43+
3844
public function __construct(RouteMatcher $routeMatcher)
3945
{
4046
parent::__construct();
@@ -48,20 +54,23 @@ public function __construct(RouteMatcher $routeMatcher)
4854
*/
4955
public function handle()
5056
{
57+
$this->docConfig = new DocumentationConfig(config('apidoc'));
58+
5159
try {
52-
URL::forceRootUrl(config('app.url'));
60+
URL::forceRootUrl($this->docConfig->get('base_url'));
5361
} catch (\Exception $e) {
54-
echo "Warning: Couldn't force base url as Lumen currently doesn't have the forceRootUrl method.\n";
62+
echo "Warning: Couldn't force base url as your version of Lumen doesn't have the forceRootUrl method.\n";
5563
echo "You should probably double check URLs in your generated documentation.\n";
5664
}
57-
$usingDingoRouter = strtolower(config('apidoc.router')) == 'dingo';
65+
$usingDingoRouter = strtolower($this->docConfig->get('router')) == 'dingo';
66+
$routes = $this->docConfig->get('routes');
5867
if ($usingDingoRouter) {
59-
$routes = $this->routeMatcher->getDingoRoutesToBeDocumented(config('apidoc.routes'));
68+
$routes = $this->routeMatcher->getDingoRoutesToBeDocumented($routes);
6069
} else {
61-
$routes = $this->routeMatcher->getLaravelRoutesToBeDocumented(config('apidoc.routes'));
70+
$routes = $this->routeMatcher->getLaravelRoutesToBeDocumented($routes);
6271
}
6372

64-
$generator = new Generator(config('apidoc.faker_seed'));
73+
$generator = new Generator($this->docConfig);
6574
$parsedRoutes = $this->processRoutes($generator, $routes);
6675
$parsedRoutes = collect($parsedRoutes)->groupBy('group')
6776
->sortBy(static function ($group) {
@@ -79,7 +88,7 @@ public function handle()
7988
*/
8089
private function writeMarkdown($parsedRoutes)
8190
{
82-
$outputPath = config('apidoc.output');
91+
$outputPath = $this->docConfig->get('output');
8392
$targetFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'index.md';
8493
$compareFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'.compare.md';
8594
$prependFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'prepend.md';
@@ -89,7 +98,7 @@ private function writeMarkdown($parsedRoutes)
8998
->with('outputPath', ltrim($outputPath, 'public/'))
9099
->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection());
91100

92-
$settings = ['languages' => config('apidoc.example_languages')];
101+
$settings = ['languages' => $this->docConfig->get('example_languages')];
93102
$parsedRouteOutput = $parsedRoutes->map(function ($routeGroup) use ($settings) {
94103
return $routeGroup->map(function ($route) use ($settings) {
95104
if (count($route['cleanBodyParameters']) && ! isset($route['headers']['Content-Type'])) {
@@ -98,6 +107,7 @@ private function writeMarkdown($parsedRoutes)
98107
$route['output'] = (string) view('apidoc::partials.route')
99108
->with('route', $route)
100109
->with('settings', $settings)
110+
->with('baseUrl', $this->docConfig->get('base_url'))
101111
->render();
102112

103113
return $route;
@@ -150,7 +160,7 @@ private function writeMarkdown($parsedRoutes)
150160
->with('infoText', $infoText)
151161
->with('prependMd', $prependFileContents)
152162
->with('appendMd', $appendFileContents)
153-
->with('outputPath', config('apidoc.output'))
163+
->with('outputPath', $this->docConfig->get('output'))
154164
->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection())
155165
->with('parsedRoutes', $parsedRouteOutput);
156166

@@ -168,7 +178,7 @@ private function writeMarkdown($parsedRoutes)
168178
->with('infoText', $infoText)
169179
->with('prependMd', $prependFileContents)
170180
->with('appendMd', $appendFileContents)
171-
->with('outputPath', config('apidoc.output'))
181+
->with('outputPath', $this->docConfig->get('output'))
172182
->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection())
173183
->with('parsedRoutes', $parsedRouteOutput);
174184

@@ -188,7 +198,7 @@ private function writeMarkdown($parsedRoutes)
188198
file_put_contents($outputPath.DIRECTORY_SEPARATOR.'collection.json', $this->generatePostmanCollection($parsedRoutes));
189199
}
190200

191-
if ($logo = config('apidoc.logo')) {
201+
if ($logo = $this->docConfig->get('logo')) {
192202
copy(
193203
$logo,
194204
$outputPath.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'logo.png'
@@ -274,7 +284,7 @@ private function isRouteVisibleForDocumentation($action)
274284
*/
275285
private function generatePostmanCollection(Collection $routes)
276286
{
277-
$writer = new CollectionWriter($routes);
287+
$writer = new CollectionWriter($routes, $this->docConfig->get('base_url'));
278288

279289
return $writer->getCollection();
280290
}
@@ -286,6 +296,6 @@ private function generatePostmanCollection(Collection $routes)
286296
*/
287297
private function shouldGeneratePostmanCollection()
288298
{
289-
return config('apidoc.postman.enabled', is_bool(config('apidoc.postman')) ? config('apidoc.postman') : false);
299+
return $this->docConfig->get('postman.enabled', is_bool($this->docConfig->get('postman')) ? $this->docConfig->get('postman') : false);
290300
}
291301
}

src/Postman/CollectionWriter.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,30 @@ class CollectionWriter
1313
*/
1414
private $routeGroups;
1515

16+
/**
17+
* @var string
18+
*/
19+
private $baseUrl;
20+
1621
/**
1722
* CollectionWriter constructor.
1823
*
1924
* @param Collection $routeGroups
2025
*/
21-
public function __construct(Collection $routeGroups)
26+
public function __construct(Collection $routeGroups, $baseUrl)
2227
{
2328
$this->routeGroups = $routeGroups;
29+
$this->baseUrl = $baseUrl;
2430
}
2531

2632
public function getCollection()
2733
{
28-
URL::forceRootUrl(config('app.url'));
34+
try {
35+
URL::forceRootUrl($this->baseUrl);
36+
} catch (\Exception $e) {
37+
echo "Warning: Couldn't force base url as your version of Lumen doesn't have the forceRootUrl method.\n";
38+
echo "You should probably double check URLs in your generated Postman collection.\n";
39+
}
2940

3041
$collection = [
3142
'variables' => [],

src/Tools/DocumentationConfig.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Mpociot\ApiDoc\Tools;
4+
5+
class DocumentationConfig
6+
{
7+
private $data;
8+
9+
public function __construct(array $config = [])
10+
{
11+
$this->data = $config;
12+
}
13+
14+
public function get($key, $default = null)
15+
{
16+
return data_get($this->data, $key, $default);
17+
}
18+
}

src/Tools/Generator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class Generator
1515
use ParamHelpers;
1616

1717
/**
18-
* @var string The seed to be used with Faker.
19-
* Useful when you want to always have the same fake output.
18+
* @var DocumentationConfig
2019
*/
21-
private $fakerSeed = null;
20+
private $config;
2221

23-
public function __construct(string $fakerSeed = null)
22+
public function __construct(DocumentationConfig $config = null)
2423
{
25-
$this->fakerSeed = $fakerSeed;
24+
// If no config is injected, pull from global
25+
$this->config = $config ?: new DocumentationConfig(config('apidoc'));
2626
}
2727

2828
/**
@@ -296,7 +296,7 @@ protected function getRouteGroup(ReflectionClass $controller, ReflectionMethod $
296296
}
297297
}
298298

299-
return config('apidoc.default_group', 'general');
299+
return $this->config->get(('default_group'));
300300
}
301301

302302
private function normalizeParameterType($type)
@@ -313,8 +313,8 @@ private function normalizeParameterType($type)
313313
private function generateDummyValue(string $type)
314314
{
315315
$faker = Factory::create();
316-
if ($this->fakerSeed) {
317-
$faker->seed($this->fakerSeed);
316+
if ($this->config->get('faker_seed')) {
317+
$faker->seed($this->config->get('faker_seed'));
318318
}
319319
$fakeFactories = [
320320
'integer' => function () use ($faker) {

0 commit comments

Comments
 (0)