Quellcode durchsuchen

Handle cached closure routes more gracefully

shalvah vor 2 Jahren
Ursprung
Commit
6fba3a61b7

+ 11 - 0
src/Exceptions/CouldntGetRouteDetails.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace Knuckles\Scribe\Exceptions;
+
+class CouldntGetRouteDetails extends \RuntimeException implements ScribeException
+{
+    public static function forRoute(string $route): self
+    {
+        return new self("Unable to retrieve controller and method for route $route; try running `php artisan route:clear`");
+    }
+}

+ 7 - 2
src/GroupedEndpoints/GroupedEndpointsFromApp.php

@@ -2,6 +2,7 @@
 
 namespace Knuckles\Scribe\GroupedEndpoints;
 
+use Illuminate\Routing\Route;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Str;
@@ -9,6 +10,7 @@ use Knuckles\Camel\Camel;
 use Knuckles\Camel\Extraction\ExtractedEndpointData;
 use Knuckles\Camel\Output\OutputEndpointData;
 use Knuckles\Scribe\Commands\GenerateDocumentation;
+use Knuckles\Scribe\Exceptions\CouldntGetRouteDetails;
 use Knuckles\Scribe\Extracting\ApiDetails;
 use Knuckles\Scribe\Extracting\Extractor;
 use Knuckles\Scribe\Matching\MatchedRoute;
@@ -102,7 +104,7 @@ class GroupedEndpointsFromApp implements GroupedEndpointsContract
             $route = $routeItem->getRoute();
 
             $routeControllerAndMethod = u::getRouteClassAndMethodNames($route);
-            if (!$this->isValidRoute($routeControllerAndMethod)) {
+            if (!$this->isValidRoute($routeControllerAndMethod, $route)) {
                 c::warn('Skipping invalid route: ' . c::getRouteRepresentation($route));
                 continue;
             }
@@ -217,9 +219,12 @@ class GroupedEndpointsFromApp implements GroupedEndpointsContract
         }
     }
 
-    private function isValidRoute(array $routeControllerAndMethod = null): bool
+    private function isValidRoute(?array $routeControllerAndMethod, Route $route): bool
     {
         if (is_array($routeControllerAndMethod)) {
+            if (count($routeControllerAndMethod) < 2) {
+                throw CouldntGetRouteDetails::forRoute(c::getRouteRepresentation($route));
+            }
             [$classOrObject, $method] = $routeControllerAndMethod;
             if (u::isInvokableObject($classOrObject)) {
                 return true;