Browse Source

Inline auto Laravel routes

shalvah 2 years ago
parent
commit
060c41a565
4 changed files with 27 additions and 45 deletions
  1. 1 1
      CHANGELOG.md
  2. 14 4
      routes/laravel.php
  3. 12 5
      routes/lumen.php
  4. 0 35
      src/Http/Controller.php

+ 1 - 1
CHANGELOG.md

@@ -18,7 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 - Support for specifying example model sources ([39ff208](https://github.com/knuckleswtf/scribe/commit/39ff208085d68eed4c459768ac5a1120934f021a))
 - Support for subgroups ([7cf07738](https://github.com/knuckleswtf/scribe/commit/7cf0773864fbdd1772fea9a5ff9e7ffd3360d7d2),[2ebf40b](https://github.com/knuckleswtf/scribe/commit/2ebf40b5e5be309bf5e685a0cd58bb70856b033d))
 - Nested response fields are now collapsed ([00b09bb](https://github.com/knuckleswtf/scribe/commit/00b09bbea8ec64006db864bf807004d48926c6d3))
-
+- Inlined routes (no more Scribe/Controller class)
 
 ## 3.34.0 (16 July 2022)
 ### Modified

+ 14 - 4
routes/laravel.php

@@ -1,14 +1,24 @@
 <?php
 
+use Illuminate\Http\JsonResponse;
 use Illuminate\Support\Facades\Route;
-use Knuckles\Scribe\Http\Controller;
+use Illuminate\Support\Facades\Storage;
+use Symfony\Component\HttpFoundation\BinaryFileResponse;
 
 $prefix = config('scribe.laravel.docs_url', '/docs');
 $middleware = config('scribe.laravel.middleware', []);
 
 Route::middleware($middleware)
     ->group(function () use ($prefix) {
-        Route::get($prefix, [Controller::class, 'webpage'])->name('scribe');
-        Route::get("$prefix.postman", [Controller::class, 'postman'])->name('scribe.postman');
-        Route::get("$prefix.openapi", [Controller::class, 'openapi'])->name('scribe.openapi');
+        Route::view($prefix, 'scribe.index')->name('scribe');
+
+        Route::get("$prefix.postman", function () {
+            return new JsonResponse(
+                Storage::disk('local')->get('scribe/collection.json'), json: true
+            );
+        })->name('scribe.postman');
+
+        Route::get("$prefix.openapi", function () {
+            return new BinaryFileResponse(Storage::disk('local')->path('scribe/openapi.yaml'));
+        })->name('scribe.openapi');
     });

+ 12 - 5
routes/lumen.php

@@ -1,6 +1,7 @@
 <?php
 
-use Knuckles\Scribe\Http\Controller;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Support\Facades\Storage;
 
 $prefix = config('scribe.laravel.docs_url', '/docs');
 $middleware = config('scribe.laravel.middleware', []);
@@ -8,9 +9,15 @@ $middleware = config('scribe.laravel.middleware', []);
 $router = app()->router;
 
 $router->group([
-    'middleware' => $middleware
+    'middleware' => $middleware,
 ], function () use ($router, $prefix) {
-    $router->get($prefix, ['uses' => Controller::class.'@webpage', 'as' => 'scribe']);
-    $router->get("$prefix.postman", ['uses' => Controller::class.'@postman', 'as' => 'scribe.postman']);
-    $router->get("$prefix.openapi", ['uses' => Controller::class.'@openapi', 'as' => 'scribe.openapi']);
+    $router->get($prefix, function () {
+        return view('scribe.index');
+    })->name('scribe');
+    $router->get("$prefix.postman", function () {
+        return new JsonResponse(Storage::disk('local')->get('scribe/collection.json'), json: true);
+    })->name('scribe.postman');
+    $router->get("$prefix.openapi", function () {
+        return response()->file(Storage::disk('local')->path('scribe/openapi.yaml'));
+    })->name('scribe.openapi');
 });

+ 0 - 35
src/Http/Controller.php

@@ -1,35 +0,0 @@
-<?php
-
-namespace Knuckles\Scribe\Http;
-
-use Illuminate\Support\Facades\Storage;
-
-class Controller
-{
-    public function webpage()
-    {
-        return view('scribe.index');
-    }
-
-    /**
-     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
-     *
-     * @return \Illuminate\Http\JsonResponse
-     */
-    public function postman()
-    {
-        return response()->json(
-            json_decode(Storage::disk('local')->get('scribe/collection.json'))
-        );
-    }
-
-    /**
-     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
-     *
-     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
-     */
-    public function openapi()
-    {
-        return response()->file(Storage::disk('local')->path('scribe/openapi.yaml'));
-    }
-}