Skip to content

Commit

Permalink
修改为Laravel5支持。
Browse files Browse the repository at this point in the history
  • Loading branch information
latrell committed Mar 8, 2015
1 parent d5d5aee commit 5e4c067
Show file tree
Hide file tree
Showing 41 changed files with 156 additions and 155 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified composer.json
100644 → 100755
Empty file.
Empty file modified phpunit.xml
100644 → 100755
Empty file.
Empty file modified public/css/fonts.css
100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file modified public/css/reset.css
100644 → 100755
Empty file.
Empty file modified public/css/screen.css
100644 → 100755
Empty file.
Empty file modified public/images/explorer_icons.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified public/images/logo_small.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified public/images/pet_store_api.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified public/images/throbber.gif
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified public/images/wordnik_api.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified public/index.html
100644 → 100755
Empty file.
Empty file modified public/lib/backbone-min.js
100644 → 100755
Empty file.
Empty file modified public/lib/handlebars-1.0.0.js
100644 → 100755
Empty file.
Empty file modified public/lib/highlight.7.3.pack.js
100644 → 100755
Empty file.
Empty file modified public/lib/jquery-1.8.0.min.js
100644 → 100755
Empty file.
Empty file modified public/lib/jquery.ba-bbq.min.js
100644 → 100755
Empty file.
Empty file modified public/lib/jquery.slideto.min.js
100644 → 100755
Empty file.
Empty file modified public/lib/jquery.wiggle.min.js
100644 → 100755
Empty file.
Empty file modified public/lib/shred.bundle.js
100644 → 100755
Empty file.
Empty file modified public/lib/shred/content.js
100644 → 100755
Empty file.
Empty file modified public/lib/swagger-oauth.js
100644 → 100755
Empty file.
Empty file modified public/lib/swagger.js
100644 → 100755
Empty file.
Empty file modified public/lib/underscore-min.js
100644 → 100755
Empty file.
Empty file modified public/o2c.html
100644 → 100755
Empty file.
Empty file modified public/swagger-ui.js
100644 → 100755
Empty file.
Empty file modified public/swagger-ui.min.js
100644 → 100755
Empty file.
17 changes: 6 additions & 11 deletions readme.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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

Expand Down
194 changes: 97 additions & 97 deletions src/Latrell/Swagger/Swagger.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Empty file modified src/Latrell/Swagger/SwaggerException.php
100644 → 100755
Empty file.
18 changes: 12 additions & 6 deletions src/Latrell/Swagger/SwaggerServiceProvider.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
Expand All @@ -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');
}
}
Empty file modified src/config/.gitkeep
100644 → 100755
Empty file.
Empty file modified src/config/config.php
100644 → 100755
Empty file.
20 changes: 10 additions & 10 deletions src/controllers/SwaggerController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ 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');
}

$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);
}
Expand Down
22 changes: 11 additions & 11 deletions src/routes.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
Route::group(array(
'prefix' => 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'
]);
});
Empty file modified src/views/.gitkeep
100644 → 100755
Empty file.
Loading

0 comments on commit 5e4c067

Please sign in to comment.