Skip to content

Commit 1bc333b

Browse files
authored
Merge pull request #2 from overint/route-filter
Add ability to filter routes to a certain prefix
2 parents bd69656 + 62df94f commit 1bc333b

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ You can also override the default config provided by the application by running
1616

1717
Generating the swagger documentation is easy, simply run `php artisan laravel-swagger:generate` in your project root. Keep in mind the command will simply print out the output in your console. If you want the docs saved in a file you can reroute the output like so: `php artisan laravel-swagger:generate > swagger.json`
1818

19+
If you wish to generate docs for a subset of your routes, you can pass a filter using `--filter`, for example: `php artisan laravel-swagger:generate --filter="/api"`
20+
1921
By default, laravel-swagger prints out the documentation in json format, if you want it in YAML format you can override the format using the `--format` flag. Make sure to have the yaml extension installed if you choose to do so.
2022

2123
Format options are:<br>

src/GenerateSwaggerDoc.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class GenerateSwaggerDoc extends Command
1212
* @var string
1313
*/
1414
protected $signature = 'laravel-swagger:generate
15-
{--format=json : The format of the output, current options are json and yaml}';
15+
{--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}';
1617

1718
/**
1819
* The console command description.
@@ -30,7 +31,7 @@ public function handle()
3031
{
3132
$config = config('laravel-swagger');
3233

33-
$docs = (new Generator($config))->generate();
34+
$docs = (new Generator($config, $this->option('filter') ?: null))->generate();
3435

3536
$formattedDocs = (new FormatterManager($docs))
3637
->setFormat($this->option('format'))

src/Generator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Generator
1111
{
1212
protected $config;
1313

14+
protected $routeFilter;
15+
1416
protected $docs;
1517

1618
protected $uri;
@@ -21,9 +23,10 @@ class Generator
2123

2224
protected $action;
2325

24-
public function __construct($config)
26+
public function __construct($config, $routeFilter = null)
2527
{
2628
$this->config = $config;
29+
$this->routeFilter = $routeFilter;
2730
}
2831

2932
public function generate()
@@ -33,6 +36,11 @@ public function generate()
3336
foreach ($this->getAppRoutes() as $route) {
3437
$this->originalUri = $uri = $this->getRouteUri($route);
3538
$this->uri = strip_optional_char($uri);
39+
40+
if ($this->routeFilter && !preg_match('/^' . preg_quote($this->routeFilter, '/') . '/', $this->uri)) {
41+
continue;
42+
}
43+
3644
$this->action = $route->getAction('uses');
3745
$methods = $route->methods();
3846

tests/GeneratorTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function testHasPaths($docs)
4343
$this->assertEquals([
4444
'/users',
4545
'/users/{id}',
46+
'/api',
47+
'/api/store',
4648
], array_keys($docs['paths']));
4749

4850
return $docs['paths'];
@@ -105,4 +107,34 @@ public function testOptionalData()
105107
$this->assertContains('application/json', $docs['consumes']);
106108
$this->assertContains('application/json', $docs['produces']);
107109
}
108-
}
110+
111+
/**
112+
* @param string|null $routeFilter
113+
* @param array $expectedRoutes
114+
*
115+
* @dataProvider filtersRoutesProvider
116+
*/
117+
public function testFiltersRoutes($routeFilter, $expectedRoutes)
118+
{
119+
$this->generator = new Generator(
120+
$this->config = config('laravel-swagger'),
121+
$routeFilter
122+
);
123+
124+
$docs = $this->generator->generate();
125+
126+
$this->assertEquals($expectedRoutes, array_keys($docs['paths']));
127+
}
128+
129+
/**
130+
* @return array
131+
*/
132+
public function filtersRoutesProvider()
133+
{
134+
return [
135+
'No Filter' => [null, ['/users', '/users/{id}', '/api', '/api/store']],
136+
'/api Filter' => ['/api', ['/api', '/api/store']],
137+
'/=nonexistant Filter' => ['/nonexistant', []],
138+
];
139+
}
140+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Mtrajano\LaravelSwagger\Tests\Stubs\Controllers;
4+
5+
use Illuminate\Routing\Controller;
6+
7+
class ApiController extends Controller
8+
{
9+
public function index()
10+
{
11+
return json_encode(['result' => 'success']);
12+
}
13+
14+
public function store()
15+
{
16+
return json_encode(['result' => 'success']);
17+
}
18+
}

tests/TestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ protected function getEnvironmentSetUp($app)
1616
$app['router']->get('/users', 'Mtrajano\\LaravelSwagger\\Tests\\Stubs\\Controllers\\UserController@index');
1717
$app['router']->get('/users/{id}', 'Mtrajano\\LaravelSwagger\\Tests\\Stubs\\Controllers\\UserController@show');
1818
$app['router']->post('/users', 'Mtrajano\\LaravelSwagger\\Tests\\Stubs\\Controllers\\UserController@store');
19+
$app['router']->get('/api', 'Mtrajano\\LaravelSwagger\\Tests\\Stubs\\Controllers\\ApiController@index');
20+
$app['router']->put('/api/store', 'Mtrajano\\LaravelSwagger\\Tests\\Stubs\\Controllers\\ApiController@store');
1921
}
20-
}
22+
}

0 commit comments

Comments
 (0)