Skip to content

Commit 4dbf4fd

Browse files
committed
added formatters
1 parent 18a576f commit 4dbf4fd

File tree

7 files changed

+105
-2
lines changed

7 files changed

+105
-2
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"Mtrajano\\LaravelSwagger\\": "src/"
1717
}
1818
},
19+
"suggest": {
20+
"ext-yaml": "Required to use the YAML output option"
21+
},
1922
"extra": {
2023
"laravel": {
2124
"providers": [

src/FormatterManager.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger;
4+
5+
class FormatterManager
6+
{
7+
private $docs;
8+
9+
private $formatter;
10+
11+
public function __construct($docs)
12+
{
13+
$this->docs = $docs;
14+
15+
$this->formatter = new Formatters\JsonFormatter($docs);
16+
}
17+
18+
public function setFormat($format)
19+
{
20+
$format = strtolower($format);
21+
22+
$this->formatter = $this->getFormatter($format);
23+
24+
return $this;
25+
}
26+
27+
protected function getFormatter($format)
28+
{
29+
switch($format) {
30+
case 'json':
31+
return new Formatters\JsonFormatter($this->docs);
32+
case 'yaml':
33+
return new Formatters\YamlFormatter($this->docs);
34+
default:
35+
throw new LaravelSwaggerException('Invalid format passed');
36+
}
37+
}
38+
39+
public function format()
40+
{
41+
return $this->formatter->format();
42+
}
43+
}

src/Formatters/Formatter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Formatters;
4+
5+
abstract class Formatter
6+
{
7+
protected $docs;
8+
9+
public function __construct($docs)
10+
{
11+
$this->docs = $docs;
12+
}
13+
14+
abstract public function format();
15+
}

src/Formatters/JsonFormatter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Formatters;
4+
5+
class JsonFormatter extends Formatter
6+
{
7+
public function format()
8+
{
9+
if (!extension_loaded('json')) {
10+
throw new \Exception('JSON extension must be loaded to use the json output format');
11+
}
12+
13+
return json_encode($this->docs, JSON_PRETTY_PRINT);
14+
}
15+
}

src/Formatters/YamlFormatter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Formatters;
4+
5+
class YamlFormatter extends Formatter
6+
{
7+
public function format()
8+
{
9+
if (!extension_loaded('yaml')) {
10+
throw new \Exception('YAML extension must be loaded to use the yaml output format');
11+
}
12+
13+
return yaml_emit($this->docs);
14+
}
15+
}

src/GenerateSwaggerDoc.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class GenerateSwaggerDoc extends Command
1111
*
1212
* @var string
1313
*/
14-
protected $signature = 'laravel-swagger:generate';
14+
protected $signature = 'laravel-swagger:generate
15+
{--format=json : The format of the output, current options are json and yaml}';
1516

1617
/**
1718
* The console command description.
@@ -31,6 +32,10 @@ public function handle()
3132

3233
$docs = (new Generator($config))->generate();
3334

34-
echo json_encode($docs, JSON_PRETTY_PRINT) . "\n";
35+
$formattedDocs = (new FormatterManager($docs))
36+
->setFormat($this->option('format'))
37+
->format();
38+
39+
$this->line($formattedDocs);
3540
}
3641
}

src/LaravelSwaggerException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger;
4+
5+
class LaravelSwaggerException extends \Exception
6+
{
7+
}

0 commit comments

Comments
 (0)