Skip to content

Commit a4ad23a

Browse files
committed
Added Laravel 5.4 compatibility
1 parent 12abacd commit a4ad23a

File tree

11 files changed

+74
-16
lines changed

11 files changed

+74
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Go to your `config/app.php` and add the service provider:
2626
Mpociot\ApiDoc\ApiDocGeneratorServiceProvider::class,
2727
```
2828

29+
> Using Laravel < 5.4? Use version 1.0! For Laravel 5.4 and up, use 2.0 instead.
30+
2931
## Usage
3032

3133
To generate your API documentation, use the `api:generate` artisan command.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"require": {
1818
"php": ">=5.5.0",
1919
"fzaninotto/faker": "~1.0",
20-
"laravel/framework": "~5.0",
20+
"laravel/framework": "~5.4",
2121
"mpociot/documentarian": "^0.2.0",
2222
"mpociot/reflection-docblock": "^1.0",
2323
"ramsey/uuid": "^3.0"

src/Mpociot/ApiDoc/ApiDocGeneratorServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public function boot()
3131
*/
3232
public function register()
3333
{
34-
$this->app['apidoc.generate'] = $this->app->share(function () {
34+
$this->app->singleton('apidoc.generate', function () {
3535
return new GenerateDocumentation();
3636
});
37-
$this->app['apidoc.update'] = $this->app->share(function () {
37+
$this->app->singleton('apidoc.update', function () {
3838
return new UpdateDocumentation();
3939
});
4040

src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ private function processLaravelRoutes(AbstractGenerator $generator, $allowedRout
256256
$bindings = $this->getBindings();
257257
$parsedRoutes = [];
258258
foreach ($routes as $route) {
259-
if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri()) || in_array($middleware, $route->middleware())) {
259+
if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $generator->getUri($route)) || in_array($middleware, $route->middleware())) {
260260
if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) {
261261
$parsedRoutes[] = $generator->processRoute($route, $bindings, $this->option('header'), $withResponse);
262-
$this->info('Processed route: ['.implode(',', $route->getMethods()).'] '.$route->getUri());
262+
$this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
263263
} else {
264-
$this->warn('Skipping route: ['.implode(',', $route->getMethods()).'] '.$route->getUri());
264+
$this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
265265
}
266266
}
267267
}

src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ abstract class AbstractGenerator
1818
*
1919
* @return mixed
2020
*/
21-
abstract protected function getUri($route);
21+
abstract public function getUri($route);
22+
23+
/**
24+
* @param $route
25+
*
26+
* @return mixed
27+
*/
28+
abstract public function getMethods($route);
2229

2330
/**
2431
* @param \Illuminate\Routing\Route $route
@@ -76,7 +83,7 @@ protected function getRouteResponse($route, $bindings, $headers = [])
7683
{
7784
$uri = $this->addRouteModelBindings($route, $bindings);
7885

79-
$methods = $route->getMethods();
86+
$methods = $this->getMethods($route);
8087

8188
// Split headers into key - value pairs
8289
$headers = collect($headers)->map(function ($value) {

src/Mpociot/ApiDoc/Generators/DingoGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,16 @@ public function callRoute($method, $uri, $parameters = [], $cookies = [], $files
7171
/**
7272
* {@inheritdoc}
7373
*/
74-
protected function getUri($route)
74+
public function getUri($route)
7575
{
7676
return $route->uri();
7777
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public function getMethods($route)
83+
{
84+
return $route->getMethods();
85+
}
7886
}

src/Mpociot/ApiDoc/Generators/LaravelGenerator.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,25 @@ class LaravelGenerator extends AbstractGenerator
1515
*
1616
* @return mixed
1717
*/
18-
protected function getUri($route)
18+
public function getUri($route)
1919
{
20-
return $route->getUri();
20+
if (version_compare(app()->version(), '5.4', '<')) {
21+
return $route->getUri();
22+
}
23+
return $route->uri();
24+
}
25+
26+
/**
27+
* @param Route $route
28+
*
29+
* @return mixed
30+
*/
31+
public function getMethods($route)
32+
{
33+
if (version_compare(app()->version(), '5.4', '<')) {
34+
return $route->getMethods();
35+
}
36+
return $route->methods();
2137
}
2238

2339
/**
@@ -46,12 +62,12 @@ public function processRoute($route, $bindings = [], $headers = [], $withRespons
4662
}
4763

4864
return $this->getParameters([
49-
'id' => md5($route->getUri().':'.implode($route->getMethods())),
65+
'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))),
5066
'resource' => $routeGroup,
5167
'title' => $routeDescription['short'],
5268
'description' => $routeDescription['long'],
53-
'methods' => $route->getMethods(),
54-
'uri' => $route->getUri(),
69+
'methods' => $this->getMethods($route),
70+
'uri' => $this->getUri($route),
5571
'parameters' => [],
5672
'response' => $content,
5773
], $routeAction, $bindings);

tests/DingoGeneratorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function setUp()
3737

3838
public function testCanParseMethodDescription()
3939
{
40+
if (version_compare($this->app->version(), '5.4', '>=')) {
41+
$this->markTestSkipped('Dingo does not support Laravel 5.4');
42+
}
43+
4044
$api = app('Dingo\Api\Routing\Router');
4145
$api->version('v1', function ($api) {
4246
$api->get('/api/test', TestController::class.'@parseMethodDescription');
@@ -51,6 +55,10 @@ public function testCanParseMethodDescription()
5155

5256
public function testCanParseRouteMethods()
5357
{
58+
if (version_compare($this->app->version(), '5.4', '>=')) {
59+
$this->markTestSkipped('Dingo does not support Laravel 5.4');
60+
}
61+
5462
$api = app('Dingo\Api\Routing\Router');
5563
$api->version('v1', function ($api) {
5664
$api->get('/get', TestController::class.'@dummy');
@@ -77,6 +85,10 @@ public function testCanParseRouteMethods()
7785

7886
public function testCanParseFormRequestRules()
7987
{
88+
if (version_compare($this->app->version(), '5.4', '>=')) {
89+
$this->markTestSkipped('Dingo does not support Laravel 5.4');
90+
}
91+
8092
$api = app('Dingo\Api\Routing\Router');
8193
$api->version('v1', function ($api) {
8294
$api->post('/post', DingoTestController::class.'@parseFormRequestRules');

tests/Fixtures/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ It can also be multiple lines long.
3030
> Example request:
3131
3232
```bash
33-
curl "http://localhost/api/test" \
33+
curl -X GET "http://localhost/api/test" \
3434
-H "Accept: application/json"
3535
```
3636

@@ -63,13 +63,14 @@ null
6363

6464

6565
<!-- END_08307893aff90cc5097c48a1c8fc2f6d -->
66+
6667
<!-- START_8ba174f2507a0967efd46fab3764b80e -->
6768
## api/fetch
6869

6970
> Example request:
7071
7172
```bash
72-
curl "http://localhost/api/fetch" \
73+
curl -X GET "http://localhost/api/fetch" \
7374
-H "Accept: application/json"
7475
```
7576

@@ -108,3 +109,4 @@ $.ajax(settings).done(function (response) {
108109

109110

110111
<!-- END_8ba174f2507a0967efd46fab3764b80e -->
112+

tests/Fixtures/resource_index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ $.ajax(settings).done(function (response) {
6262

6363

6464
<!-- END_2ea88ff35aa222f5582e50f39a2b35fd -->
65+
6566
<!-- START_99a7210df460e7fd8ad2508ee28b9763 -->
6667
## Show the form for creating a new resource.
6768

@@ -103,6 +104,7 @@ $.ajax(settings).done(function (response) {
103104

104105

105106
<!-- END_99a7210df460e7fd8ad2508ee28b9763 -->
107+
106108
<!-- START_f0654d3f2fc63c11f5723f233cc53c83 -->
107109
## Store a newly created resource in storage.
108110

@@ -135,6 +137,7 @@ $.ajax(settings).done(function (response) {
135137

136138

137139
<!-- END_f0654d3f2fc63c11f5723f233cc53c83 -->
140+
138141
<!-- START_7a5835399fad9a53bc0430d6e3054297 -->
139142
## Display the specified resource.
140143

@@ -176,6 +179,7 @@ $.ajax(settings).done(function (response) {
176179

177180

178181
<!-- END_7a5835399fad9a53bc0430d6e3054297 -->
182+
179183
<!-- START_5ed9d10b12650f9536edfa994fafae15 -->
180184
## Show the form for editing the specified resource.
181185

@@ -217,6 +221,7 @@ $.ajax(settings).done(function (response) {
217221

218222

219223
<!-- END_5ed9d10b12650f9536edfa994fafae15 -->
224+
220225
<!-- START_a4a2abed1e8e8cad5e6a3282812fe3f3 -->
221226
## Update the specified resource in storage.
222227

@@ -251,6 +256,7 @@ $.ajax(settings).done(function (response) {
251256

252257

253258
<!-- END_a4a2abed1e8e8cad5e6a3282812fe3f3 -->
259+
254260
<!-- START_4bb7fb4a7501d3cb1ed21acfc3b205a9 -->
255261
## Remove the specified resource from storage.
256262

@@ -283,3 +289,4 @@ $.ajax(settings).done(function (response) {
283289

284290

285291
<!-- END_4bb7fb4a7501d3cb1ed21acfc3b205a9 -->
292+

0 commit comments

Comments
 (0)