From c92447900f0e8921ab83973cbb846cc3df6111fc Mon Sep 17 00:00:00 2001 From: tomirons Date: Wed, 6 Mar 2024 16:48:30 -0500 Subject: [PATCH] gracefully continue --- src/Processors/RouteProcessor.php | 109 ++++++++++++++++-------------- 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/src/Processors/RouteProcessor.php b/src/Processors/RouteProcessor.php index 90230c1..099a7e6 100644 --- a/src/Processors/RouteProcessor.php +++ b/src/Processors/RouteProcessor.php @@ -9,6 +9,7 @@ use Illuminate\Routing\Route; use Illuminate\Routing\Router; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; use Illuminate\Support\Stringable; @@ -54,80 +55,84 @@ public function process(array $output): array */ protected function processRoute(Route $route) { - $methods = array_filter($route->methods(), fn ($value) => $value !== 'HEAD'); - $middlewares = $route->gatherMiddleware(); + try { + $methods = array_filter($route->methods(), fn ($value) => $value !== 'HEAD'); + $middlewares = $route->gatherMiddleware(); - foreach ($methods as $method) { - $includedMiddleware = false; + foreach ($methods as $method) { + $includedMiddleware = false; - foreach ($middlewares as $middleware) { - if (in_array($middleware, $this->config['include_middleware'])) { - $includedMiddleware = true; + foreach ($middlewares as $middleware) { + if (in_array($middleware, $this->config['include_middleware'])) { + $includedMiddleware = true; + } } - } - if (empty($middlewares) || ! $includedMiddleware) { - continue; - } + if (empty($middlewares) || ! $includedMiddleware) { + continue; + } - $reflectionMethod = $this->getReflectionMethod($route->getAction()); + $reflectionMethod = $this->getReflectionMethod($route->getAction()); - if (! $reflectionMethod) { - continue; - } + if (! $reflectionMethod) { + continue; + } - $routeHeaders = $this->config['headers']; + $routeHeaders = $this->config['headers']; - if ($this->authentication && in_array($this->config['auth_middleware'], $middlewares)) { - $routeHeaders[] = $this->authentication->toArray(); - } + if ($this->authentication && in_array($this->config['auth_middleware'], $middlewares)) { + $routeHeaders[] = $this->authentication->toArray(); + } - $uri = Str::of($route->uri()) - ->after('/') - ->replaceMatches('/{([[:alnum:]]+)}/', ':$1'); + $uri = Str::of($route->uri()) + ->after('/') + ->replaceMatches('/{([[:alnum:]]+)}/', ':$1'); - // if (!$uri->toString()) { - // return []; - // } + // if (!$uri->toString()) { + // return []; + // } - if ($this->config['include_doc_comments']) { - $description = (new DocBlockProcessor)($reflectionMethod); - } + if ($this->config['include_doc_comments']) { + $description = (new DocBlockProcessor)($reflectionMethod); + } - $data = [ - 'name' => $route->uri(), - 'request' => array_merge( - $this->processRequest( - $method, - $uri, - $this->config['enable_formdata'] ? (new FormDataProcessor)->process($reflectionMethod) : collect() + $data = [ + 'name' => $route->uri(), + 'request' => array_merge( + $this->processRequest( + $method, + $uri, + $this->config['enable_formdata'] ? (new FormDataProcessor)->process($reflectionMethod) : collect() + ), + ['description' => $description ?? ''] ), - ['description' => $description ?? ''] - ), - 'response' => [], + 'response' => [], - 'protocolProfileBehavior' => [ - 'disableBodyPruning' => $this->config['protocol_profile_behavior']['disable_body_pruning'] ?? false, - ], - ]; + 'protocolProfileBehavior' => [ + 'disableBodyPruning' => $this->config['protocol_profile_behavior']['disable_body_pruning'] ?? false, + ], + ]; - if ($this->config['structured']) { - $routeNameSegments = ( + if ($this->config['structured']) { + $routeNameSegments = ( $route->getName() ? Str::of($route->getName())->explode('.') : Str::of($route->uri())->after('api/')->explode('/') - )->filter(fn ($value) => ! is_null($value) && $value !== ''); + )->filter(fn ($value) => ! is_null($value) && $value !== ''); - if (! $this->config['crud_folders']) { - if (in_array($routeNameSegments->last(), ['index', 'store', 'show', 'update', 'destroy'])) { - $routeNameSegments->forget($routeNameSegments->count() - 1); + if (! $this->config['crud_folders']) { + if (in_array($routeNameSegments->last(), ['index', 'store', 'show', 'update', 'destroy'])) { + $routeNameSegments->forget($routeNameSegments->count() - 1); + } } - } - $this->buildTree($this->output, $routeNameSegments->all(), $data); - } else { - $this->output['item'][] = $data; + $this->buildTree($this->output, $routeNameSegments->all(), $data); + } else { + $this->output['item'][] = $data; + } } + } catch (\Exception $e) { + Log::warning('Failed to process route: '.$route->uri()); } }