Skip to content

Commit ece5754

Browse files
(v2) Backend changes
1 parent 18237ac commit ece5754

9 files changed

+241
-65
lines changed

.github/workflows/node.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
on: [push]
2+
3+
name: "CI Node"
4+
5+
jobs:
6+
test:
7+
name: Test
8+
9+
runs-on: ubuntu-latest
10+
11+
strategy:
12+
matrix:
13+
node-versions: [16, 18]
14+
15+
steps:
16+
- name: Cancel Previous Runs
17+
uses: styfle/[email protected]
18+
with:
19+
access_token: ${{ github.token }}
20+
- name: Checkout
21+
uses: actions/checkout@v2
22+
23+
- uses: actions/setup-node@v3
24+
with:
25+
node-version: ${{ matrix.node-versions }}
26+
27+
- name: NPM Install
28+
working-directory: ./ui
29+
run: npm install
30+
31+
- name: Lint, build/export
32+
working-directory: ./ui
33+
run: |
34+
npm run lint
35+
npm run export
36+
37+
38+

.github/workflows/phptest.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
on: [push]
22

3-
name: "CI"
3+
name: "CI PHP"
44

55
jobs:
66
test:
@@ -13,6 +13,10 @@ jobs:
1313
php-versions: [7.4, 8.0.6, 8.1, 8.2]
1414

1515
steps:
16+
- name: Cancel Previous Runs
17+
uses: styfle/[email protected]
18+
with:
19+
access_token: ${{ github.token }}
1620
- name: Checkout
1721
uses: actions/checkout@v2
1822

src/Commands/LaravelRequestDocsCommand.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Controllers/LaravelRequestDocsController.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,31 @@ public function __construct(LaravelRequestDocs $laravelRequestDoc, LaravelReques
1919
}
2020

2121
public function index(Request $request)
22+
{
23+
return view('request-docs::index');
24+
}
25+
public function api(Request $request)
2226
{
2327
$docs = $this->laravelRequestDocs->getDocs();
24-
$docs = $this->laravelRequestDocs->sortDocs($docs, config('request-docs.sort_by', 'default'));
28+
$docs = $this->laravelRequestDocs->sortDocs($docs, $request->sort);
29+
$docs = $this->laravelRequestDocs->groupDocs($docs, $request->groupby);
30+
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;
37+
38+
$docs = $this->laravelRequestDocs->filterByMethods(
39+
$docs,
40+
$showGet,
41+
$showPost,
42+
$showPut,
43+
$showPatch,
44+
$showDelete,
45+
$showHead
46+
);
2547
if ($request->openapi) {
2648
return response()->json(
2749
$this->laravelRequestDocsToOpenApi->openApi($docs)->toArray(),
@@ -32,6 +54,51 @@ public function index(Request $request)
3254
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
3355
);
3456
}
35-
return view('request-docs::index')->with(compact('docs'));
57+
58+
59+
return response()->json(
60+
$docs,
61+
Response::HTTP_OK,
62+
[
63+
'Content-type'=> 'application/json; charset=utf-8'
64+
],
65+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
66+
);
67+
}
68+
69+
public function assets(Request $request)
70+
{
71+
$path = explode('/', $request->path());
72+
$path = end($path);
73+
// read js, css from dist folder
74+
$path = base_path() . "/vendor/rakutentech/laravel-request-docs/resources/dist/_astro/" . $path;
75+
if (file_exists($path)) {
76+
$headers = ['Content-Type' => 'text/plain'];
77+
// set MIME type to js module
78+
if (str_ends_with($path, '.js')) {
79+
$headers = ['Content-Type' => 'application/javascript'];
80+
}
81+
if (str_ends_with($path, '.css')) {
82+
$headers = ['Content-Type' => 'text/css'];
83+
}
84+
if (str_ends_with($path, '.woff')) {
85+
$headers = ['Content-Type' => 'font/woff'];
86+
}
87+
if (str_ends_with($path, '.woff2')) {
88+
$headers = ['Content-Type' => 'font/woff2'];
89+
}
90+
if (str_ends_with($path, '.png')) {
91+
$headers = ['Content-Type' => 'image/png'];
92+
}
93+
if (str_ends_with($path, '.jpg')) {
94+
$headers = ['Content-Type' => 'image/jpg'];
95+
}
96+
97+
// set cache control headers
98+
$headers['Cache-Control'] = 'public, max-age=31536000';
99+
$headers['Expires'] = gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000);
100+
return response()->file($path, $headers);
101+
}
102+
return response()->json(['error' => 'file not found'], 404);
36103
}
37104
}

src/LaravelRequestDocs.php

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function getDocs(): array
3838

3939
public function sortDocs(array $docs, $sortBy = 'default'): array
4040
{
41+
if ($sortBy === 'default') {
42+
return $docs;
43+
}
4144
if ($sortBy === 'route_names') {
4245
sort($docs);
4346
return $docs;
@@ -49,11 +52,13 @@ public function sortDocs(array $docs, $sortBy = 'default'): array
4952
'PUT',
5053
'PATCH',
5154
'DELETE',
55+
'HEAD',
5256
];
5357
foreach ($methods as $method) {
5458
foreach ($docs as $key => $doc) {
5559
if (in_array($method, $doc['methods'])) {
5660
if (!in_array($doc, $sorted)) {
61+
$doc['methods'] = [$method];
5762
$sorted[] = $doc;
5863
}
5964
}
@@ -62,6 +67,64 @@ public function sortDocs(array $docs, $sortBy = 'default'): array
6267
return $sorted;
6368
}
6469

70+
public function filterByMethods($docs, $get, $post, $put, $path, $delete, $head)
71+
{
72+
$filtered = [];
73+
foreach ($docs as $key => $doc) {
74+
if ($get && in_array('GET', $doc['methods'])) {
75+
$_doc = $doc;
76+
$_doc['methods'] = ['GET'];
77+
$filtered[] = $_doc;
78+
}
79+
}
80+
foreach ($docs as $key => $doc) {
81+
if ($post && in_array('POST', $doc['methods'])) {
82+
$_doc = $doc;
83+
$_doc['methods'] = ['POST'];
84+
$filtered[] = $_doc;
85+
}
86+
}
87+
foreach ($docs as $key => $doc) {
88+
if ($put && in_array('PUT', $doc['methods'])) {
89+
$_doc = $doc;
90+
$_doc['methods'] = ['PUT'];
91+
$filtered[] = $_doc;
92+
}
93+
}
94+
foreach ($docs as $key => $doc) {
95+
if ($path && in_array('PATCH', $doc['methods'])) {
96+
$_doc = $doc;
97+
$_doc['methods'] = ['PATCH'];
98+
$filtered[] = $_doc;
99+
}
100+
}
101+
foreach ($docs as $key => $doc) {
102+
if ($delete && in_array('DELETE', $doc['methods'])) {
103+
$_doc = $doc;
104+
$_doc['methods'] = ['DELETE'];
105+
$filtered[] = $_doc;
106+
}
107+
}
108+
foreach ($docs as $key => $doc) {
109+
if ($head && in_array('HEAD', $doc['methods'])) {
110+
$_doc = $doc;
111+
$_doc['methods'] = ['HEAD'];
112+
$filtered[] = $_doc;
113+
}
114+
}
115+
116+
return $filtered;
117+
}
118+
119+
public function groupDocs($docs, $group = 'default')
120+
{
121+
if ($group == 'default') {
122+
return $docs;
123+
}
124+
$grouped = [];//unimplemented
125+
return $docs; //unimplemented, returning as it is
126+
}
127+
65128
public function getControllersInfo(): array
66129
{
67130
$controllersInfo = [];
@@ -99,13 +162,13 @@ public function getControllersInfo(): array
99162
$controllersInfo[] = [
100163
'uri' => $route->uri,
101164
'methods' => $route->methods,
102-
'middlewares' => $middlewares,
103-
'controller' => $controllerName,
104-
'controller_full_path' => $controllerFullPath,
105-
'method' => $method,
165+
'middlewares' => config('request-docs.hide_meta_data') ? [] : $middlewares,
166+
'controller' => config('request-docs.hide_meta_data') ? '' : $controllerName,
167+
'controller_full_path' => config('request-docs.hide_meta_data') ? '' : $controllerFullPath,
168+
'method' => config('request-docs.hide_meta_data') ? '' : $method,
106169
'httpMethod' => $httpMethod,
107170
'rules' => [],
108-
'docBlock' => ""
171+
'docBlock' => "",
109172
];
110173
} catch (Exception $e) {
111174
continue;
@@ -218,6 +281,11 @@ public function rulesByRegex($requestClassName, $methodName)
218281
$rules = [];
219282

220283
for ($i = $data->getStartLine() - 1; $i <= $data->getEndLine() - 1; $i++) {
284+
// check if line is a comment
285+
$trimmed = trim($lines[$i]);
286+
if (Str::startsWith($trimmed, '//') || Str::startsWith($trimmed, '#')) {
287+
continue;
288+
}
221289
// check if => in string, only pick up rules that are coded on single line
222290
if (Str::contains($lines[$i], '=>')) {
223291
preg_match_all("/(?:'|\").*?(?:'|\")/", $lines[$i], $matches);
@@ -250,8 +318,8 @@ private function customParamsDocComment($docComment): array
250318
$params = [];
251319

252320
foreach (explode("\n", $docComment) as $comment) {
253-
if (Str::contains($comment, '@QAparam')) {
254-
$comment = trim(Str::replace(['@QAparam', '*'], '', $comment));
321+
if (Str::contains($comment, '@LRDparam')) {
322+
$comment = trim(Str::replace(['@LRDparam', '*'], '', $comment));
255323

256324
$comment = explode(' ', $comment);
257325

src/LaravelRequestDocsMiddleware.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use Illuminate\Database\Events\QueryExecuted;
77
use Illuminate\Support\Facades\DB;
88
use KitLoong\AppLogger\QueryLog\LogWriter as QueryLogger;
9+
use Log;
910

1011
class LaravelRequestDocsMiddleware extends QueryLogger
1112
{
1213
private array $queries = [];
14+
private array $logs = [];
1315

1416
public function handle($request, Closure $next)
1517
{
@@ -18,11 +20,20 @@ public function handle($request, Closure $next)
1820
}
1921

2022
$this->listenDB();
23+
$this->listenToLogs();
2124
$response = $next($request);
2225

26+
try {
27+
$response->getData();
28+
} catch (\Exception $e) {
29+
// not a json response
30+
return $response;
31+
}
32+
2333
$content = $response->getData();
2434
$content->_lrd = [
2535
'queries' => $this->queries,
36+
'logs' => $this->logs,
2637
'memory' => (string) round(memory_get_peak_usage(true) / 1048576, 2) . "MB",
2738
];
2839
$jsonContent = json_encode($content);
@@ -46,4 +57,10 @@ public function listenDB()
4657
$this->queries[] = $this->getMessages($query);
4758
});
4859
}
60+
public function listenToLogs()
61+
{
62+
Log::listen(function ($message) {
63+
$this->logs[] = $message;
64+
});
65+
}
4966
}

0 commit comments

Comments
 (0)