Skip to content

Commit

Permalink
Handle cached closure routes more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Feb 14, 2023
1 parent 27937c2 commit 6fba3a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/CouldntGetRouteDetails.php
Original file line number Diff line number Diff line change
@@ -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`");
}
}
9 changes: 7 additions & 2 deletions src/GroupedEndpoints/GroupedEndpointsFromApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Knuckles\Scribe\GroupedEndpoints;

use Illuminate\Routing\Route;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
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;
Expand Down Expand Up @@ -102,7 +104,7 @@ private function extractEndpointsInfoFromLaravelApp(array $matches, array $cache
$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;
}
Expand Down Expand Up @@ -217,9 +219,12 @@ protected function writeEndpointsToDisk(array $grouped): void
}
}

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;
Expand Down

0 comments on commit 6fba3a6

Please sign in to comment.