Skip to content

Commit 63511df

Browse files
authored
Merge pull request #177 from rakutentech/feature/types
Use native types
2 parents b0b6771 + 717dfdf commit 63511df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1913
-889
lines changed

.github/workflows/phplint.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
on: [push]
2+
3+
name: "CI PHP"
4+
5+
jobs:
6+
test:
7+
8+
runs-on: ubuntu-latest
9+
10+
name: PHP lint
11+
12+
steps:
13+
- name: Cancel Previous Runs
14+
uses: styfle/[email protected]
15+
with:
16+
access_token: ${{ github.token }}
17+
18+
- name: Checkout
19+
uses: actions/checkout@v2
20+
21+
- name: Install PHP with extensions
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
coverage: pcov
25+
extensions: intl, gd, zip, pdo, sqlite, pdo_sqlite, dom, curl, libxml, mbstring, fileinfo, exif, iconv
26+
ini-values: memory_limit=-1,disable_functions="",pcov.exclude="~(vendor|tests|node_modules)~",pcov.directory="./"
27+
php-version: 8.2
28+
tools: composer:v2
29+
30+
- name: Composer Install
31+
run: |
32+
composer require --no-interaction --no-update
33+
composer install
34+
35+
- name: PHPLint
36+
run: |
37+
./vendor/bin/phpcs
38+
39+
- name: PHPStan
40+
run: |
41+
./vendor/bin/phpstan analyse

.github/workflows/phptest.yml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,33 @@ name: "CI PHP"
44

55
jobs:
66
test:
7-
name: Test
87

98
runs-on: ubuntu-latest
109

1110
strategy:
1211
matrix:
13-
php-versions: [7.4, 8.0.6, 8.1, 8.2]
12+
php: [7.4, 8.0, 8.1, 8.2]
13+
laravel: [8.*]
14+
include:
15+
- php: 8.0
16+
laravel: 9.*
17+
- php: 8.1
18+
laravel: 9.*
19+
- php: 8.2
20+
laravel: 9.*
21+
- php: 8.1
22+
laravel: 10.*
23+
- php: 8.2
24+
laravel: 10.*
25+
26+
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
1427

1528
steps:
1629
- name: Cancel Previous Runs
1730
uses: styfle/[email protected]
1831
with:
19-
access_token: ${{ github.token }}
32+
access_token: ${{ github.token }}
33+
2034
- name: Checkout
2135
uses: actions/checkout@v2
2236

@@ -26,22 +40,20 @@ jobs:
2640
coverage: pcov
2741
extensions: intl, gd, zip, pdo, sqlite, pdo_sqlite, dom, curl, libxml, mbstring, fileinfo, exif, iconv
2842
ini-values: memory_limit=-1,disable_functions="",pcov.exclude="~(vendor|tests|node_modules)~",pcov.directory="./"
29-
php-version: ${{ matrix.php-versions }}
43+
php-version: ${{ matrix.php }}
3044
tools: composer:v2
3145

3246
- name: Composer Install
33-
run: composer install
47+
run: |
48+
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
49+
composer install
3450
3551
- name: PHPUNIT Tests
3652
run: |
3753
./vendor/bin/phpunit --coverage-clover=coverage.xml
3854
39-
- name: PHPLint
40-
run: |
41-
./vendor/bin/phpcs --standard=phpcs.xml --extensions=php --ignore=tests/migrations config/ src/
42-
4355
- name: Upload to codecov
4456
uses: codecov/codecov-action@v3
4557
with:
46-
name: PHP ${{ matrix.php-versions }}
58+
name: PHP ${{ matrix.php }}
4759
files: coverage.xml

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
Supports Open API 3.0.0
1111
</p>
1212

13+
<p align="center">
14+
<img src="https://github.com/rakutentech/laravel-request-docs/actions/workflows/node.yml/badge.svg?branch=master" alt="CI Node">
15+
<img src="https://github.com/rakutentech/laravel-request-docs/actions/workflows/phptest.yml/badge.svg?branch=master" alt="CI PHP">
16+
<a href="https://codecov.io/gh/rakutentech/laravel-request-docs"><img src="https://codecov.io/gh/rakutentech/laravel-request-docs/branch/master/graph/badge.svg?token=U6ZRDPY6QZ" alt="codecov"></a>
17+
<a href="https://packagist.org/packages/rakutentech/laravel-request-docs"><img src="https://poser.pugx.org/rakutentech/laravel-request-docs/v/stable.png" alt="Latest Stable Version"></a>
18+
<a href="https://packagist.org/packages/rakutentech/laravel-request-docs"><img src="http://poser.pugx.org/rakutentech/laravel-request-docs/downloads" alt="Total Downloads"></a>
19+
<a href="LICENSE.md"><img src="https://poser.pugx.org/rakutentech/laravel-request-docs/license.png" alt="License"></a>
20+
</p>
21+
1322
**Fast:** Install on any Laravel Project
1423

1524
**Hassle Free:** Auto Generate API Documentation for request rules and parameters
@@ -52,7 +61,7 @@ Read more: https://medium.com/web-developer/laravel-automatically-generate-api-d
5261
You can install the package via composer:
5362

5463
```bash
55-
composer require rakutentech/laravel-request-docs --dev
64+
composer require rakutentech/laravel-request-docs
5665
```
5766

5867

@@ -63,6 +72,18 @@ php artisan vendor:publish --tag=request-docs-config
6372
php artisan route:cache
6473
```
6574

75+
(optional) Add following middleware to your API, so that the sql logs, model events are captured.
76+
77+
`app/Http/Kernel.php`
78+
79+
```sh
80+
'api' => [
81+
...
82+
\Rakutentech\LaravelRequestDocs\LaravelRequestDocsMiddleware::class,
83+
... and so on
84+
85+
```
86+
6687
# Usage
6788
6889
## Dashboard

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"brianium/paratest": "^6.2",
2828
"friendsofphp/php-cs-fixer": "^3.5",
2929
"nunomaduro/collision": "^5.3|^6.0",
30+
"nunomaduro/larastan": "^1.0|^2.4",
3031
"orchestra/testbench": "^6.15|^7.0|^8.0",
3132
"phpunit/phpunit": "^9.3",
3233
"spatie/laravel-ray": "^1.23",
@@ -44,10 +45,10 @@
4445
}
4546
},
4647
"scripts": {
47-
"psalm": "vendor/bin/psalm",
48-
"phpcs": "vendor/bin/phpcs",
49-
"test": "./vendor/bin/testbench package:test --parallel --no-coverage",
50-
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
48+
"phpstan": "phpstan analyse",
49+
"phpcs": "phpcs",
50+
"test": "testbench package:test --parallel --no-coverage",
51+
"test-coverage": "phpunit --coverage-html coverage"
5152
},
5253
"config": {
5354
"sort-packages": true

config/request-docs.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
return [
4+
'enabled' => true,
45
// change it to true will make lrd to throw exception if rules in request class need to be changed
56
// keep it false
67
'debug' => false,
@@ -37,7 +38,7 @@
3738
'rules_methods' => [
3839
'rules'
3940
],
40-
// Can be overridden as // @LRDResponses 200|400|401
41+
// Can be overridden as // @LRDresponses 200|400|401
4142
'default_responses' => [ "200", "400", "401", "403", "404", "405", "422", "429", "500", "503"],
4243

4344
// By default, LRD group your routes by the first /path.

phpcs.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
<file>src</file>
55
<file>tests</file>
66

7-
<exclude-pattern type="relative-root">tests/*</exclude-pattern>
87
<rule ref="PSR2"/>
98
<rule ref="Generic.Files.LineLength">
109
<properties>
1110
<property name="lineLimit" value="210"/>
1211
<property name="absoluteLineLimit" value="0"/>
1312
</properties>
1413
</rule>
14+
15+
<!-- Align corresponding assignment statement tokens -->
16+
<rule ref="Generic.Formatting.MultipleStatementAlignment">
17+
<properties>
18+
<property name="error" value="true"/>
19+
</properties>
20+
</rule>
1521
</ruleset>

phpstan.neon

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
includes:
2+
- ./vendor/nunomaduro/larastan/extension.neon
3+
4+
parameters:
5+
6+
paths:
7+
- src
8+
- tests
9+
10+
# The level 8 is the highest level
11+
level: 5
12+
13+
# ignoreErrors:
14+
# - '#PHPDoc tag @var#'
15+
#
16+
# excludePaths:
17+
# - ./*/*/FileToBeExcluded.php
18+
19+
checkMissingIterableValueType: false

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<env name="SESSION_DRIVER" value="array"/>
1717
<env name="QUEUE_DRIVER" value="sync"/>
1818
<env name="MAIL_DRIVER" value="log"/>
19-
<env name="DB_CONNECTION" value="sqlite_testing"/>
19+
<env name="DB_CONNECTION" value="sqlite"/>
2020
<env name="DB_DATABASE" value=":memory:"/>
2121
</php>
2222
</phpunit>

src/Controllers/LaravelRequestDocsController.php

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,70 @@
22

33
namespace Rakutentech\LaravelRequestDocs\Controllers;
44

5+
use Illuminate\Http\JsonResponse;
56
use Illuminate\Http\Request;
67
use Illuminate\Http\Response;
8+
use Illuminate\Routing\Controller;
79
use Rakutentech\LaravelRequestDocs\LaravelRequestDocs;
810
use Rakutentech\LaravelRequestDocs\LaravelRequestDocsToOpenApi;
9-
use Illuminate\Routing\Controller;
11+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1012

1113
class LaravelRequestDocsController extends Controller
1214
{
13-
private $laravelRequestDocs;
15+
private LaravelRequestDocs $laravelRequestDocs;
16+
private LaravelRequestDocsToOpenApi $laravelRequestDocsToOpenApi;
1417

1518
public function __construct(LaravelRequestDocs $laravelRequestDoc, LaravelRequestDocsToOpenApi $laravelRequestDocsToOpenApi)
1619
{
17-
$this->laravelRequestDocs = $laravelRequestDoc;
20+
$this->laravelRequestDocs = $laravelRequestDoc;
1821
$this->laravelRequestDocsToOpenApi = $laravelRequestDocsToOpenApi;
1922
}
2023

21-
public function index(Request $request)
24+
/**
25+
* @codeCoverageIgnore
26+
*/
27+
public function index(Request $request): Response
2228
{
23-
return view('request-docs::index');
29+
return response()->view('request-docs::index');
2430
}
25-
public function api(Request $request)
26-
{
27-
$docs = $this->laravelRequestDocs->getDocs();
28-
$docs = $this->laravelRequestDocs->sortDocs($docs, $request->sort);
29-
$docs = $this->laravelRequestDocs->groupDocs($docs, $request->groupby);
3031

31-
$showGet = $request->has('showGet') ? $request->showGet == 'true' : true;
32-
$showPost = $request->has('showPost') ? $request->showPost == 'true' : true;
33-
$showPut = $request->has('showPut') ? $request->showPut == 'true' : true;
34-
$showPatch = $request->has('showPatch') ? $request->showPatch == 'true' : true;
35-
$showDelete = $request->has('showDelete') ? $request->showDelete == 'true' : true;
36-
$showHead = $request->has('showHead') ? $request->showHead == 'true' : true;
32+
/**
33+
* @throws \ReflectionException
34+
* @throws \Throwable
35+
*/
36+
public function api(Request $request): JsonResponse
37+
{
38+
$showGet = !$request->has('showGet') || $request->input('showGet') === 'true';
39+
$showPost = !$request->has('showPost') || $request->input('showPost') == 'true';
40+
$showPut = !$request->has('showPut') || $request->input('showPut') === 'true';
41+
$showPatch = !$request->has('showPatch') || $request->input('showPatch') === 'true';
42+
$showDelete = !$request->has('showDelete') || $request->input('showDelete') === 'true';
43+
$showHead = !$request->has('showHead') || $request->input('showHead') === 'true';
3744

38-
$docs = $this->laravelRequestDocs->filterByMethods(
39-
$docs,
45+
// Get a list of Doc with route and rules information.
46+
// If user defined `Route::match(['get', 'post'], 'uri', ...)`,
47+
// only a single Doc will be generated.
48+
$docs = $this->laravelRequestDocs->getDocs(
4049
$showGet,
4150
$showPost,
4251
$showPut,
4352
$showPatch,
4453
$showDelete,
45-
$showHead
54+
$showHead,
4655
);
47-
if ($request->openapi) {
56+
57+
// Loop and split Doc by the `methods` property.
58+
// `Route::match([...n], 'uri', ...)` will generate n number of Doc.
59+
$docs = $this->laravelRequestDocs->splitByMethods($docs);
60+
$docs = $this->laravelRequestDocs->sortDocs($docs, $request->input('sort'));
61+
$docs = $this->laravelRequestDocs->groupDocs($docs, $request->input('groupby'));
62+
63+
if ($request->input('openapi')) {
4864
return response()->json(
49-
$this->laravelRequestDocsToOpenApi->openApi($docs)->toArray(),
65+
$this->laravelRequestDocsToOpenApi->openApi($docs->all())->toArray(),
5066
Response::HTTP_OK,
5167
[
52-
'Content-type'=> 'application/json; charset=utf-8'
68+
'Content-type' => 'application/json; charset=utf-8'
5369
],
5470
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
5571
);
@@ -59,12 +75,17 @@ public function api(Request $request)
5975
$docs,
6076
Response::HTTP_OK,
6177
[
62-
'Content-type'=> 'application/json; charset=utf-8',
78+
'Content-type' => 'application/json; charset=utf-8',
6379
],
6480
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
6581
);
6682
}
6783

84+
/**
85+
* @codeCoverageIgnore
86+
* @param \Illuminate\Http\Request $request
87+
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse|\Illuminate\Http\JsonResponse
88+
*/
6889
public function assets(Request $request)
6990
{
7091
$path = explode('/', $request->path());
@@ -95,7 +116,7 @@ public function assets(Request $request)
95116

96117
// set cache control headers
97118
$headers['Cache-Control'] = 'public, max-age=1800';
98-
$headers['Expires'] = gmdate('D, d M Y H:i:s \G\M\T', time() + 1800);
119+
$headers['Expires'] = gmdate('D, d M Y H:i:s \G\M\T', time() + 1800);
99120
return response()->file($path, $headers);
100121
}
101122
return response()->json(['error' => 'file not found'], 404);

0 commit comments

Comments
 (0)