Browse Source

Added Laravel 5.4 compatibility

Marcel Pociot 8 years ago
parent
commit
a4ad23af21

+ 2 - 0
README.md

@@ -26,6 +26,8 @@ Go to your `config/app.php` and add the service provider:
 Mpociot\ApiDoc\ApiDocGeneratorServiceProvider::class,
 ```
 
+> Using Laravel < 5.4? Use version 1.0! For Laravel 5.4 and up, use 2.0 instead.
+
 ## Usage
 
 To generate your API documentation, use the `api:generate` artisan command.

+ 1 - 1
composer.json

@@ -17,7 +17,7 @@
     "require": {
         "php": ">=5.5.0",
         "fzaninotto/faker": "~1.0",
-        "laravel/framework": "~5.0",
+        "laravel/framework": "~5.4",
         "mpociot/documentarian": "^0.2.0",
         "mpociot/reflection-docblock": "^1.0",
         "ramsey/uuid": "^3.0"

+ 2 - 2
src/Mpociot/ApiDoc/ApiDocGeneratorServiceProvider.php

@@ -31,10 +31,10 @@ class ApiDocGeneratorServiceProvider extends ServiceProvider
      */
     public function register()
     {
-        $this->app['apidoc.generate'] = $this->app->share(function () {
+        $this->app->singleton('apidoc.generate', function () {
             return new GenerateDocumentation();
         });
-        $this->app['apidoc.update'] = $this->app->share(function () {
+        $this->app->singleton('apidoc.update', function () {
             return new UpdateDocumentation();
         });
 

+ 3 - 3
src/Mpociot/ApiDoc/Commands/GenerateDocumentation.php

@@ -256,12 +256,12 @@ class GenerateDocumentation extends Command
         $bindings = $this->getBindings();
         $parsedRoutes = [];
         foreach ($routes as $route) {
-            if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $route->getUri()) || in_array($middleware, $route->middleware())) {
+            if (in_array($route->getName(), $allowedRoutes) || str_is($routePrefix, $generator->getUri($route)) || in_array($middleware, $route->middleware())) {
                 if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) {
                     $parsedRoutes[] = $generator->processRoute($route, $bindings, $this->option('header'), $withResponse);
-                    $this->info('Processed route: ['.implode(',', $route->getMethods()).'] '.$route->getUri());
+                    $this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
                 } else {
-                    $this->warn('Skipping route: ['.implode(',', $route->getMethods()).'] '.$route->getUri());
+                    $this->warn('Skipping route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
                 }
             }
         }

+ 9 - 2
src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

@@ -18,7 +18,14 @@ abstract class AbstractGenerator
      *
      * @return mixed
      */
-    abstract protected function getUri($route);
+    abstract public function getUri($route);
+
+    /**
+     * @param $route
+     *
+     * @return mixed
+     */
+    abstract public function getMethods($route);
 
     /**
      * @param  \Illuminate\Routing\Route $route
@@ -76,7 +83,7 @@ abstract class AbstractGenerator
     {
         $uri = $this->addRouteModelBindings($route, $bindings);
 
-        $methods = $route->getMethods();
+        $methods = $this->getMethods($route);
 
         // Split headers into key - value pairs
         $headers = collect($headers)->map(function ($value) {

+ 9 - 1
src/Mpociot/ApiDoc/Generators/DingoGenerator.php

@@ -71,8 +71,16 @@ class DingoGenerator extends AbstractGenerator
     /**
      * {@inheritdoc}
      */
-    protected function getUri($route)
+    public function getUri($route)
     {
         return $route->uri();
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getMethods($route)
+    {
+        return $route->getMethods();
+    }
 }

+ 21 - 5
src/Mpociot/ApiDoc/Generators/LaravelGenerator.php

@@ -15,9 +15,25 @@ class LaravelGenerator extends AbstractGenerator
      *
      * @return mixed
      */
-    protected function getUri($route)
+    public function getUri($route)
     {
-        return $route->getUri();
+        if (version_compare(app()->version(), '5.4', '<')) {
+            return $route->getUri();
+        }
+        return $route->uri();
+    }
+
+    /**
+     * @param Route $route
+     *
+     * @return mixed
+     */
+    public function getMethods($route)
+    {
+        if (version_compare(app()->version(), '5.4', '<')) {
+            return $route->getMethods();
+        }
+        return $route->methods();
     }
 
     /**
@@ -46,12 +62,12 @@ class LaravelGenerator extends AbstractGenerator
         }
 
         return $this->getParameters([
-            'id' => md5($route->getUri().':'.implode($route->getMethods())),
+            'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))),
             'resource' => $routeGroup,
             'title' => $routeDescription['short'],
             'description' => $routeDescription['long'],
-            'methods' => $route->getMethods(),
-            'uri' => $route->getUri(),
+            'methods' => $this->getMethods($route),
+            'uri' => $this->getUri($route),
             'parameters' => [],
             'response' => $content,
         ], $routeAction, $bindings);

+ 12 - 0
tests/DingoGeneratorTest.php

@@ -37,6 +37,10 @@ class DingoGeneratorTest extends TestCase
 
     public function testCanParseMethodDescription()
     {
+        if (version_compare($this->app->version(), '5.4', '>=')) {
+            $this->markTestSkipped('Dingo does not support Laravel 5.4');
+        }
+
         $api = app('Dingo\Api\Routing\Router');
         $api->version('v1', function ($api) {
             $api->get('/api/test', TestController::class.'@parseMethodDescription');
@@ -51,6 +55,10 @@ class DingoGeneratorTest extends TestCase
 
     public function testCanParseRouteMethods()
     {
+        if (version_compare($this->app->version(), '5.4', '>=')) {
+            $this->markTestSkipped('Dingo does not support Laravel 5.4');
+        }
+
         $api = app('Dingo\Api\Routing\Router');
         $api->version('v1', function ($api) {
             $api->get('/get', TestController::class.'@dummy');
@@ -77,6 +85,10 @@ class DingoGeneratorTest extends TestCase
 
     public function testCanParseFormRequestRules()
     {
+        if (version_compare($this->app->version(), '5.4', '>=')) {
+            $this->markTestSkipped('Dingo does not support Laravel 5.4');
+        }
+
         $api = app('Dingo\Api\Routing\Router');
         $api->version('v1', function ($api) {
             $api->post('/post', DingoTestController::class.'@parseFormRequestRules');

+ 4 - 2
tests/Fixtures/index.md

@@ -30,7 +30,7 @@ It can also be multiple lines long.
 > Example request:
 
 ```bash
-curl "http://localhost/api/test" \
+curl -X GET "http://localhost/api/test" \
 -H "Accept: application/json"
 ```
 
@@ -63,13 +63,14 @@ null
 
 
 <!-- END_08307893aff90cc5097c48a1c8fc2f6d -->
+
 <!-- START_8ba174f2507a0967efd46fab3764b80e -->
 ## api/fetch
 
 > Example request:
 
 ```bash
-curl "http://localhost/api/fetch" \
+curl -X GET "http://localhost/api/fetch" \
 -H "Accept: application/json"
 ```
 
@@ -108,3 +109,4 @@ $.ajax(settings).done(function (response) {
 
 
 <!-- END_8ba174f2507a0967efd46fab3764b80e -->
+

+ 7 - 0
tests/Fixtures/resource_index.md

@@ -62,6 +62,7 @@ $.ajax(settings).done(function (response) {
 
 
 <!-- END_2ea88ff35aa222f5582e50f39a2b35fd -->
+
 <!-- START_99a7210df460e7fd8ad2508ee28b9763 -->
 ## Show the form for creating a new resource.
 
@@ -103,6 +104,7 @@ $.ajax(settings).done(function (response) {
 
 
 <!-- END_99a7210df460e7fd8ad2508ee28b9763 -->
+
 <!-- START_f0654d3f2fc63c11f5723f233cc53c83 -->
 ## Store a newly created resource in storage.
 
@@ -135,6 +137,7 @@ $.ajax(settings).done(function (response) {
 
 
 <!-- END_f0654d3f2fc63c11f5723f233cc53c83 -->
+
 <!-- START_7a5835399fad9a53bc0430d6e3054297 -->
 ## Display the specified resource.
 
@@ -176,6 +179,7 @@ $.ajax(settings).done(function (response) {
 
 
 <!-- END_7a5835399fad9a53bc0430d6e3054297 -->
+
 <!-- START_5ed9d10b12650f9536edfa994fafae15 -->
 ## Show the form for editing the specified resource.
 
@@ -217,6 +221,7 @@ $.ajax(settings).done(function (response) {
 
 
 <!-- END_5ed9d10b12650f9536edfa994fafae15 -->
+
 <!-- START_a4a2abed1e8e8cad5e6a3282812fe3f3 -->
 ## Update the specified resource in storage.
 
@@ -251,6 +256,7 @@ $.ajax(settings).done(function (response) {
 
 
 <!-- END_a4a2abed1e8e8cad5e6a3282812fe3f3 -->
+
 <!-- START_4bb7fb4a7501d3cb1ed21acfc3b205a9 -->
 ## Remove the specified resource from storage.
 
@@ -283,3 +289,4 @@ $.ajax(settings).done(function (response) {
 
 
 <!-- END_4bb7fb4a7501d3cb1ed21acfc3b205a9 -->
+

+ 4 - 0
tests/GenerateDocumentationTest.php

@@ -70,6 +70,10 @@ class GenerateDocumentationTest extends TestCase
 
     public function testConsoleCommandDoesNotWorkWithClosureUsingDingo()
     {
+        if (version_compare($this->app->version(), '5.4', '>=')) {
+            $this->markTestSkipped('Dingo does not support Laravel 5.4');
+        }
+
         $api = app('Dingo\Api\Routing\Router');
         $api->version('v1', function ($api) {
             $api->get('/closure', function () {