diff --git a/README.md b/README.md index 1ee2752..5cab306 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,27 @@ # laravel-postman -This package allows you to export your API routes to a postman import json file +This package allows you to export your API routes to a postman import json file, original work by https://github.com/sojeda/laravel-postman ## Installation Install the package via composer -`composer require --dev jimenezmaximiliano/laravel-postman` +`composer require --dev phpsa/laravel-postman` Then add the service provider in config/app.php: ### PHP >= 5.5 -`JimenezMaximiliano\LaravelPostman\LaravelPostmanServiceProvider::class` +`Phpsa\LaravelPostman\ServiceProvider::class` ### PHP < 5.5 -`JimenezMaximiliano\LaravelPostman\LaravelPostmanServiceProvider` +`Phpsa\LaravelPostman\ServiceProvider` ## Configuration First, publish the package configuration file: -`php artisan vendor:publish` +`php artisan vendor:publish --provider="Phpsa\LaravelPostman\ServiceProvider" --tag="config"` Note: publishing the configuration file is optional, you can use de default package options. @@ -78,4 +78,4 @@ params that you want to see in postman. ### Export -`php artisan laravelPostman:export` \ No newline at end of file +`php artisan laravelPostman:export` diff --git a/composer.json b/composer.json index d1eea2d..3b50346 100644 --- a/composer.json +++ b/composer.json @@ -1,18 +1,23 @@ { - "name": "jimenezmaximiliano/laravel-postman", + "name": "phpsa/laravel-postman", "type": "library", "description": "Export laravel API routes to postman", "keywords": [ - "jimenezmaximiliano", "laravel", "postman", "github", "export", "api" ], - "homepage": "https://github.com/jimenezmaximiliano/laravel-postman", + "homepage": "https://github.com/phpsa/laravel-postman", "license": "MIT", "authors": [ + { + "name": "Craig Smith", + "email": "vxdhost@gmail.com", + "role" : "developer" + + }, { "name": "Maximiliano Jimenez", "email": "jimenez@maximiliano.com.ar", @@ -21,27 +26,28 @@ } ], "require": { - "illuminate/support": "~5", - "illuminate/console": "~5", - "php" : ">=5.4" + "illuminate/support": "^6.0", + "php" : ">=7.1" }, "require-dev": { - "phpunit/phpunit" : "~4.0||~5.0" + "phpunit/phpunit" : "^7.0||^8.0" }, "autoload": { "psr-4": { - "JimenezMaximiliano\\LaravelPostman\\": "src" + "Phpsa\\LaravelPostman\\": "src" } }, "scripts": { "test": "phpunit", "format": "phpcbf --standard=psr2 src/" }, - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } - }, + "extra": { + "laravel": { + "providers": [ + "Phpsa\\LaravelPostman\\ServiceProvider" + ] + } + }, "config": { "sort-packages": true } diff --git a/src/Helper.php b/src/Helper.php index d4e387b..fff9b73 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -1,22 +1,22 @@ addTrailingSlash($configURL); } - + return $this->addTrailingSlash(config('app.url')); } - + /** * Returns the API prefix string - * + * * @return string */ public function getApiPrefix() { $apiPrefix = config('postman.apiPrefix'); - + return !empty($apiPrefix) ? $apiPrefix : 'api'; } - + /** * Returns a postman collection structure array - * + * * @param string $collectionName * @param string $collectionDescription * @return array */ public function getCollectionStructure( - $collectionName, - $collectionDescription) - { + $collectionName, + $collectionDescription + ) { return [ 'variables' => [], 'info' => [ @@ -87,100 +87,106 @@ public function getCollectionStructure( 'item' => [], ]; } - + /** * Obtains a postman folder name from the given laravel route - * + * * @param Illuminate\Routing\Route $route * @return string */ public function getRouteFolder($route) { $actionStringParts = explode('@', $route->getActionName()); - + if (count($actionStringParts) === 1) { - - return 'Others'; + + return 'Others'; } - + $fullController = $actionStringParts[self::CONTROLLER_STRING_INDEX]; $controllerClass = explode('\\', $fullController); - + return str_replace('Controller', '', last($controllerClass)); } - + /** * Returns the path where the exported file will be located - * + * * @return string */ public function getExportDirectory() { $exportDirectory = config('postman.exportDirectory'); - + if (empty($exportDirectory)) { - + return $exportDirectory; } - + return $this->addTrailingSlash($exportDirectory); } - + /** * Finds out if a postman model can be get from the route - * + * * @param Illuminate\Routing\Route $route * @return boolean */ public function canGetPostmanModel($route) { - if (method_exists($route, 'getController') - && is_object($route->getController()) - && property_exists($route->getController(), 'postmanModel')) { - + if ( + method_exists($route, 'getController') + && is_object($route->getController()) + && property_exists($route->getController(), 'postmanModel') + ) { + return true; } - - if (method_exists($route, 'getAction') - && is_array($route->getAction()) - && in_array('controller', array_keys($route->getAction()))) { - + + if ( + method_exists($route, 'getAction') + && is_array($route->getAction()) + && in_array('controller', array_keys($route->getAction())) + ) { + return true; } - + return false; } - + /** * Returns a route's postman model - * + * * @param Illuminate\Routing\Route $route * @return object|null */ public function getPostmanModel($route) { if (!$this->canGetPostmanModel($route)) { - + return null; } - + if (method_exists($route, 'getController')) { - - $postmanModelClass = $route->getController()->postmanModel; + if (property_exists($route->getController(), 'postmanModel')) { + $postmanModelClass = $route->getController()->postmanModel; + return new $postmanModelClass(); + } } - + $action = $route->getAction(); $controllerAction = explode('@', $action['controller']); $controllerClass = $controllerAction[0]; $controller = app($controllerClass); - + if (!property_exists($controller, 'postmanModel')) { - + return null; } - + $postmanModelClass = $controller->postmanModel; - + return new $postmanModelClass(); } } diff --git a/src/LaravelPostmanCommand.php b/src/LaravelPostmanCommand.php index ac196d9..4bca129 100644 --- a/src/LaravelPostmanCommand.php +++ b/src/LaravelPostmanCommand.php @@ -1,14 +1,15 @@ getCollectionName(); $collectionDescription = $this->getCollectionDescription(); $collection = $this->helper->getCollectionStructure( - $collectionName, - $collectionDescription); - + $collectionName, + $collectionDescription + ); + foreach ($this->getRoutes() as $folderName => $folderRoutes) { - + $items = []; foreach ($folderRoutes as $route) { $items = array_merge($this->getRouteItems($route), $items); } - + $collection['item'][] = [ 'name' => $folderName, 'description' => '', 'item' => $items, ]; } - + file_put_contents( - $this->helper->getExportDirectory() . 'postman.json', - json_encode($collection)); + $this->helper->getExportDirectory() . 'postman.json', + json_encode($collection) + ); } - + /** * Returns an array of route items (route + method) for the given route - * + * * @param Illuminate\Routing\Route $route * @return array */ - protected function getRouteItems($route) + protected function getRouteItems(\Illuminate\Routing\Route $route) { $baseURL = $this->helper->getBaseURL(); $path = $this->helper->replaceGetParameters($route->uri()); @@ -82,28 +85,31 @@ protected function getRouteItems($route) $routeNameFinal = !empty($routeName) ? $routeName : $path; $methods = $route->methods(); $items = []; - + foreach ($methods as $method) { if ($method === 'HEAD' && config('postman.skipHEAD', true)) { - + continue; } $body = $this->getBody($route, $method); + $docs = $this->getDocs($route); $items[] = $this->getItemStructure( - $routeNameFinal, - $baseURL, - $path, - $method, - $body); + $routeNameFinal, + $baseURL, + $path, + $method, + $body, + $docs + ); } - + return $items; } - + /** * Returns an array with postman item format - * + * * @param string $routeName * @param string $baseURL * @param string $path @@ -112,15 +118,25 @@ protected function getRouteItems($route) * @return string */ protected function getItemStructure( - $routeName, - $baseURL, - $path, - $method, - $body) - { + $routeName, + $baseURL, + $path, + $method, + $body, + $docs + ) { + + + $parts = explode("\n", $docs); + $methodComment = trim($parts[1], "\t* "); + + // $args = $classMethod->getParameters(); + // dd($parts, $args, $docs, $method_comment); + return [ 'name' => $routeName, 'request' => [ + 'description' => $methodComment, 'url' => $baseURL . $path, 'method' => $method, 'body' => $body, @@ -128,45 +144,47 @@ protected function getItemStructure( 'response' => [], ]; } - + /** * Returns the user's collection name - * + * * @return string */ protected function getCollectionName() { $configCollectionName = config('postman.collectionName'); - + if (!empty($configCollectionName)) { - + return $configCollectionName; } - + return $this->ask('Enter collection name', 'LaravelPostman Collection'); } - + /** * Returns the user's collection description - * + * * @return string */ protected function getCollectionDescription() { $configCollectionDescription = config('postman.collectionDescription'); - + if (!empty($configCollectionDescription)) { - + return $configCollectionDescription; } - - return $this->ask('Enter collection description', - 'Postman collection generated by LaravelPostman'); + + return $this->ask( + 'Enter collection description', + 'Postman collection generated by LaravelPostman' + ); } - + /** * Returns an array of API routes organized by folders - * + * * @return array */ protected function getRoutes() @@ -174,42 +192,43 @@ protected function getRoutes() $resultRoutes = []; $apiPrefix = $this->helper->getApiPrefix(); $apiPrefixLength = strlen($apiPrefix); - + foreach (Route::getRoutes() as $route) { - + $path = $route->uri(); if (substr($path, 0, $apiPrefixLength) !== $apiPrefix) { $this->info('Omiting ' . $path); - + continue; } - + $routeFolder = $this->helper->getRouteFolder($route); if (!isset($resultRoutes[$routeFolder])) { $resultRoutes[$routeFolder] = []; } - + $resultRoutes[$routeFolder][] = $route; } - + return $resultRoutes; } - + /** * Returns an postman body array for the given route - * + * * @param Illuminate\Routing\Route $route * @param string $method * @return array */ - protected function getBody($route, $method) { - + protected function getBody($route, $method) + { + $postmanParams = $this->getRouteParams($route, $method); - + if (empty($postmanParams)) { return []; } - + $body['mode'] = 'urlencoded'; $body['urlencoded'] = []; foreach ($postmanParams as $param) { @@ -219,36 +238,45 @@ protected function getBody($route, $method) { 'enabled' => true, ]; } - + return $body; } - + /** * Returns an array of the given route parameters - * + * * @param Illuminate\Routing\Route $route * @param string $method * @return array */ - protected function getRouteParams($route, $method) + protected function getRouteParams(\Illuminate\Routing\Route $route, $method) { if ($method === 'GET' || $method === 'DELETE') { return []; } - + if (!$this->helper->canGetPostmanModel($route)) { return []; } - + $postmanModel = $this->helper->getPostmanModel($route); - - if (!is_object($postmanModel) - || !method_exists($postmanModel, 'getPostmanParams')) { - + + if ( + !is_object($postmanModel) + || !method_exists($postmanModel, 'getPostmanParams') + ) { + return []; } - + return $postmanModel->getPostmanParams(); } + + protected function getDocs(\Illuminate\Routing\Route $route) + { + $class = new ReflectionClass($route->getController()); + $classMethod = $class->getMethod($route->getActionMethod()); + return $classMethod->getDocComment(); + } } diff --git a/src/LaravelPostmanServiceProvider.php b/src/LaravelPostmanServiceProvider.php deleted file mode 100644 index 96c46a6..0000000 --- a/src/LaravelPostmanServiceProvider.php +++ /dev/null @@ -1,39 +0,0 @@ -app->singleton('JimenezMaximiliano\LaravelPostman\Helper', function($app) - { - return new Helper(); - }); - - $this->commands('JimenezMaximiliano\LaravelPostman\LaravelPostmanCommand'); - - $configFilePath = __DIR__ . '/../config/laravelPostman.php'; - $this->publishes([ - $configFilePath => config_path('laravelPostman.php'), - ]); - } -} \ No newline at end of file diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..2612eeb --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,44 @@ +publishes([ + self::CONFIG_PATH => config_path('postman.php'), + ], 'config'); + } + + /** + * Register any package services. + * + * @return void + */ + public function register() + { + + $this->mergeConfigFrom( + self::CONFIG_PATH, + 'postman' + ); + + $this->app->singleton('Phpsa\LaravelPostman\Helper', static function ($app) { + return new Helper(); + }); + + $this->commands('Phpsa\LaravelPostman\LaravelPostmanCommand'); + } +}