Skip to content

Commit 3acced1

Browse files
v fix
2 parents 8ad44a9 + 4a78b94 commit 3acced1

File tree

4 files changed

+101
-18
lines changed

4 files changed

+101
-18
lines changed

README.md

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
# Laravel Docs Generator
1+
<p align="center">
2+
<a href="https://github.com/rakutentech/laravel-request-docs">
3+
<img alt="Laravel Request Docs" src="https://imgur.com/9eDTUaI.png" width="360">
4+
</a>
5+
</p>
6+
7+
<p align="center">
8+
The Hassle-Free automatic API documentation generation for Laravel. <br>
9+
A Swagger alernative.
10+
</p>
11+
12+
**Fast:** Install on any Laravel Project
13+
14+
**Hassle Free:** Auto Generate API Documentation for request rules and parameters
15+
16+
**Analyze:** In build SQL query time analyzer, response time and headers output.
17+
18+
## Features
19+
20+
- Automatic routes fetching from Laravel Routes
21+
- Automatic rules fetching from injected Request
22+
- Support for Authorization Headers
23+
- Support for SQL query, response time and memory consumption by request on Laravel
24+
- Intelligent auto request builder using ``faker.js``
25+
- Display extra documentation using markdown
26+
# Read on Medium
227

328
Automatically generate api documentation for Laravel without writing annotations.
429

@@ -11,7 +36,7 @@ Read more: https://medium.com/web-developer/laravel-automatically-generate-api-d
1136
| PHP | 7.4 or 8.0 |
1237
| Laravel | 6.* or 8.* or 9.* |
1338

14-
## Installation
39+
# Installation
1540

1641
You can install the package via composer:
1742

@@ -26,7 +51,7 @@ You can publish the config file with:
2651
php artisan vendor:publish --tag=request-docs-config
2752
```
2853

29-
## Usage
54+
# Usage
3055

3156
View in the browser on ``/request-docs/``
3257

@@ -38,31 +63,59 @@ php artisan lrd:generate
3863

3964
Docs HTML is generated inside ``docs/``.
4065

41-
## Design pattern
66+
# Design pattern
4267

4368
In order for this plugin to work, you need to follow the design pattern by injecting the request class inside the controller.
4469
For extra documentation you can use markdown inside your controller method as well.
4570

4671
![Design pattern](https://imgur.com/yXjq3jp.png)
4772

48-
### Screenshots
73+
# Screenshots
4974

50-
Generated API documentation
75+
**Generated API documentation**
5176

5277
![Preview](https://imgur.com/8DvBBhs.png)
5378

54-
Try API
79+
**Try API**
5580

5681
![Preview](https://imgur.com/kcKVSzm.png)
5782

83+
**SQL query profile**
84+
85+
![Preview](https://imgur.com/y8jT3jj.png)
86+
87+
**Resonse profile**
88+
89+
![Preview](https://imgur.com/U0Je956.png)
90+
91+
**Customize Headers**
92+
93+
![Preview](https://imgur.com/5ydtRd8.png)
94+
95+
96+
# Extra
97+
98+
You write extra documentation in markdown which will be rendered as HTML on the dashboard.
99+
Example of using it in controller
100+
101+
```php
102+
/**
103+
* @lrd:start
104+
* #Hello markdown
105+
* ## Documentation for /my route
106+
* @lrd:end
107+
*/
108+
public function index(MyIndexRequest $request): Resource
109+
{
110+
```
58111

59-
## Testing
112+
# Testing
60113

61114
```bash
62115
./vendor/bin/phpunit
63116
```
64117

65-
## Linting
118+
# Linting
66119

67120
```bash
68121
./vendor/bin/phpcs --standard=phpcs.xml --extensions=php --ignore=tests/migrations config/ src/
@@ -75,7 +128,7 @@ Fixing lints
75128
./vendor/bin/php-cs-fixer fix config/
76129
```
77130

78-
## Changelog
131+
# Changelog
79132

80133
- Initial Release
81134
- v1.9 Added improvements such as status code, response headers, custom request headers and fixed issues reported by users

config/request-docs.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

33
return [
4+
// change it to true will make lrd to throw exception if rules in request class need to be changed
5+
// keep it false
6+
'debug' => false,
47
'document_name' => 'LRD',
58

69
/*

resources/views/index.blade.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@
100100
</nav>
101101
<div id="app" v-cloak class="w-full flex lg:pt-10">
102102
<aside class="text-sm ml-1.5 text-grey-darkest break-all bg-gray-200 pl-2 h-screen sticky top-1 overflow-auto">
103-
<h1 class="font-medium mx-3 mt-3">Routes List</h1>
103+
<h1 class="font-medium mx-3 mt-3" style="width: max-content;min-width:350px;">Routes List</h1>
104104
<hr class="border-b border-gray-300">
105105
<table class="table-fixed text-sm mt-5" style="width: max-content">
106106
<tbody>
107107
@foreach ($docs as $index => $doc)
108108
<tr>
109109
<td>
110-
<a href="#{{$doc['methods'][0] .'-'. $doc['uri']}}" @click="highlightSidebar({{$index}})">
110+
<a href="#{{$doc['methods'][0] .'-'. $doc['uri']}}" @click="highlightSidebar({{$index}})" v-if="!docs[{{$index}}]['isHidden']">
111111
<span class="
112112
font-medium
113113
inline-flex
@@ -154,7 +154,21 @@ class="inline-flex text-xs"
154154
<br><br>
155155
<div class="ml-6 mr-6 pl-2 w-2/3 p-2" style="width: 100%">
156156
<h1 class="pl-2 pr-2 break-normal text-black break-normal font-sans text-black font-medium">
157-
Settings
157+
Search・Sort settings
158+
</h1>
159+
<hr class="border-b border-gray-300">
160+
<br>
161+
<section class="pt-5 pl-2 pr-2 pb-5 border mb-10 rounded bg-white shadow">
162+
<div class="font-sans">
163+
<h2 class="text-sm break-normal text-black break-normal font-sans pb-1 pt-1 text-black">
164+
Filter
165+
</h2>
166+
<p class="text-xs pb-2 font-medium text-gray-500">Hide non matching</code></p>
167+
<input type="text" v-model="filterTerm" @input="filterDocs" class="w-full p-2 border-2 border-gray-300 rounded" placeholder="/api/search">
168+
</div>
169+
</section>
170+
<h1 class="pl-2 pr-2 break-normal text-black break-normal font-sans text-black font-medium">
171+
Request Settings (editable)
158172
</h1>
159173
<hr class="border-b border-gray-300">
160174
<br>
@@ -175,7 +189,7 @@ class="my-prism-editor"
175189
<hr class="border-b border-gray-300">
176190
<br>
177191
@foreach ($docs as $index => $doc)
178-
<section class="pt-5 pl-2 pr-2 pb-5 border mb-10 rounded bg-white shadow">
192+
<section class="pt-5 pl-2 pr-2 pb-5 border mb-10 rounded bg-white shadow" v-if="!docs[{{$index}}]['isHidden']">
179193
<div class="font-sans" id="{{$doc['httpMethod'] .'-'. $doc['uri']}}">
180194
<h1 class="text-sm break-normal text-black bg-indigo-50 break-normal font-sans pb-1 pt-1 text-black">
181195
<span class="w-20
@@ -618,7 +632,8 @@ class="my-prism-editor"
618632
data: {
619633
docs: docs,
620634
showRoute: false,
621-
requestHeaders: ''
635+
requestHeaders: '',
636+
filterTerm: ''
622637
},
623638
created: function () {
624639
axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
@@ -637,6 +652,11 @@ class="my-prism-editor"
637652
highlighterAtom(code) {
638653
return Prism.highlight(code, Prism.languages.atom, "js");
639654
},
655+
filterDocs() {
656+
for (doc of this.docs) {
657+
doc['isHidden'] = !doc['uri'].includes(this.filterTerm)
658+
}
659+
},
640660
request(doc) {
641661
642662
// convert string to lower case

src/LaravelRequestDocs.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ public function appendRequestRules(array $controllersInfo)
135135
// doesn't work well when the same rule is defined as array ['required', 'integer'] or in multiple lines such as
136136
// If your rules are not populated using this library, then fix your rule to only throw validation errors and not throw exceptions
137137
// such as 404, 500 inside the request class.
138-
// $controllersInfo[$index]['rules'] = $this->rulesByRegex($requestClassName);
138+
$controllersInfo[$index]['rules'] = $this->rulesByRegex($requestClassName);
139+
140+
if (config('request-docs.debug')) {
141+
throw $e;
142+
}
139143
}
140144
$controllersInfo[$index]['docBlock'] = $this->lrdDocComment($reflectionMethod->getDocComment());
141145
}
@@ -203,8 +207,11 @@ public function rulesByRegex($requestClassName)
203207
$rules = [];
204208

205209
for ($i = $data->getStartLine() - 1; $i <= $data->getEndLine() - 1; $i++) {
206-
preg_match_all("/(?:'|\").*?(?:'|\")/", $lines[$i], $matches);
207-
$rules[] = $matches;
210+
// check if => in string, only pick up rules that are coded on single line
211+
if (Str::contains($lines[$i], '=>')) {
212+
preg_match_all("/(?:'|\").*?(?:'|\")/", $lines[$i], $matches);
213+
$rules[] = $matches;
214+
}
208215
}
209216

210217
$rules = collect($rules)

0 commit comments

Comments
 (0)