Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 64 additions & 32 deletions config/l5-swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,170 +3,202 @@
return [

'api' => [

/*
|--------------------------------------------------------------------------
| Edit to set the api's title
|--------------------------------------------------------------------------
*/
*/

'title' => 'Swagger UI',

/*
|--------------------------------------------------------------------------
| Edit to set the api's Auth token
|--------------------------------------------------------------------------
*/
*/

'auth_token' => env('L5_SWAGGER_API_AUTH_TOKEN', false),

/*
|--------------------------------------------------------------------------
| Edit to set the api key variable in interface
|--------------------------------------------------------------------------
*/
*/

'key_var' => env('L5_SWAGGER_API_KEY_VAR', 'api_key'),

/*
|--------------------------------------------------------------------------
| Edit to set the securityDefinition that is used in requests
|--------------------------------------------------------------------------
*/

'security_definition' => env('L5_SWAGGER_API_SECURITY_DEFINITION', 'api_key'),

/*
|--------------------------------------------------------------------------
| Edit to set where to inject api key (header, query)
|--------------------------------------------------------------------------
*/
*/

'key_inject' => env('L5_SWAGGER_API_KEY_INJECT', 'query'),

/*
|--------------------------------------------------------------------------
| Edit to set the api's version number
|--------------------------------------------------------------------------
*/

'version' => env('L5_SWAGGER_API_VERSION', '1'),

/*
|--------------------------------------------------------------------------
| Edit to set the swagger version number
|--------------------------------------------------------------------------
*/
*/

'swagger_version' => env('L5_SWAGGER_DEFAULT_API_VERSION', '1'),

],

'routes' => [

/*
|--------------------------------------------------------------------------
| Route for accessing api documentation interface
|--------------------------------------------------------------------------
*/
*/

'api' => 'api/documentation',

/*
|--------------------------------------------------------------------------
| Route for accessing parsed swagger annotations.
|--------------------------------------------------------------------------
*/
*/

'docs' => 'docs',

],

'paths' => [

/*
|--------------------------------------------------------------------------
| Absolute path to location where parsed swagger annotations will be stored
|--------------------------------------------------------------------------
*/
*/

'docs' => storage_path('api-docs'),

/*
|--------------------------------------------------------------------------
| Absolute path to directory containing the swagger annotations are stored.
|--------------------------------------------------------------------------
*/
*/

'annotations' => base_path('app'),

/*
|--------------------------------------------------------------------------
| Absolute path to directory where to export assets
|--------------------------------------------------------------------------
*/
*/

'assets' => public_path('vendor/l5-swagger'),

/*
|--------------------------------------------------------------------------
| Path to assets public directory
|--------------------------------------------------------------------------
*/
*/

'assets_public' => '/vendor/l5-swagger',

/*
|--------------------------------------------------------------------------
| Absolute path to directory where to export views
|--------------------------------------------------------------------------
*/
*/

'views' => base_path('resources/views/vendor/l5-swagger'),

/*
|--------------------------------------------------------------------------
| Edit to set the api's base path
|--------------------------------------------------------------------------
*/

'base' => '',

/*
|--------------------------------------------------------------------------
| Absolute path to directories that you would like to exclude from swagger generation
|--------------------------------------------------------------------------
*/
*/

'excludes' => [],

],

/*
|--------------------------------------------------------------------------
| Turn this off to remove swagger generation on production
|--------------------------------------------------------------------------
*/
*/

'generate_always' => env('L5_SWAGGER_GENERATE_ALWAYS', false),

/*
|--------------------------------------------------------------------------
| Edit to set the swagger version number
|--------------------------------------------------------------------------
*/
*/

'swagger_version' => env('SWAGGER_VERSION', '2.0'),

/*
|--------------------------------------------------------------------------
| Edit to trust the proxy's ip address - needed for AWS Load Balancer
|--------------------------------------------------------------------------
*/
*/

'proxy' => false,

/*
|--------------------------------------------------------------------------
| Uncomment to pass the validatorUrl parameter to SwaggerUi init on the JS
| side. A null value here disables validation. A string will override
| the default url. If not specified, behavior is default and validation
| is enabled.
|--------------------------------------------------------------------------
*/
// 'validatorUrl' => null,
|--------------------------------------------------------------------------
| Uncomment to pass the validatorUrl parameter to SwaggerUi init on the JS
| side. A null value here disables validation. A string will override
| the default url. If not specified, behavior is default and validation
| is enabled.
|--------------------------------------------------------------------------
*/

//'validatorUrl' => null,

'headers' => [

/*
|--------------------------------------------------------------------------
| Uncomment to add response headers when swagger is generated
|--------------------------------------------------------------------------
*/
/*"view" => [
'Content-Type' => 'text/plain'
],*/
*/

//'view' => [
// 'Content-Type' => 'text/plain',
//],

/*
|--------------------------------------------------------------------------
| Uncomment to add request headers when swagger performs requests
|--------------------------------------------------------------------------
*/
/*"request" => [
'TestMe' => 'testValue'
],*/
*/

//'request' => [
// 'TestMe' => 'testValue',
//],

],

];
84 changes: 84 additions & 0 deletions src/Http/Controllers/SwaggerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace L5Swagger\Http\Controllers;

use File;
use Request;
use Response;
use L5Swagger\Generator;
use Illuminate\Routing\Controller as BaseController;

class SwaggerController extends BaseController
{
/**
* Dump api-docs.json content endpoint.
*
* @param string $page
* @return \Response
*/
public function docs($page = 'api-docs.json')
{
$filePath = config('l5-swagger.paths.docs') . '/' . $page;

if (File::extension($filePath) === '') {
$filePath .= '.json';
}
if (! File::exists($filePath)) {
abort(404, 'Cannot find ' . $filePath);
}

$content = File::get($filePath);

return Response::make($content, 200, [
'Content-Type' => 'application/json',
]);
}

/**
* Display Swagger API page.
*
* @return \Response
*/
public function api()
{
if (config('l5-swagger.generate_always')) {
Generator::generateDocs();
}

if (config('l5-swagger.proxy')) {
$proxy = Request::server('REMOTE_ADDR');
Request::setTrustedProxies([$proxy]);
}

$extras = [];
if (array_key_exists('validatorUrl', config('l5-swagger'))) {
// This allows for a null value, since this has potentially
// desirable side effects for swagger. See the view for more
// details.
$extras['validatorUrl'] = config('l5-swagger.validatorUrl');
}

// Need the / at the end to avoid CORS errors on Homestead systems.
$response = Response::make(
view('l5-swagger::index', [
'apiKey' => config('l5-swagger.api.auth_token'),
'apiKeyVar' => config('l5-swagger.api.key_var'),
'securityDefinition' => config('l5-swagger.api.security_definition'),
'apiKeyInject' => config('l5-swagger.api.key_inject'),
'secure' => Request::secure(),
'urlToDocs' => url(config('l5-swagger.routes.docs')),
'requestHeaders' => config('l5-swagger.headers.request'),
], $extras),
200
);

$headersView = config('l5-swagger.headers.view');
if (is_array($headersView) and ! empty($headersView)) {
foreach ($headersView as $key => $value) {
$response->header($key, $value);
}
}

return $response;
}
}
60 changes: 2 additions & 58 deletions src/routes.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,5 @@
<?php

use Swagger\Swagger;
$router->any(config('l5-swagger.routes.docs') . '/{page?}', '\L5Swagger\Http\Controllers\SwaggerController@docs');

$router->any(config('l5-swagger.routes.docs').'/{page?}', function ($page = 'api-docs.json') {
$filePath = config('l5-swagger.paths.docs')."/{$page}";

if (File::extension($filePath) === '') {
$filePath .= '.json';
}
if (!File::Exists($filePath)) {
abort(404, "Cannot find {$filePath}");
}

$content = File::get($filePath);

return Response::make($content, 200, [
'Content-Type' => 'application/json',
]);
});

$router->get(config('l5-swagger.routes.api'), function () {
if (config('l5-swagger.generate_always')) {
\L5Swagger\Generator::generateDocs();
}

if (config('l5-swagger.proxy')) {
$proxy = Request::server('REMOTE_ADDR');
Request::setTrustedProxies([$proxy]);
}

$extras = [];
if (array_key_exists('validatorUrl', config('l5-swagger'))) {
// This allows for a null value, since this has potentially
// desirable side effects for swagger. See the view for more
// details.
$extras['validatorUrl'] = config('l5-swagger.validatorUrl');
}

//need the / at the end to avoid CORS errors on Homestead systems.
$response = \Response::make(
view('l5-swagger::index', [
'apiKey' => config('l5-swagger.api.auth_token'),
'apiKeyVar' => config('l5-swagger.api.key_var'),
'securityDefinition' => config('l5-swagger.api.security_definition'),
'apiKeyInject' => config('l5-swagger.api.key_inject'),
'secure' => Request::secure(),
'urlToDocs' => url(config('l5-swagger.routes.docs')),
'requestHeaders' => config('l5-swagger.headers.request'),
], $extras),
200
);

if (is_array(config('l5-swagger.headers.view')) && !empty(config('l5-swagger.headers.view'))) {
foreach (config('l5-swagger.headers.view') as $key => $value) {
$response->header($key, $value);
}
}

return $response;
});
$router->get(config('l5-swagger.routes.api'), '\L5Swagger\Http\Controllers\SwaggerController@api');