From 87a479e8ee68fdc05462b3faa8d34e1d2ae2e6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ka=CC=88llstrand=20Modig?= Date: Thu, 25 Feb 2021 23:19:42 +0100 Subject: [PATCH 1/4] Refactored structure for retreiving reflection methods. If config is set to only allow generation for available controller methods the new function will return null if scaffolded functions are not available. This is useful when making collections for Route::resource where Laravel scaffolding assumes model/{model}/create and similar view based routes that isn't actually in place. It also prevents items being generated for routes that points to non-existing methods. --- config/api-postman.php | 8 +++++ src/Commands/ExportPostmanCommand.php | 48 +++++++++++++++++---------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/config/api-postman.php b/config/api-postman.php index c83850b..089cf3f 100644 --- a/config/api-postman.php +++ b/config/api-postman.php @@ -87,4 +87,12 @@ */ 'disk' => 'local', + /* + * Generate for available controller methods only + * + * Forces generation to only generate items for actual methods instead of all the + * standard scaffolded ones. Useful for API resources where form routes are not used. + */ + 'available_methods_only' => true, + ]; diff --git a/src/Commands/ExportPostmanCommand.php b/src/Commands/ExportPostmanCommand.php index 932fd0a..81ec4a7 100644 --- a/src/Commands/ExportPostmanCommand.php +++ b/src/Commands/ExportPostmanCommand.php @@ -65,27 +65,25 @@ public function handle(): void } } - if (empty($middlewares) || ! $includedMiddleware) { + if (empty($middlewares) || !$includedMiddleware) { continue; } $requestRules = []; - if ($this->config['enable_formdata']) { - $routeAction = $route->getAction(); - - if ($routeAction['uses'] instanceof Closure) { - $reflectionMethod = new ReflectionFunction($routeAction['uses']); - } else { - $routeData = explode('@', $routeAction['uses']); - $reflection = new ReflectionClass($routeData[0]); - $reflectionMethod = $reflection->getMethod($routeData[1]); - } + $routeAction = $route->getAction(); + + $reflectionMethod = $this->getReflectionMethod($routeAction); + + if (!$reflectionMethod) { + continue; + } + if ($this->config['enable_formdata']) { $rulesParameter = null; foreach ($reflectionMethod->getParameters() as $parameter) { - if (! $parameterType = $parameter->getType()) { + if (!$parameterType = $parameter->getType()) { continue; } @@ -117,7 +115,7 @@ public function handle(): void if ($this->isStructured()) { $routeNames = $route->action['as'] ?? null; - if (! $routeNames) { + if (!$routeNames) { $routeUri = explode('/', $route->uri()); // remove "api" from the start @@ -128,7 +126,7 @@ public function handle(): void $routeNames = explode('.', $routeNames); $routeNames = array_filter($routeNames, function ($value) { - return ! is_null($value) && $value !== ''; + return !is_null($value) && $value !== ''; }); $this->buildTree($this->structure, $routeNames, $request); @@ -143,6 +141,22 @@ public function handle(): void $this->info("Postman Collection Exported: $exportName"); } + protected function getReflectionMethod(array $routeAction): ?object + { + if ($routeAction['uses'] instanceof Closure) { + return new ReflectionFunction($routeAction['uses']); + } + + $routeData = explode('@', $routeAction['uses']); + $reflection = new ReflectionClass($routeData[0]); + + if ($this->config['available_methods_only'] && !$reflection->hasMethod($routeData[1])) { + return null; + } + + return $reflection->getMethod($routeData[1]); + } + protected function buildTree(array &$routes, array $segments, array $request): void { $parent = &$routes; @@ -167,7 +181,7 @@ protected function buildTree(array &$routes, array $segments, array $request): v unset($item); - if (! $matched) { + if (!$matched) { $item = [ 'name' => $segment, 'item' => $segment === $destination ? [$request] : [], @@ -189,8 +203,8 @@ public function makeRequest($route, $method, $routeHeaders, $requestRules) 'method' => strtoupper($method), 'header' => $routeHeaders, 'url' => [ - 'raw' => '{{base_url}}/'.$route->uri(), - 'host' => '{{base_url}}/'.$route->uri(), + 'raw' => '{{base_url}}/' . $route->uri(), + 'host' => '{{base_url}}/' . $route->uri(), ], ], ]; From 74618a63e081e34a7941f28455b47a658ef16bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ka=CC=88llstrand=20Modig?= Date: Thu, 25 Feb 2021 23:23:11 +0100 Subject: [PATCH 2/4] PSR complicance reversed to accomodate StyleCI --- src/Commands/ExportPostmanCommand.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Commands/ExportPostmanCommand.php b/src/Commands/ExportPostmanCommand.php index 81ec4a7..0dc30a2 100644 --- a/src/Commands/ExportPostmanCommand.php +++ b/src/Commands/ExportPostmanCommand.php @@ -65,7 +65,7 @@ public function handle(): void } } - if (empty($middlewares) || !$includedMiddleware) { + if (empty($middlewares) || ! $includedMiddleware) { continue; } @@ -75,7 +75,7 @@ public function handle(): void $reflectionMethod = $this->getReflectionMethod($routeAction); - if (!$reflectionMethod) { + if (! $reflectionMethod) { continue; } @@ -83,7 +83,7 @@ public function handle(): void $rulesParameter = null; foreach ($reflectionMethod->getParameters() as $parameter) { - if (!$parameterType = $parameter->getType()) { + if (! $parameterType = $parameter->getType()) { continue; } @@ -115,7 +115,7 @@ public function handle(): void if ($this->isStructured()) { $routeNames = $route->action['as'] ?? null; - if (!$routeNames) { + if (! $routeNames) { $routeUri = explode('/', $route->uri()); // remove "api" from the start @@ -126,7 +126,7 @@ public function handle(): void $routeNames = explode('.', $routeNames); $routeNames = array_filter($routeNames, function ($value) { - return !is_null($value) && $value !== ''; + return ! is_null($value) && $value ! == ''; }); $this->buildTree($this->structure, $routeNames, $request); @@ -150,7 +150,7 @@ protected function getReflectionMethod(array $routeAction): ?object $routeData = explode('@', $routeAction['uses']); $reflection = new ReflectionClass($routeData[0]); - if ($this->config['available_methods_only'] && !$reflection->hasMethod($routeData[1])) { + if ($this->config['available_methods_only'] && ! $reflection->hasMethod($routeData[1])) { return null; } @@ -181,7 +181,7 @@ protected function buildTree(array &$routes, array $segments, array $request): v unset($item); - if (!$matched) { + if (! $matched) { $item = [ 'name' => $segment, 'item' => $segment === $destination ? [$request] : [], @@ -203,8 +203,8 @@ public function makeRequest($route, $method, $routeHeaders, $requestRules) 'method' => strtoupper($method), 'header' => $routeHeaders, 'url' => [ - 'raw' => '{{base_url}}/' . $route->uri(), - 'host' => '{{base_url}}/' . $route->uri(), + 'raw' => '{{base_url}}/'.$route->uri(), + 'host' => '{{base_url}}/'.$route->uri(), ], ], ]; From 3964d7500d53a307407e13902f76bb877db41ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ka=CC=88llstrand=20Modig?= Date: Thu, 25 Feb 2021 23:24:32 +0100 Subject: [PATCH 3/4] Slight bug after StyleCI fix --- src/Commands/ExportPostmanCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ExportPostmanCommand.php b/src/Commands/ExportPostmanCommand.php index 0dc30a2..9de5008 100644 --- a/src/Commands/ExportPostmanCommand.php +++ b/src/Commands/ExportPostmanCommand.php @@ -126,7 +126,7 @@ public function handle(): void $routeNames = explode('.', $routeNames); $routeNames = array_filter($routeNames, function ($value) { - return ! is_null($value) && $value ! == ''; + return ! is_null($value) && $value !== ''; }); $this->buildTree($this->structure, $routeNames, $request); From a99919b1c6f5dadc0b1c9f15cc6ca48d787d2357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ka=CC=88llstrand=20Modig?= Date: Thu, 25 Feb 2021 23:59:05 +0100 Subject: [PATCH 4/4] After more consideration, this shouldn't be optional. No items should be generated in collection if the route points to a missing method. Maybe issuing a warning? --- config/api-postman.php | 9 --------- src/Commands/ExportPostmanCommand.php | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/config/api-postman.php b/config/api-postman.php index 089cf3f..80372c8 100644 --- a/config/api-postman.php +++ b/config/api-postman.php @@ -86,13 +86,4 @@ * Specify the configured disk for storing the postman collection file. */ 'disk' => 'local', - - /* - * Generate for available controller methods only - * - * Forces generation to only generate items for actual methods instead of all the - * standard scaffolded ones. Useful for API resources where form routes are not used. - */ - 'available_methods_only' => true, - ]; diff --git a/src/Commands/ExportPostmanCommand.php b/src/Commands/ExportPostmanCommand.php index 9de5008..165ac38 100644 --- a/src/Commands/ExportPostmanCommand.php +++ b/src/Commands/ExportPostmanCommand.php @@ -150,7 +150,7 @@ protected function getReflectionMethod(array $routeAction): ?object $routeData = explode('@', $routeAction['uses']); $reflection = new ReflectionClass($routeData[0]); - if ($this->config['available_methods_only'] && ! $reflection->hasMethod($routeData[1])) { + if (! $reflection->hasMethod($routeData[1])) { return null; }