Skip to content

Commit 0219f14

Browse files
author
Vignesh C
committed
tags added to the docs by its controller name
1 parent d4c3aa0 commit 0219f14

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/GenerateSwaggerDoc.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ class GenerateSwaggerDoc extends Command
1313
*/
1414
protected $signature = 'laravel-swagger:generate
1515
{--format=json : The format of the output, current options are json and yaml}
16-
{--filter= : Filter to a specific route prefix, such as /api or /v2/api}
17-
{--auth= : Authentication to be applied globally}';
16+
{--filter=api : Filter to a specific route prefix, such as /api or /v2/api}
17+
{--auth=jwt : Authentication to be applied globally}
18+
{--host= : Host name of the swagger if you wish to change the default}';
1819

1920
/**
2021
* The console command description.
@@ -32,7 +33,7 @@ public function handle()
3233
{
3334
$config = config('laravel-swagger');
3435

35-
$docs = (new Generator($config, $this->option('filter') ?: null, $this->option('auth') ?: null))->generate();
36+
$docs = (new Generator($config, $this->option('filter') ?: null, $this->option('auth') ?: null, $this->option('host') ?: null))->generate();
3637

3738
$formattedDocs = (new FormatterManager($docs))
3839
->setFormat($this->option('format'))

src/Generator.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Generator
1515

1616
protected $auth;
1717

18+
protected $host;
19+
1820
protected $docs;
1921

2022
protected $uri;
@@ -25,11 +27,12 @@ class Generator
2527

2628
protected $action;
2729

28-
public function __construct($config, $routeFilter = null, $auth = null)
30+
public function __construct($config, $routeFilter = null, $auth = null, $host = null)
2931
{
3032
$this->config = $config;
3133
$this->routeFilter = $routeFilter;
3234
$this->auth = $auth;
35+
$this->host = $host;
3336
}
3437

3538
public function generate()
@@ -59,6 +62,8 @@ public function generate()
5962

6063
$this->generatePath();
6164

65+
$this->addTags($route->getAction());
66+
6267
$this->addAuthParameters($route->middleware());
6368
}
6469
}
@@ -75,7 +80,7 @@ protected function getBaseInfo()
7580
'description' => $this->config['description'],
7681
'version' => $this->config['appVersion'],
7782
],
78-
'host' => $this->config['host'],
83+
'host' => !empty($this->host) ? $this->host : $this->config['host'],
7984
'basePath' => $this->config['basePath'],
8085
];
8186

@@ -136,6 +141,7 @@ protected function generatePath()
136141

137142
$this->docs['paths'][$this->uri][$this->method] = [
138143
'description' => "$methodDescription {$this->uri}",
144+
'tags' => [],
139145
'responses' => [
140146
'200' => [
141147
'description' => 'OK'
@@ -180,6 +186,23 @@ protected function getFormRules()
180186
}
181187
}
182188

189+
protected function addTags($controllerArray)
190+
{
191+
$tagName = $this->getControllerName($controllerArray);
192+
$this->docs['paths'][$this->uri][$this->method]['tags'][] = $tagName;
193+
}
194+
195+
protected function getControllerName($controllerArray)
196+
{
197+
$namespaceReplaced = str_replace($controllerArray['namespace']. '\\', '', $controllerArray['controller']);
198+
$actionNameReplaced = substr($namespaceReplaced, 0, strpos($namespaceReplaced, '@'));
199+
$controllerReplaced = str_replace('Controller', '', $actionNameReplaced);
200+
$controllerNameArray = preg_split('/(?=[A-Z])/', $controllerReplaced);
201+
$controllerName = trim(implode(' ', $controllerNameArray));
202+
203+
return $controllerName;
204+
}
205+
183206
protected function addAuthParameters($middlewares)
184207
{
185208
if(!empty($this->auth)) {

0 commit comments

Comments
 (0)