Skip to content

Commit d7847a4

Browse files
authored
Merge pull request #608 from mpociot/non-static-docs
Add options for non static docs
2 parents 113ced0 + 4eecf10 commit d7847a4

30 files changed

+645
-421
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ composer.lock
33
.php_cs.cache
44
/vendor/
55
public/
6+
resources/docs/
7+
resources/views/apidoc/
68
tests/public/
79
.idea/
810
coverage.xml
911
results.xml
1012
docs/_build
1113
docs/make.bat
14+
tests/public/docs/
15+
tests/resources/**

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
## [4.0.0]
1717
### Added
18+
- Support for Eloquent API resources (https://github.com/mpociot/laravel-apidoc-generator/pull/601)
1819
- `bindings` replaced by `@urlParam` annotation (https://github.com/mpociot/laravel-apidoc-generator/pull/599)
1920
- Better support for arrays and objects in bodyParams (https://github.com/mpociot/laravel-apidoc-generator/pull/597)
2021

22+
### Modified
23+
- Made ResponseCalls strategy only execute if no successful responses exist. (https://github.com/mpociot/laravel-apidoc-generator/pull/605)
24+
- Hide null responses in examples. (https://github.com/mpociot/laravel-apidoc-generator/pull/605)
25+
- Made `responses` stage additive (https://github.com/mpociot/laravel-apidoc-generator/pull/605)
26+
- Renamed `query` and `body` in `response_calls` config to `queryParams` and `bodyParams` (https://github.com/mpociot/laravel-apidoc-generator/pull/603)
27+
28+
### Removed
29+
- Removed `apply.response_calls.headers` in favour of `apply.headers` (https://github.com/mpociot/laravel-apidoc-generator/pull/603)
30+
- Removed bindings in response_calls (https://github.com/mpociot/laravel-apidoc-generator/pull/599)
31+
2132
## [3.17.1] - Thursday, 12 September 2019
2233
### Fixed
2334
- ResponseCalls: Call Lumen application correctly since it does not use HttpKernel (https://github.com/mpociot/laravel-apidoc-generator/pull/585)

config/apidoc.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22

33
return [
4-
54
/*
6-
* The output path for the generated documentation.
7-
* This path should be relative to the root of your application.
5+
* The type of documentation output to generate.
6+
* - "static" will generate a static HTMl page in the /public/docs folder,
7+
* - "laravel" will generate the documentation as a Blade view,
8+
* so you can add routing and authentication.
89
*/
9-
'output' => 'public/docs',
10+
'type' => 'static',
1011

1112
/*
1213
* The router to be used (Laravel or Dingo).
@@ -21,6 +22,9 @@
2122

2223
/*
2324
* Generate a Postman collection in addition to HTML docs.
25+
* For 'static' docs, the collection will be generated to public/docs/collection.json.
26+
* For 'laravel' docs, it will be generated to storage/app/apidoc/collection.json.
27+
* The `ApiDoc::routes()` helper will add routes for both the HTML and the Postman collection.
2428
*/
2529
'postman' => [
2630
/*
@@ -160,23 +164,23 @@
160164

161165
'strategies' => [
162166
'metadata' => [
163-
\Mpociot\ApiDoc\Strategies\Metadata\GetFromDocBlocks::class,
167+
\Mpociot\ApiDoc\Extracting\Strategies\Metadata\GetFromDocBlocks::class,
164168
],
165169
'urlParameters' => [
166-
\Mpociot\ApiDoc\Strategies\UrlParameters\GetFromUrlParamTag::class,
170+
\Mpociot\ApiDoc\Extracting\Strategies\UrlParameters\GetFromUrlParamTag::class,
167171
],
168172
'queryParameters' => [
169-
\Mpociot\ApiDoc\Strategies\QueryParameters\GetFromQueryParamTag::class,
173+
\Mpociot\ApiDoc\Extracting\Strategies\QueryParameters\GetFromQueryParamTag::class,
170174
],
171175
'bodyParameters' => [
172-
\Mpociot\ApiDoc\Strategies\BodyParameters\GetFromBodyParamTag::class,
176+
\Mpociot\ApiDoc\Extracting\Strategies\BodyParameters\GetFromBodyParamTag::class,
173177
],
174178
'responses' => [
175-
\Mpociot\ApiDoc\Strategies\Responses\UseResponseTag::class,
176-
\Mpociot\ApiDoc\Strategies\Responses\UseResponseFileTag::class,
177-
\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,
178-
\Mpociot\ApiDoc\Strategies\Responses\UseTransformerTags::class,
179-
\Mpociot\ApiDoc\Strategies\Responses\ResponseCalls::class,
179+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseResponseTag::class,
180+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseResponseFileTag::class,
181+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseApiResourceTags::class,
182+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseTransformerTags::class,
183+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\ResponseCalls::class,
180184
],
181185
],
182186

docs/config.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22

33
Before you can generate your documentation, you'll need to configure a few things in your `config/apidoc.php`. If you aren't sure what an option does, it's best to leave it set to the default. If you don't have this config file, see the [installation instructions](index.html#installation).
44

5-
## `output`
6-
This is the file path where the generated documentation will be written to. Note that the documentation is generated as static HTML and CSS assets, so the route is accessed directly, and not via the Laravel routing mechanism. This path should be relative to the root of your application. Default: **public/docs**
5+
## `type`
6+
This is the type of documentation output to generate.
7+
- `static` will generate a static HTMl page in the `public/docs` folder, so anyone can visit your documentation page by going to {yourapp.domain}/docs.
8+
- `laravel` will generate the documentation as a Blade view within the `resources/views/apidoc` folder, so you can add routing and authentication.
9+
10+
If you're using `laravel` type, you can call `\Mpociot\ApiDoc\ApiDoc::routes()` from your routes file (usually `routes/web.php`). This method will create a `/doc` route for your documentation, along with a `/doc.json` variant that will return the Postman collection, if you have that enabled. This method returns the route, so you can call additional methods to customise it (by adding middleware, for instance). You can also pass in the path you'd like to use instead.
11+
12+
```php
13+
\Mpociot\ApiDoc\ApiDoc::routes("/apidoc")->middleware("auth.basic");
14+
```
15+
> Note: There is currently a known issue with using `/docs` as the path for `laravel` docs. You should not use it, as it conflicts with the folder structure in the `public` folder and may confuse the webserver.
16+
17+
You may, of course, set up your own routing instead of using the `routes()` helper.
718

819
## `router`
920
The router to use when processing your routes (can be Laravel or Dingo. Defaults to **Laravel**)
@@ -13,6 +24,8 @@ The base URL to be used in examples and the Postman collection. By default, this
1324

1425
## `postman`
1526
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.
27+
- For `static` docs (see [type](#type)), the collection will be created in `public/docs/collection.json`, so it can be accessed by visiting {yourapp.domain}/docs/colllection.json.
28+
- For `laravel` docs, the collection will be generated to `storage/app/apidoc/collection.json`. The `ApiDoc::routes()` helper will add a `/docs.json` endpoint to fetch it..
1629

1730
### `enabled`
1831
Whether or not to generate a Postman API collection. Default: **true**

docs/migrating.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Migrating
2+
Rename your old config file. Publish the config file

docs/plugins.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ There are a number of strategies inccluded with the package, so you don't have t
1616
> Note: The included ResponseCalls strategy is designed to stop if a response with a 2xx status code has already been gotten via any other strategy.
1717
1818
## Strategies
19-
To create a strategy, create a class that extends `\Mpociot\ApiDoc\Strategies\Strategy`.
19+
To create a strategy, create a class that extends `\Mpociot\ApiDoc\Extracting\Strategies\Strategy`.
2020

2121
The `__invoke` method of the strategy is where you perform your actions and return data. It receives the following arguments:
2222
- the route (instance of `\Illuminate\Routing\Route`)
@@ -31,7 +31,7 @@ The `__invoke` method of the strategy is where you perform your actions and retu
3131
<?php
3232

3333
use Illuminate\Routing\Route;
34-
use Mpociot\ApiDoc\Strategies\Strategy;
34+
use Mpociot\ApiDoc\Extracting\Strategies\Strategy;
3535

3636
class AddOrganizationIdBodyParameter extends Strategy
3737
{
@@ -55,23 +55,23 @@ The last thing to do is to register the strategy. Strategies are registered in a
5555
...
5656
'strategies' => [
5757
'metadata' => [
58-
\Mpociot\ApiDoc\Strategies\Metadata\GetFromDocBlocks::class,
58+
\Mpociot\ApiDoc\Extracting\Strategies\Metadata\GetFromDocBlocks::class,
5959
],
6060
'urlParameters' => [
61-
\Mpociot\ApiDoc\Strategies\UrlParameters\GetFromUrlParamTag::class,
61+
\Mpociot\ApiDoc\Extracting\Strategies\UrlParameters\GetFromUrlParamTag::class,
6262
],
6363
'queryParameters' => [
64-
\Mpociot\ApiDoc\Strategies\QueryParameters\GetFromQueryParamTag::class,
64+
\Mpociot\ApiDoc\Extracting\Strategies\QueryParameters\GetFromQueryParamTag::class,
6565
],
6666
'bodyParameters' => [
67-
\Mpociot\ApiDoc\Strategies\BodyParameters\GetFromBodyParamTag::class,
67+
\Mpociot\ApiDoc\Extracting\Strategies\BodyParameters\GetFromBodyParamTag::class,
6868
],
6969
'responses' => [
70-
\Mpociot\ApiDoc\Strategies\Responses\UseResponseTag::class,
71-
\Mpociot\ApiDoc\Strategies\Responses\UseResponseFileTag::class,
72-
\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,
73-
\Mpociot\ApiDoc\Strategies\Responses\UseTransformerTags::class,
74-
\Mpociot\ApiDoc\Strategies\Responses\ResponseCalls::class,
70+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseResponseTag::class,
71+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseResponseFileTag::class,
72+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseApiResourceTags::class,
73+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\UseTransformerTags::class,
74+
\Mpociot\ApiDoc\Extracting\Strategies\Responses\ResponseCalls::class,
7575
],
7676
],
7777
...
@@ -82,7 +82,7 @@ You can add, replace or remove strategies from here. In our case, we're adding o
8282
```php
8383

8484
'bodyParameters' => [
85-
\Mpociot\ApiDoc\Strategies\BodyParameters\GetFromBodyParamTag::class,
85+
\Mpociot\ApiDoc\Extracting\Strategies\BodyParameters\GetFromBodyParamTag::class,
8686
AddOrganizationIdBodyParameter::class,
8787
],
8888
```
@@ -124,9 +124,9 @@ You are also provided with the instance pproperty `stage`, which is set to the n
124124
## Utilities
125125
You have access to a number of tools when developing strategies. They include:
126126

127-
- The `RouteDocBlocker` class (in the `\Mpociot\ApiDoc\Tools` namespace) has a single public static method, `getDocBlocksFromRoute(Route $route)`. It allows you to retrieve the docblocks for a given route. It returns an array of with two keys: `method` and `class` containing the docblocks for the method and controller handling the route respectively. Both are instances of `\Mpociot\Reflection\DocBlock`.
127+
- The `RouteDocBlocker` class (in the `\Mpociot\ApiDoc\Extracting` namespace) has a single public static method, `getDocBlocksFromRoute(Route $route)`. It allows you to retrieve the docblocks for a given route. It returns an array of with two keys: `method` and `class` containing the docblocks for the method and controller handling the route respectively. Both are instances of `\Mpociot\Reflection\DocBlock`.
128128

129-
- The `ParamsHelper` trait (in the `\Mpociot\ApiDoc\Tools` namespace) can be included in your strategies. It contains a number of useful methods for working with parameters, including type casting and generating dummy values.
129+
- The `ParamsHelper` trait (in the `\Mpociot\ApiDoc\Extracting` namespace) can be included in your strategies. It contains a number of useful methods for working with parameters, including type casting and generating dummy values.
130130

131131
## API
132132
Each strategy class must implement the __invoke method with the parameters as described above. This method must return the needed data for the intended stage, or `null` to indicate failure.

src/ApiDoc.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Mpociot\ApiDoc;
4+
5+
use Illuminate\Support\Facades\Route;
6+
use Illuminate\Support\Facades\Storage;
7+
8+
class ApiDoc
9+
{
10+
public static function routes($path = '/doc')
11+
{
12+
return Route::get("$path{format?}", function (?string $format = null) {
13+
if ($format == '.json') {
14+
return response(
15+
Storage::disk('local')->get('apidoc/collection.json'),
16+
200,
17+
['Content-type' => 'application/json']
18+
19+
);
20+
}
21+
22+
return view('apidoc.index');
23+
})->name('apidoc');
24+
}
25+
}

0 commit comments

Comments
 (0)