From 5e4c0677f7dc122827e491a86c9b989ce6ec4b17 Mon Sep 17 00:00:00 2001 From: Latrell Chan Date: Sun, 8 Mar 2015 15:20:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BALaravel5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 0 .travis.yml | 0 composer.json | 0 phpunit.xml | 0 public/css/fonts.css | 0 ...G9GqCrobXxL-KRMQFhaRv2pGgT5Kf0An0s4MM.woff | Bin ...eUPV0v-yRb-cjciBsxEYwM7FgeyaSgU71cLG0.woff | Bin public/css/reset.css | 0 public/css/screen.css | 0 public/images/explorer_icons.png | Bin public/images/logo_small.png | Bin public/images/pet_store_api.png | Bin public/images/throbber.gif | Bin public/images/wordnik_api.png | Bin public/index.html | 0 public/lib/backbone-min.js | 0 public/lib/handlebars-1.0.0.js | 0 public/lib/highlight.7.3.pack.js | 0 public/lib/jquery-1.8.0.min.js | 0 public/lib/jquery.ba-bbq.min.js | 0 public/lib/jquery.slideto.min.js | 0 public/lib/jquery.wiggle.min.js | 0 public/lib/shred.bundle.js | 0 public/lib/shred/content.js | 0 public/lib/swagger-oauth.js | 0 public/lib/swagger.js | 0 public/lib/underscore-min.js | 0 public/o2c.html | 0 public/swagger-ui.js | 0 public/swagger-ui.min.js | 0 readme.md | 17 +- src/Latrell/Swagger/Swagger.php | 194 +++++++++--------- src/Latrell/Swagger/SwaggerException.php | 0 .../Swagger/SwaggerServiceProvider.php | 18 +- src/config/.gitkeep | 0 src/config/config.php | 0 src/controllers/SwaggerController.php | 20 +- src/routes.php | 22 +- src/views/.gitkeep | 0 src/views/index.blade.php | 40 ++-- tests/.gitkeep | 0 41 files changed, 156 insertions(+), 155 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .travis.yml mode change 100644 => 100755 composer.json mode change 100644 => 100755 phpunit.xml mode change 100644 => 100755 public/css/fonts.css mode change 100644 => 100755 public/css/fonts/EFpQQyG9GqCrobXxL-KRMQFhaRv2pGgT5Kf0An0s4MM.woff mode change 100644 => 100755 public/css/fonts/s-BiyweUPV0v-yRb-cjciBsxEYwM7FgeyaSgU71cLG0.woff mode change 100644 => 100755 public/css/reset.css mode change 100644 => 100755 public/css/screen.css mode change 100644 => 100755 public/images/explorer_icons.png mode change 100644 => 100755 public/images/logo_small.png mode change 100644 => 100755 public/images/pet_store_api.png mode change 100644 => 100755 public/images/throbber.gif mode change 100644 => 100755 public/images/wordnik_api.png mode change 100644 => 100755 public/index.html mode change 100644 => 100755 public/lib/backbone-min.js mode change 100644 => 100755 public/lib/handlebars-1.0.0.js mode change 100644 => 100755 public/lib/highlight.7.3.pack.js mode change 100644 => 100755 public/lib/jquery-1.8.0.min.js mode change 100644 => 100755 public/lib/jquery.ba-bbq.min.js mode change 100644 => 100755 public/lib/jquery.slideto.min.js mode change 100644 => 100755 public/lib/jquery.wiggle.min.js mode change 100644 => 100755 public/lib/shred.bundle.js mode change 100644 => 100755 public/lib/shred/content.js mode change 100644 => 100755 public/lib/swagger-oauth.js mode change 100644 => 100755 public/lib/swagger.js mode change 100644 => 100755 public/lib/underscore-min.js mode change 100644 => 100755 public/o2c.html mode change 100644 => 100755 public/swagger-ui.js mode change 100644 => 100755 public/swagger-ui.min.js mode change 100644 => 100755 readme.md mode change 100644 => 100755 src/Latrell/Swagger/Swagger.php mode change 100644 => 100755 src/Latrell/Swagger/SwaggerException.php mode change 100644 => 100755 src/Latrell/Swagger/SwaggerServiceProvider.php mode change 100644 => 100755 src/config/.gitkeep mode change 100644 => 100755 src/config/config.php mode change 100644 => 100755 src/controllers/SwaggerController.php mode change 100644 => 100755 src/routes.php mode change 100644 => 100755 src/views/.gitkeep mode change 100644 => 100755 src/views/index.blade.php mode change 100644 => 100755 tests/.gitkeep diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 diff --git a/phpunit.xml b/phpunit.xml old mode 100644 new mode 100755 diff --git a/public/css/fonts.css b/public/css/fonts.css old mode 100644 new mode 100755 diff --git a/public/css/fonts/EFpQQyG9GqCrobXxL-KRMQFhaRv2pGgT5Kf0An0s4MM.woff b/public/css/fonts/EFpQQyG9GqCrobXxL-KRMQFhaRv2pGgT5Kf0An0s4MM.woff old mode 100644 new mode 100755 diff --git a/public/css/fonts/s-BiyweUPV0v-yRb-cjciBsxEYwM7FgeyaSgU71cLG0.woff b/public/css/fonts/s-BiyweUPV0v-yRb-cjciBsxEYwM7FgeyaSgU71cLG0.woff old mode 100644 new mode 100755 diff --git a/public/css/reset.css b/public/css/reset.css old mode 100644 new mode 100755 diff --git a/public/css/screen.css b/public/css/screen.css old mode 100644 new mode 100755 diff --git a/public/images/explorer_icons.png b/public/images/explorer_icons.png old mode 100644 new mode 100755 diff --git a/public/images/logo_small.png b/public/images/logo_small.png old mode 100644 new mode 100755 diff --git a/public/images/pet_store_api.png b/public/images/pet_store_api.png old mode 100644 new mode 100755 diff --git a/public/images/throbber.gif b/public/images/throbber.gif old mode 100644 new mode 100755 diff --git a/public/images/wordnik_api.png b/public/images/wordnik_api.png old mode 100644 new mode 100755 diff --git a/public/index.html b/public/index.html old mode 100644 new mode 100755 diff --git a/public/lib/backbone-min.js b/public/lib/backbone-min.js old mode 100644 new mode 100755 diff --git a/public/lib/handlebars-1.0.0.js b/public/lib/handlebars-1.0.0.js old mode 100644 new mode 100755 diff --git a/public/lib/highlight.7.3.pack.js b/public/lib/highlight.7.3.pack.js old mode 100644 new mode 100755 diff --git a/public/lib/jquery-1.8.0.min.js b/public/lib/jquery-1.8.0.min.js old mode 100644 new mode 100755 diff --git a/public/lib/jquery.ba-bbq.min.js b/public/lib/jquery.ba-bbq.min.js old mode 100644 new mode 100755 diff --git a/public/lib/jquery.slideto.min.js b/public/lib/jquery.slideto.min.js old mode 100644 new mode 100755 diff --git a/public/lib/jquery.wiggle.min.js b/public/lib/jquery.wiggle.min.js old mode 100644 new mode 100755 diff --git a/public/lib/shred.bundle.js b/public/lib/shred.bundle.js old mode 100644 new mode 100755 diff --git a/public/lib/shred/content.js b/public/lib/shred/content.js old mode 100644 new mode 100755 diff --git a/public/lib/swagger-oauth.js b/public/lib/swagger-oauth.js old mode 100644 new mode 100755 diff --git a/public/lib/swagger.js b/public/lib/swagger.js old mode 100644 new mode 100755 diff --git a/public/lib/underscore-min.js b/public/lib/underscore-min.js old mode 100644 new mode 100755 diff --git a/public/o2c.html b/public/o2c.html old mode 100644 new mode 100755 diff --git a/public/swagger-ui.js b/public/swagger-ui.js old mode 100644 new mode 100755 diff --git a/public/swagger-ui.min.js b/public/swagger-ui.min.js old mode 100644 new mode 100755 diff --git a/readme.md b/readme.md old mode 100644 new mode 100755 index 8a93742..bae4896 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ Swagger ====== -Swagger for Laravel +Swagger for Laravel5 This package combines [swagger-php](https://github.com/zircote/swagger-php) and [swagger-ui](https://github.com/wordnik/swagger-ui) into one Laravel-friendly package. @@ -23,23 +23,18 @@ Update your packages with ```composer update``` or install with ```composer inst To use the Swagger Service Provider, you must register the provider when bootstrapping your Laravel application. There are essentially two ways to do this. -Find the `providers` key in `app/config/app.php` and register the Smarty Service Provider. +Find the `providers` key in `config/app.php` and register the Swagger Service Provider. ```php - 'providers' => array( + 'providers' => [ // ... 'Latrell\Swagger\SwaggerServiceProvider', - ) + ] ``` -Run php artisan `swagger:install` to push swagger-ui to your public folder and publish the config file. +Run `php artisan vendor:publish` to push swagger-ui to your public folder and publish the config file. -OR - -Then publish the public files with `php artisan asset:publish latrell/swagger` to push swagger-ui to your public folder. - -Then publish the config file with `php artisan config:publish latrell/swagger`. This will add the file `app/config/packages/latrell/swagger/config.php`. -This config file is the primary way you interact with Swagger. +Config file `config/latrell-swagger.php` is the primary way you interact with Swagger. ## Example diff --git a/src/Latrell/Swagger/Swagger.php b/src/Latrell/Swagger/Swagger.php old mode 100644 new mode 100755 index 49cb0ff..e9dcf3c --- a/src/Latrell/Swagger/Swagger.php +++ b/src/Latrell/Swagger/Swagger.php @@ -4,112 +4,112 @@ class Swagger { - protected $options; + protected $options; - public function __construct($options = array()) - { - $this->options = $options; - } + public function __construct($options = []) + { + $this->options = $options; + } - public function fire() - { - $projectPaths = $this->realpaths($this->paths); - $excludePaths = $this->realpaths($this->exclude); - $outputPath = head($this->realpaths(base_path($this->output))) . DIRECTORY_SEPARATOR; + public function fire() + { + $projectPaths = $this->realpaths($this->paths); + $excludePaths = $this->realpaths($this->exclude); + $outputPath = head($this->realpaths(base_path($this->output))) . DIRECTORY_SEPARATOR; - $swagger = new \Swagger\Swagger($projectPaths, $excludePaths); + $swagger = new \Swagger\Swagger($projectPaths, $excludePaths); - $resourceList = $swagger->getResourceList(array( - 'output' => 'array', - 'suffix' => $this->suffix, - 'apiVersion' => $this->default_api_version, - 'swaggerVersion' => $this->default_swagger_version, - 'template' => $this->api_doc_template - )); - $resourceOptions = array( - 'output' => 'json', - 'defaultSwaggerVersion' => $resourceList['swaggerVersion'], - 'defaultBasePath' => $this->default_base_path - ); - if (isset($resourceList['apiVersion'])) { - $resourceOptions['defaultApiVersion'] = $resourceList['apiVersion']; - } + $resourceList = $swagger->getResourceList([ + 'output' => 'array', + 'suffix' => $this->suffix, + 'apiVersion' => $this->default_api_version, + 'swaggerVersion' => $this->default_swagger_version, + 'template' => $this->api_doc_template + ]); + $resourceOptions = [ + 'output' => 'json', + 'defaultSwaggerVersion' => $resourceList['swaggerVersion'], + 'defaultBasePath' => $this->default_base_path + ]; + if (isset($resourceList['apiVersion'])) { + $resourceOptions['defaultApiVersion'] = $resourceList['apiVersion']; + } - $resourceName = false; - $output = array(); - foreach ($swagger->getResourceNames() as $resourceName) { - $json = $swagger->getResource($resourceName, $resourceOptions); - $resourceName = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($resourceName, DIRECTORY_SEPARATOR)); - $output[$resourceName] = $json; - } - if (! $output) { - throw new SwaggerException('no valid resources found'); - } - if (file_exists($outputPath) && ! is_dir($outputPath)) { - throw new SwaggerException(sprintf('[%s] is not a directory', $outputPath)); - } elseif (! file_exists($outputPath) && ! mkdir($outputPath, 0755, true)) { - throw new SwaggerException(sprintf('[%s] is not writeable', $outputPath)); - } + $resourceName = false; + $output = []; + foreach ($swagger->getResourceNames() as $resourceName) { + $json = $swagger->getResource($resourceName, $resourceOptions); + $resourceName = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($resourceName, DIRECTORY_SEPARATOR)); + $output[$resourceName] = $json; + } + if (! $output) { + throw new SwaggerException('no valid resources found'); + } + if (file_exists($outputPath) && ! is_dir($outputPath)) { + throw new SwaggerException(sprintf('[%s] is not a directory', $outputPath)); + } elseif (! file_exists($outputPath) && ! mkdir($outputPath, 0755, true)) { + throw new SwaggerException(sprintf('[%s] is not writeable', $outputPath)); + } - $filename = $outputPath . 'api-docs.json'; - if (file_put_contents($filename, \Swagger\Swagger::jsonEncode($resourceList, true))) { - $this->logger('Created ' . $filename); - } - foreach ($output as $name => $json) { - $name = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($name, DIRECTORY_SEPARATOR)); - $filename = $outputPath . $name . '.json'; - $this->logger('Created ' . $filename); - file_put_contents($filename, $json); - } - $this->logger(''); - } + $filename = $outputPath . 'api-docs.json'; + if (file_put_contents($filename, \Swagger\Swagger::jsonEncode($resourceList, true))) { + $this->logger('Created ' . $filename); + } + foreach ($output as $name => $json) { + $name = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($name, DIRECTORY_SEPARATOR)); + $filename = $outputPath . $name . '.json'; + $this->logger('Created ' . $filename); + file_put_contents($filename, $json); + } + $this->logger(''); + } - protected function logger() - { - // echo join('', func_get_args()), PHP_EOL; - } + protected function logger() + { + // echo join('', func_get_args()), PHP_EOL; + } - /** - * 检查并转换路径为绝对路径。 - * - * @param array $paths - * @throws SwaggerException - * @return array - */ - protected function realpaths($paths) - { - if (is_string($paths)) { - $paths = array( - $paths - ); - } - if (! is_array($paths)) { - return array(); - } - foreach ($paths as $i => $path) { - $paths[$i] = realpath($path); - if ($paths[$i] === false) { - $paths[$i] = realpath(base_path($path)); - if ($paths[$i] === false) { - throw new SwaggerException("Path \"{$path}\" not found"); - } - } - } - return $paths; - } + /** + * 检查并转换路径为绝对路径。 + * + * @param array $paths + * @throws SwaggerException + * @return array + */ + protected function realpaths($paths) + { + if (is_string($paths)) { + $paths = [ + $paths + ]; + } + if (! is_array($paths)) { + return []; + } + foreach ($paths as $i => $path) { + $paths[$i] = realpath($path); + if ($paths[$i] === false) { + $paths[$i] = realpath(base_path($path)); + if ($paths[$i] === false) { + throw new SwaggerException("Path \"{$path}\" not found"); + } + } + } + return $paths; + } - public function __set($key, $value) - { - $this->options[$key] = $value; - } + public function __set($key, $value) + { + $this->options[$key] = $value; + } - public function __get($key) - { - return $this->options[$key]; - } + public function __get($key) + { + return $this->options[$key]; + } - public function __isset($key) - { - return isset($this->options[$key]); - } + public function __isset($key) + { + return isset($this->options[$key]); + } } \ No newline at end of file diff --git a/src/Latrell/Swagger/SwaggerException.php b/src/Latrell/Swagger/SwaggerException.php old mode 100644 new mode 100755 diff --git a/src/Latrell/Swagger/SwaggerServiceProvider.php b/src/Latrell/Swagger/SwaggerServiceProvider.php old mode 100644 new mode 100755 index 8711ae9..63edb87 --- a/src/Latrell/Swagger/SwaggerServiceProvider.php +++ b/src/Latrell/Swagger/SwaggerServiceProvider.php @@ -11,7 +11,17 @@ class SwaggerServiceProvider extends ServiceProvider */ public function boot() { - if (config('latrell/swagger/config.enable')) { + $this->publishes([ + __DIR__ . '/../../config/config.php' => config_path('latrell-swagger.php') + ]); + + $this->loadViewsFrom(__DIR__ . '/../../views', 'latrell/swagger'); + + $this->publishes([ + __DIR__ . '/../../../public' => public_path('vendor/latrell/swagger') + ], 'public'); + + if (config('latrell-swagger.enable')) { require __DIR__ . '/../../routes.php'; } } @@ -23,10 +33,6 @@ public function boot() */ public function register() { - $configPath = __DIR__ . '/../../config/config.php'; - $this->mergeConfigFrom($configPath, 'latrell/swagger/config'); - $this->publishes([ - $configPath => config_path('latrell/swagger/config.php') - ]); + $this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'latrell-swagger'); } } diff --git a/src/config/.gitkeep b/src/config/.gitkeep old mode 100644 new mode 100755 diff --git a/src/config/config.php b/src/config/config.php old mode 100644 new mode 100755 diff --git a/src/controllers/SwaggerController.php b/src/controllers/SwaggerController.php old mode 100644 new mode 100755 index 027b388..38a656d --- a/src/controllers/SwaggerController.php +++ b/src/controllers/SwaggerController.php @@ -14,14 +14,14 @@ public function getIndex() { $swagger = new Swagger(); - $swagger->paths = Config::get('swagger::paths'); - $swagger->exclude = Config::get('swagger::exclude'); - $swagger->output = Config::get('swagger::output'); - $swagger->suffix = Config::get('swagger::suffix'); - $swagger->default_api_version = Config::get('swagger::default-api-version'); - $swagger->default_swagger_version = Config::get('swagger::default-swagger-version'); - $swagger->api_doc_template = Config::get('swagger::api-doc-template'); - $swagger->default_base_path = Config::get('swagger::default-base-path'); + $swagger->paths = config('latrell-swagger.paths'); + $swagger->exclude = config('latrell-swagger.exclude'); + $swagger->output = config('latrell-swagger.output'); + $swagger->suffix = config('latrell-swagger.suffix'); + $swagger->default_api_version = config('latrell-swagger.default-api-version'); + $swagger->default_swagger_version = config('latrell-swagger.default-swagger-version'); + $swagger->api_doc_template = config('latrell-swagger.api-doc-template'); + $swagger->default_base_path = config('latrell-swagger.default-base-path'); if (is_null($swagger->default_base_path)) { $swagger->default_base_path = Config::get('app.url'); @@ -29,12 +29,12 @@ public function getIndex() $swagger->fire(); - return View::make('swagger::index'); + return view('latrell/swagger::index'); } public function getDocs($page = 'api-docs.json') { - $path = base_path(Config::get('swagger::output') . '/' . $page); + $path = base_path(config('latrell-swagger.output') . '/' . $page); if (! file_exists($path)) { App::abort(404); } diff --git a/src/routes.php b/src/routes.php old mode 100644 new mode 100755 index 0dfd7a5..14ed502 --- a/src/routes.php +++ b/src/routes.php @@ -1,16 +1,16 @@ Config::get('swagger::prefix') -), function () +Route::group([ + 'prefix' => config('latrell-swagger.prefix') +], function () { - Route::get('/', array( - 'as' => 'swagger_index', - 'uses' => 'Latrell\Swagger\SwaggerController@getIndex' - )); + Route::get('/', [ + 'as' => 'swagger_index', + 'uses' => 'Latrell\Swagger\SwaggerController@getIndex' + ]); - Route::get('docs/{page?}', array( - 'as' => 'swagger_docs', - 'uses' => 'Latrell\Swagger\SwaggerController@getDocs' - )); + Route::get('docs/{page?}', [ + 'as' => 'swagger_docs', + 'uses' => 'Latrell\Swagger\SwaggerController@getDocs' + ]); }); \ No newline at end of file diff --git a/src/views/.gitkeep b/src/views/.gitkeep old mode 100644 new mode 100755 diff --git a/src/views/index.blade.php b/src/views/index.blade.php old mode 100644 new mode 100755 index 5b069a9..4ddb741 --- a/src/views/index.blade.php +++ b/src/views/index.blade.php @@ -1,27 +1,27 @@ - {{ Config::get('swagger::title'); }} + {{ config('latrell-swagger.title') }} - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - +