generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to exclude routes using attributes instead of re-defining rou…
…tes resolution method (#594) * attributes to exclude routes * Fix styling --------- Co-authored-by: romalytvynenko <[email protected]>
- Loading branch information
1 parent
65e2842
commit 1a7547b
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Excludes all routes of a controller from the API documentation. Applies to controller's methods. | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class ExcludeAllRoutesFromDocs {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Excludes a route from API documentation. Applies to controller's methods. | ||
*/ | ||
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)] | ||
class ExcludeRouteFromDocs {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use Dedoc\Scramble\Attributes\ExcludeAllRoutesFromDocs; | ||
use Dedoc\Scramble\Attributes\ExcludeRouteFromDocs; | ||
use Dedoc\Scramble\Scramble; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Support\Facades\Route as RouteFacade; | ||
|
||
function RoutesFilteringTest_generateForRoutes($callback) | ||
{ | ||
$routesUris = array_map( | ||
fn (Route $r) => $r->uri, | ||
$callback() | ||
); | ||
|
||
Scramble::routes(fn (Route $r) => in_array($r->uri, $routesUris)); | ||
|
||
return app()->make(\Dedoc\Scramble\Generator::class)(); | ||
} | ||
|
||
it('filters routes with ExcludeRouteFromDocs attribute', function () { | ||
$documentation = RoutesFilteringTest_generateForRoutes(fn () => [ | ||
RouteFacade::post('foo', [RoutesFilteringTest_ControllerA::class, 'foo']), | ||
RouteFacade::post('bar', [RoutesFilteringTest_ControllerA::class, 'bar']), | ||
]); | ||
|
||
expect(array_keys($documentation['paths']))->toBe(['/foo']); | ||
}); | ||
class RoutesFilteringTest_ControllerA | ||
{ | ||
public function foo() {} | ||
|
||
#[ExcludeRouteFromDocs] | ||
public function bar() {} | ||
} | ||
|
||
it('filters all controller routes with ExcludeAllRoutesFromDocs attribute', function () { | ||
$documentation = RoutesFilteringTest_generateForRoutes(fn () => [ | ||
RouteFacade::post('foo', [RoutesFilteringTest_ControllerB::class, 'foo']), | ||
RouteFacade::post('bar', [RoutesFilteringTest_ControllerB::class, 'bar']), | ||
]); | ||
|
||
expect(array_keys($documentation['paths'] ?? []))->toBe([]); | ||
}); | ||
|
||
#[ExcludeAllRoutesFromDocs] | ||
class RoutesFilteringTest_ControllerB | ||
{ | ||
public function foo() {} | ||
|
||
public function bar() {} | ||
} |