Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi config support (v2.0) #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
multi config support
  • Loading branch information
Sano000 committed Jun 15, 2015
commit 1cd08f221aa4920b7e966e5d7d6fdbe25ed02ee2
36 changes: 35 additions & 1 deletion src/Jlapp/Swaggervel/SwaggervelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Jlapp\Swaggervel;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;

class SwaggervelServiceProvider extends ServiceProvider {
Expand Down Expand Up @@ -43,7 +44,40 @@ public function register()
__DIR__.'/../../config/swaggervel.php', 'swaggervel'
);

require_once __DIR__ .'/routes.php';
foreach($this->getMultiKeys() as $configName) {
if (Config::get("$configName.active")) {
require __DIR__ .'/routes.php';
}
}
}


/**
* Get additional configs
*
* @return array
*/
public function getMultiKeys()
{
$result = ['swaggervel'];

$additional = Config::get('swaggervel.additional');
if ($additional) {
is_array($additional) || ($additional = (array) $additional);

$this->mergeConfigFrom(
__DIR__.'/../../config/swaggervel.php', 'swagger.admin'
);

foreach ($additional as $key) {
$this->mergeConfigFrom(
__DIR__.'/../../config/swaggervel.php', $key
);

$result[] = $key;
}
}

return $result;
}
}
33 changes: 17 additions & 16 deletions src/Jlapp/Swaggervel/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use Swagger\Swagger;

Route::any(Config::get('swaggervel.doc-route').'/{page?}', function($page='api-docs.json') {
$filePath = Config::get('swaggervel.doc-dir') . "/{$page}";
Route::any(Config::get("$configName.doc-route").'/{page?}', function($page='api-docs.json') use ($configName) {
$filePath = Config::get("$configName.doc-dir") . "/{$page}";

if (File::extension($filePath) === "") {
if (File::extension($filePath) !== "json") {
$filePath .= ".json";
}
if (!File::Exists($filePath)) {
Expand All @@ -18,10 +18,10 @@
));
});

Route::get('api-docs', function() {
if (Config::get('swaggervel.generateAlways')) {
$appDir = base_path()."/".Config::get('swaggervel.app-dir');
$docDir = Config::get('swaggervel.doc-dir');
Route::get(Config::get("$configName.api-docs-route"), function() use ($configName) {
if (Config::get("$configName.generateAlways")) {
$appDir = base_path()."/".Config::get("$configName.app-dir");
$docDir = Config::get("$configName.doc-dir");

if (!File::exists($docDir) || is_writable($docDir)) {
// delete all existing documentation
Expand All @@ -31,10 +31,10 @@

File::makeDirectory($docDir);

$defaultBasePath = Config::get('swaggervel.default-base-path');
$defaultApiVersion = Config::get('swaggervel.default-api-version');
$defaultSwaggerVersion = Config::get('swaggervel.default-swagger-version');
$excludeDirs = Config::get('swaggervel.excludes');
$defaultBasePath = Config::get("$configName.default-base-path");
$defaultApiVersion = Config::get("$configName.default-api-version");
$defaultSwaggerVersion = Config::get("$configName.default-swagger-version");
$excludeDirs = Config::get("$configName.excludes");

$swagger = new Swagger($appDir, $excludeDirs);

Expand Down Expand Up @@ -67,7 +67,7 @@
}
}

if (Config::get('swaggervel.behind-reverse-proxy')) {
if (Config::get("$configName.behind-reverse-proxy")) {
$proxy = Request::server('REMOTE_ADDR');
Request::setTrustedProxies(array($proxy));
}
Expand All @@ -78,8 +78,9 @@
//need the / at the end to avoid CORS errors on Homestead systems.
$response = response()->view('swaggervel::index', array(
'secure' => Request::secure(),
'urlToDocs' => url(Config::get('swaggervel.doc-route')),
'requestHeaders' => Config::get('swaggervel.requestHeaders') )
'apiKey' => Config::get("$configName.api-key"),
'urlToDocs' => url(Config::get("$configName.doc-route")),
'requestHeaders' => Config::get("$configName.requestHeaders") )
);

//need the / at the end to avoid CORS errors on Homestead systems.
Expand All @@ -92,8 +93,8 @@
200
);*/

if (Config::has('swaggervel.viewHeaders')) {
foreach (Config::get('swaggervel.viewHeaders') as $key => $value) {
if (Config::has("$configName.viewHeaders")) {
foreach (Config::get("$configName.viewHeaders") as $key => $value) {
$response->header($key, $value);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/config/swaggervel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

return array(
'active' => true,

/*
|--------------------------------------------------------------------------
| Absolute path to location where parsed swagger annotations will be stored
Expand All @@ -21,6 +23,13 @@
*/
'doc-route' => 'docs',

/*
|--------------------------------------------------------------------------
| Relative path to access documentation.
|--------------------------------------------------------------------------
*/
'api-docs-route' => 'api-docs',

/*
|--------------------------------------------------------------------------
| Absolute path to directory containing the swagger annotations are stored.
Expand Down
38 changes: 19 additions & 19 deletions src/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Droid+Sans:400,700">
<link rel="stylesheet" href="vendor/swaggervel/css/reset.css">
<link rel="stylesheet" href="vendor/swaggervel/css/reset.css">
<link rel="stylesheet" href="vendor/swaggervel/css/screen.css">
<link rel="stylesheet" href="vendor/swaggervel/css/screen.css">
<link rel="stylesheet" href="{{ URL::asset('vendor/swaggervel/css/reset.css') }}">
<link rel="stylesheet" href="{{ URL::asset('vendor/swaggervel/css/reset.css') }}">
<link rel="stylesheet" href="{{ URL::asset('vendor/swaggervel/css/screen.css') }}">
<link rel="stylesheet" href="{{ URL::asset('vendor/swaggervel/css/screen.css') }}">

<script src="vendor/swaggervel/lib/shred.bundle.js"></script>
<script src="vendor/swaggervel/lib/jquery-1.8.0.min.js"></script>
<script src="vendor/swaggervel/lib/jquery.slideto.min.js"></script>
<script src="vendor/swaggervel/lib/jquery.wiggle.min.js"></script>
<script src="vendor/swaggervel/lib/jquery.ba-bbq.min.js"></script>
<script src="vendor/swaggervel/lib/handlebars-1.0.0.js"></script>
<script src="vendor/swaggervel/lib/underscore-min.js"></script>
<script src="vendor/swaggervel/lib/backbone-min.js"></script>
<script src="vendor/swaggervel/lib/swagger.js"></script>
<script src="vendor/swaggervel/swagger-ui.js"></script>
<script src="vendor/swaggervel/lib/highlight.7.3.pack.js"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/shred.bundle.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/jquery-1.8.0.min.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/jquery.slideto.min.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/jquery.wiggle.min.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/jquery.ba-bbq.min.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/handlebars-1.0.0.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/underscore-min.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/backbone-min.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/swagger.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/swagger-ui.js') }}"></script>
<script src="{{ URL::asset('vendor/swaggervel/lib/highlight.7.3.pack.js') }}"></script>

<!-- enabling this will enable oauth2 implicit scope support -->
{{-- {{ HTML::script('packages/jlapp/swaggervel/lib/swagger-oauth.js' , array(), $secure); !!}--}}
{{-- {!! HTML::script('packages/jlapp/swaggervel/lib/swagger-oauth.js' , array(), $secure); !!} --}}

<script type="text/javascript">
$(function () {
Expand Down Expand Up @@ -64,7 +64,7 @@
log("key: " + key);
if (key && key.trim() != "") {
log("added key " + key);
window.authorizations.add("key", new ApiKeyAuthorization("{!! Config::get('swaggervel.api-key') !!}", key, "query"));
window.authorizations.add("key", new ApiKeyAuthorization("{!! $apiKey !!}", key, "query"));
} else {
window.authorizations.remove("key");
}
Expand All @@ -80,10 +80,10 @@

<form id='api_selector'>
<div class='input icon-btn'>
<image id = "show-pet-store-icon" title = "Show Swagger Petstore Example Apis" src="vendor/swaggervel/images/pet_store_api.png" />
<image id = "show-pet-store-icon" title = "Show Swagger Petstore Example Apis" src="{{ URL::asset('vendor/swaggervel/images/pet_store_api.png') }}" />
</div>
<div class='input icon-btn'>
<image id = "show-wordnik-dev-icon" title = "Show Wordnik Developer Apis" src="vendor/swaggervel/images/wordnik_api.png" />
<image id = "show-wordnik-dev-icon" title = "Show Wordnik Developer Apis" src="{{ URL::asset('vendor/swaggervel/images/wordnik_api.png') }}" />
</div>
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl"
type="text"/></div>
Expand Down