Browse Source

Auto-detect Dingo router

shalvah 4 years ago
parent
commit
89569d7a7c

+ 1 - 0
composer.dingo.json

@@ -29,6 +29,7 @@
         "league/flysystem": "^1.0",
         "mpociot/reflection-docblock": "^1.0.1",
         "nunomaduro/collision": "^3.0|^4.0|^5.0",
+        "ocramius/package-versions": "^2.1",
         "ramsey/uuid": "^3.8|^4.0",
         "shalvah/clara": "^2.6",
         "spatie/data-transfer-object": "^2.6",

+ 1 - 0
composer.json

@@ -28,6 +28,7 @@
     "league/flysystem": "^1.0",
     "mpociot/reflection-docblock": "^1.0.1",
     "nunomaduro/collision": "^3.0|^4.0|^5.0",
+    "ocramius/package-versions": "^2.1",
     "ramsey/uuid": "^3.8|^4.0",
     "shalvah/clara": "^2.6",
     "spatie/data-transfer-object": "^2.6",

+ 0 - 5
config/scribe.php

@@ -297,11 +297,6 @@ INTRO
      */
     'logo' => false,
 
-    /*
-     * The router your API is using (Laravel or Dingo).
-     */
-    'router' => 'laravel',
-
     /*
      * If you would like the package to generate the same example values for parameters on each run,
      * set this to any number (eg. 1234)

+ 22 - 3
src/Commands/GenerateDocumentation.php

@@ -64,7 +64,7 @@ class GenerateDocumentation extends Command
             $this->extractAndWriteApiDetailsToDisk();
         } else {
             if (!is_dir(static::$camelDir)) {
-                throw new \InvalidArgumentException("Can't use --no-extraction because there are no endpoints in the ".static::$camelDir." directory.");
+                throw new \InvalidArgumentException("Can't use --no-extraction because there are no endpoints in the " . static::$camelDir . " directory.");
             }
             $groupedEndpoints = Camel::loadEndpointsIntoGroups(static::$camelDir);
         }
@@ -293,7 +293,7 @@ class GenerateDocumentation extends Command
             $cachedEndpoints = Camel::loadEndpointsToFlatPrimitivesArray(static::$cacheDir);
         }
 
-        $routes = $routeMatcher->getRoutes($this->docConfig->get('routes'), $this->docConfig->get('router'));
+        $routes = $routeMatcher->getRoutes($this->docConfig->get('routes'), $this->getRouter());
         $endpoints = $this->extractEndpointsInfoFromLaravelApp($routes, $cachedEndpoints, $latestEndpointsData);
         $groupedEndpoints = Camel::groupEndpoints($endpoints);
         $this->writeEndpointsToDisk($groupedEndpoints);
@@ -305,7 +305,7 @@ class GenerateDocumentation extends Command
     protected function writeExampleCustomEndpoint(): void
     {
         // We add an example to guide users in case they need to add a custom endpoint.
-        if (!file_exists(static::$camelDir.'/custom.0.yaml')) {
+        if (!file_exists(static::$camelDir . '/custom.0.yaml')) {
             copy(__DIR__ . '/../../resources/example_custom_endpoint.yaml', static::$camelDir . '/custom.0.yaml');
         }
     }
@@ -315,4 +315,23 @@ class GenerateDocumentation extends Command
         $apiDetails = new ApiDetails($this->docConfig, !$this->option('force'));
         $apiDetails->writeMarkdownFiles();
     }
+
+    protected function getRouter(): string
+    {
+        if ($router = $this->docConfig->get('router', null)) {
+            if (!in_array($router, ['dingo', 'laravel'])) {
+                throw new \InvalidArgumentException("Unknown `router` value: $router");
+            }
+            return $router;
+        }
+
+        try {
+            $dingoVersion = \PackageVersions\Versions::getVersion('dingo/api');
+        } catch (\OutOfBoundsException $e) {
+            return 'laravel';
+        }
+
+        c::info('Detected Dingo API router');
+        return 'dingo';
+    }
 }

+ 0 - 1
tests/GenerateDocumentationTest.php

@@ -108,7 +108,6 @@ class GenerateDocumentationTest extends BaseLaravelTest
             $api->get('/test', [TestController::class, 'withEndpointDescription']);
         });
 
-        config(['scribe.router' => 'dingo']);
         config(['scribe.routes.0.match.prefixes' => ['*']]);
         config(['scribe.routes.0.match.versions' => ['v1']]);
         $output = $this->artisan('scribe:generate');

+ 3 - 3
tests/Strategies/Responses/ResponseCallsTest.php

@@ -164,7 +164,7 @@ class ResponseCallsTest extends BaseLaravelTest
             ],
         ];
 
-        $strategy = new ResponseCalls(new DocumentationConfig(['router' => 'dingo']));
+        $strategy = new ResponseCalls(new DocumentationConfig());
         $results = $strategy->makeResponseCallIfConditionsPass(
             ExtractedEndpointData::fromRoute($route), $rules
         );
@@ -206,7 +206,7 @@ class ResponseCallsTest extends BaseLaravelTest
                 'header' => 'value',
             ],
         ]);
-        $strategy = new ResponseCalls(new DocumentationConfig(['router' => 'dingo']));
+        $strategy = new ResponseCalls(new DocumentationConfig());
         $results = $strategy->makeResponseCallIfConditionsPass($endpointData, $rules);
 
         $this->assertEquals(200, $results[0]['status']);
@@ -231,7 +231,7 @@ class ResponseCallsTest extends BaseLaravelTest
             ],
         ];
 
-        $strategy = new ResponseCalls(new DocumentationConfig(['router' => 'dingo']));
+        $strategy = new ResponseCalls(new DocumentationConfig());
         $results = $strategy->makeResponseCallIfConditionsPass(
             ExtractedEndpointData::fromRoute($route), $rules
         );