Browse Source

Merge branch 'master' into v2

# Conflicts:
#	composer.dingo.json
#	composer.json
#	src/Tools/Utils.php
shalvah 4 years ago
parent
commit
c544932cfa

+ 2 - 4
.travis.yml

@@ -4,6 +4,7 @@ language: php
 env:
   global:
     - SETUP=stable
+    - COMPOSER_MEMORY_LIMIT=-1
 
 matrix:
   fast_finish: true
@@ -14,14 +15,11 @@ matrix:
     - php: 7.4
       env: SETUP=lint
       name: "Lint code"
-    - php: 7.2
-    - php: 7.2
-      env: SETUP=lowest
     - php: 7.3
     - php: 7.3
       env: SETUP=lowest
     - php: 7.4
-      # No lowest for 7.4 because the lowest (Laravel 5.8) doesn't support 7.4
+      # No lowest for 7.4 because the lowest we support (Laravel 5.8) doesn't support 7.4
 
 cache:
   directories:

+ 8 - 0
CHANGELOG.md

@@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.
 
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project aims to adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## 1.8.0 (Tuesday, 15 September, 2020)
+- Lumen 8 support
+- Fixed Laravel 8 + legacy factories support
+- Fixed the OpenAPI route for `laravel` type docs (https://github.com/knuckleswtf/scribe/pull/96)
+
+## 1.7.0 (Saturday, 12 September, 2020)
+Laravel 8 support
+
 ## 1.6.0 (Tuesday, 8 September, 2020)
 ### Additions
 - New `description` field, where you can add a description of your API. This field will be used as the `info.description` field in the Postman collection and OpenAPI spec, and as the first paragraph under the "Introduction" section on the generated webpage, before the `intro_text`. (https://github.com/knuckleswtf/scribe/pull/90/commits/dc356f3f2b13732d567dbee88dad07fc0441f40e)

+ 5 - 2
composer.dingo.json

@@ -16,9 +16,10 @@
         }
     ],
     "require": {
+        "php": ">=7.2.5",
         "ext-fileinfo": "*",
         "ext-json": "*",
-        "dingo/api": "^2.3",
+        "dingo/api": "^2.3|^3.0",
         "erusev/parsedown": "^1.7.4",
         "fzaninotto/faker": "^1.9.1",
         "illuminate/console": "^6.0|^7.0|^8.0",
@@ -37,7 +38,9 @@
         "dms/phpunit-arraysubset-asserts": "^0.1.0",
         "brianium/paratest": "^4.0",
         "league/fractal": "^0.19.0",
-        "orchestra/testbench": "^4.0|^5.0",
+        "laravel/legacy-factories": "^1.0.4",
+        "laravel/lumen-framework": "^6.0|^7.0|^8.0",
+        "orchestra/testbench": "^4.0|^5.0|^6.0",
         "phpstan/phpstan": "^0.12.19",
         "phpunit/phpunit": "^8.0|^9.0"
     },

+ 1 - 0
composer.json

@@ -36,6 +36,7 @@
   "require-dev": {
     "brianium/paratest": "^4.0",
     "dms/phpunit-arraysubset-asserts": "^0.1.0",
+    "laravel/legacy-factories": "^1.0.4",
     "laravel/lumen-framework": "^6.0|^7.0|^8.0",
     "league/fractal": "^0.19.0",
     "orchestra/testbench": "^4.0|^5.0",

+ 1 - 1
docs/documenting/documenting-endpoint-responses.md

@@ -301,7 +301,7 @@ When generating responses from `@apiResource` and `@transformer` tags, Scribe ne
 3. If that fails, Scribe creates an instance using `new YourModel()`.
 
 ### Applying factory states
-If you want specific states to be applied to your model when instantiating via `factory(YourModel::class)`, you can use the `states` attribute on `@apiResourceModel` or `@transformerModel`. Separate multiple states with a comma.
+If you want specific states to be applied to your model when instantiating via the Laravel model factory, you can use the `states` attribute on `@apiResourceModel` or `@transformerModel`. Separate multiple states with a comma.
 
 ```php
 /**

+ 1 - 0
phpstan.neon

@@ -6,6 +6,7 @@ parameters:
         - '#Call to an undefined static method Illuminate\\Support\\Facades\\Route::getRoutes\(\).#'
         - '#Call to an undefined static method Illuminate\\Support\\Facades\\URL::forceRootUrl\(\)#'
         - '#Call to an undefined static method Illuminate\\Support\\Facades\\URL::formatRoot\(\)#'
+        - '#Call to an undefined static method Illuminate\\Support\\Facades\\Storage::path\(\)#'
         - '#Cannot access offset .+ on Illuminate\\Contracts\\Foundation\\Application.#'
         - '#Call to an undefined method Illuminate\\Routing\\Route::versions\(\).#'
         - '#Call to an undefined method Illuminate\\Contracts\\Validation\\Validator::getRules\(\).#'

+ 2 - 4
src/Extracting/Strategies/Responses/UseApiResourceTags.php

@@ -18,6 +18,7 @@ use Knuckles\Scribe\Extracting\Strategies\Strategy;
 use Knuckles\Scribe\Tools\AnnotationParser as a;
 use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
 use Knuckles\Scribe\Tools\ErrorHandlingUtils as e;
+use Knuckles\Scribe\Tools\Utils;
 use Mpociot\Reflection\DocBlock;
 use Mpociot\Reflection\DocBlock\Tag;
 use ReflectionClass;
@@ -178,10 +179,7 @@ class UseApiResourceTags extends Strategy
             // but the user might write it that way in a comment. Let's be safe.
             $type = ltrim($type, '\\');
 
-            $factory = factory($type);
-            if (count($factoryStates)) {
-                $factory->states($factoryStates);
-            }
+            $factory = Utils::getModelFactory($type, $factoryStates);
             try {
                 return $factory->create();
             } catch (Exception $e) {

+ 2 - 5
src/Extracting/Strategies/Responses/UseTransformerTags.php

@@ -13,6 +13,7 @@ use Knuckles\Scribe\Extracting\Strategies\Strategy;
 use Knuckles\Scribe\Tools\AnnotationParser as a;
 use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
 use Knuckles\Scribe\Tools\ErrorHandlingUtils as e;
+use Knuckles\Scribe\Tools\Utils;
 use League\Fractal\Manager;
 use League\Fractal\Resource\Collection;
 use League\Fractal\Resource\Item;
@@ -167,11 +168,7 @@ class UseTransformerTags extends Strategy
             // but the user might write it that way in a comment. Let's be safe.
             $type = ltrim($type, '\\');
 
-            $factory = factory($type);
-            if (count($factoryStates)) {
-                $factory->states($factoryStates);
-            }
-
+            $factory = Utils::getModelFactory($type, $factoryStates);
             try {
                 return $factory->create();
             } catch (Exception $e) {

+ 6 - 3
src/Http/Controller.php

@@ -23,10 +23,13 @@ class Controller
         );
     }
 
+    /**
+     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
+     *
+     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
+     */
     public function openapi()
     {
-        return response()->json(
-            json_decode(Storage::disk('local')->get('scribe/openapi.yaml'))
-        );
+        return response()->file(Storage::path('scribe/openapi.yaml'));
     }
 }

+ 21 - 1
src/Tools/Utils.php

@@ -102,9 +102,9 @@ class Utils
      *
      * @param array $routeControllerAndMethod
      *
-     * @return ReflectionFunctionAbstract
      * @throws ReflectionException
      *
+     * @return ReflectionFunctionAbstract
      */
     public static function getReflectedRouteMethod(array $routeControllerAndMethod): ReflectionFunctionAbstract
     {
@@ -126,4 +126,24 @@ class Utils
     {
         return substr($typeName, 0, -2);
     }
+
+    public static function getModelFactory(string $modelName, array $states = [])
+    {
+        if (!function_exists('factory')) { // Laravel 8 type factory
+            $factory = call_user_func_array([$modelName, 'factory'], []);
+            if (count($states)) {
+                foreach ($states as $state) {
+                    $factory = $factory->$state();
+                }
+            }
+        } else {
+            $factory = factory($modelName);
+            if (count($states)) {
+                $factory = $factory->states($states);
+            }
+        }
+
+        return $factory;
+    }
+
 }

+ 4 - 3
tests/Fixtures/TestController.php

@@ -5,6 +5,7 @@ namespace Knuckles\Scribe\Tests\Fixtures;
 use Illuminate\Http\Request;
 use Illuminate\Routing\Controller;
 use Knuckles\Scribe\Tests\Unit\GeneratorTestCase;
+use Knuckles\Scribe\Tools\Utils;
 
 /**
  * @group Group A
@@ -175,7 +176,7 @@ class TestController extends Controller
      */
     public function withEloquentApiResource()
     {
-        return new TestUserApiResource(factory(TestUser::class)->make(['id' => 0]));
+        return new TestUserApiResource(Utils::getModelFactory(TestUser::class)->make(['id' => 0]));
     }
 
     /**
@@ -187,7 +188,7 @@ class TestController extends Controller
     public function withEloquentApiResourceCollection()
     {
         return TestUserApiResource::collection(
-            collect([factory(TestUser::class)->make(['id' => 0])])
+            collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
         );
     }
 
@@ -200,7 +201,7 @@ class TestController extends Controller
     public function withEloquentApiResourceCollectionClass()
     {
         return new TestUserApiResourceCollection(
-            collect([factory(TestUser::class)->make(['id' => 0])])
+            collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
         );
     }
 

+ 6 - 2
tests/Strategies/Responses/UseApiResourceTagsTest.php

@@ -6,11 +6,12 @@ use Knuckles\Scribe\Extracting\Strategies\Responses\UseApiResourceTags;
 use Knuckles\Scribe\ScribeServiceProvider;
 use Knuckles\Scribe\Tests\Fixtures\TestUser;
 use Knuckles\Scribe\Tools\DocumentationConfig;
+use Knuckles\Scribe\Tools\Utils;
 use Mpociot\Reflection\DocBlock\Tag;
 use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
 use Orchestra\Testbench\TestCase;
 
-class UseApiResourceTagsTest extends TestCase
+    class UseApiResourceTagsTest extends TestCase
 {
     use ArraySubsetAsserts;
 
@@ -22,6 +23,9 @@ class UseApiResourceTagsTest extends TestCase
         if (class_exists(\Dingo\Api\Provider\LaravelServiceProvider::class)) {
             $providers[] = \Dingo\Api\Provider\LaravelServiceProvider::class;
         }
+        if (class_exists(\Illuminate\Database\Eloquent\LegacyFactoryServiceProvider::class)) {
+            $providers[] = \Illuminate\Database\Eloquent\LegacyFactoryServiceProvider ::class;
+        }
         return $providers;
     }
 
@@ -102,7 +106,7 @@ class UseApiResourceTagsTest extends TestCase
         $factory = app(\Illuminate\Database\Eloquent\Factory::class);
         $factory->afterMaking(TestUser::class, function (TestUser $user, $faker) {
             if ($user->id === 4) {
-                $child = factory(TestUser::class)->make(['id' => 5, 'parent_id' => 4]);
+                $child = Utils::getModelFactory(TestUser::class)->make(['id' => 5, 'parent_id' => 4]);
                 $user->setRelation('children', collect([$child]));
             }
         });