Browse Source

Merge branch 'knuckleswtf:master' into openapi-nested-objects-properties

AdamiecRadek 2 năm trước cách đây
mục cha
commit
216ff427a4

+ 9 - 2
src/Commands/GenerateDocumentation.php

@@ -7,8 +7,6 @@ use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\URL;
 use Illuminate\Support\Str;
 use Knuckles\Camel\Camel;
-use Knuckles\Camel\Output\OutputEndpointData;
-use Knuckles\Scribe\Exceptions\GroupNotFound;
 use Knuckles\Scribe\GroupedEndpoints\GroupedEndpointsFactory;
 use Knuckles\Scribe\Matching\RouteMatcherInterface;
 use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
@@ -86,6 +84,13 @@ class GenerateDocumentation extends Command
         return $this->docConfig;
     }
 
+    protected function runBootstrapHook()
+    {
+        if (is_callable(Globals::$__bootstrap)) {
+            call_user_func_array(Globals::$__bootstrap, [$this]);
+        }
+    }
+
     public function bootstrap(): void
     {
         // The --verbose option is included with all Artisan commands.
@@ -110,6 +115,8 @@ class GenerateDocumentation extends Command
         if ($this->forcing && !$this->shouldExtract) {
             throw new \InvalidArgumentException("Can't use --force and --no-extraction together.");
         }
+
+        $this->runBootstrapHook();
     }
 
     protected function mergeUserDefinedEndpoints(array $groupedEndpoints, array $userDefinedEndpoints): array

+ 1 - 0
src/Extracting/Strategies/ResponseFields/GetFromResponseFieldAttribute.php

@@ -38,6 +38,7 @@ class GetFromResponseFieldAttribute extends PhpAttributeStrategy
 
         return collect([...$attributesOnController, ...$attributesOnMethod, ...$attributesOnApiResourceMethods])
             ->mapWithKeys(function ($attributeInstance) use ($endpointData) {
+                /** @var ResponseField $attributeInstance */
                 $data = $attributeInstance->toArray();
 
                 $data['type'] = ResponseFieldTools::inferTypeOfResponseField($data, $endpointData);

+ 11 - 0
src/Scribe.php

@@ -3,6 +3,7 @@
 namespace Knuckles\Scribe;
 
 use Knuckles\Camel\Extraction\ExtractedEndpointData;
+use Knuckles\Scribe\Commands\GenerateDocumentation;
 use Knuckles\Scribe\Tools\Globals;
 use Symfony\Component\HttpFoundation\Request;
 
@@ -21,6 +22,16 @@ class Scribe
         Globals::$__beforeResponseCall = $callable;
     }
 
+    /**
+     * Specify a callback that will be executed just before the generate command is executed
+     *
+     * @param callable(GenerateDocumentation): mixed $callable
+     */
+    public static function bootstrap(callable $callable)
+    {
+        Globals::$__bootstrap = $callable;
+    }
+
     /**
      * Specify a callback that will be executed when Scribe is done generating your docs.
      * This callback will receive a map of all the output paths generated, that looks like this:

+ 2 - 0
src/Tools/Globals.php

@@ -12,6 +12,8 @@ class Globals
 
     public static $__beforeResponseCall;
 
+    public static $__bootstrap;
+
     public static $__afterGenerating;
 
     public static $__instantiateFormRequestUsing;

+ 0 - 1
src/Writing/OpenAPISpecWriter.php

@@ -8,7 +8,6 @@ use Illuminate\Support\Str;
 use Knuckles\Camel\Camel;
 use Knuckles\Camel\Extraction\Response;
 use Knuckles\Camel\Output\OutputEndpointData;
-use Knuckles\Camel\Output\Group;
 use Knuckles\Camel\Output\Parameter;
 use Knuckles\Scribe\Extracting\ParamHelpers;
 use Knuckles\Scribe\Tools\DocumentationConfig;

+ 19 - 0
tests/GenerateDocumentation/BehavioursTest.php

@@ -3,6 +3,7 @@
 namespace Knuckles\Scribe\Tests\GenerateDocumentation;
 
 use Illuminate\Support\Facades\Route as RouteFacade;
+use Knuckles\Scribe\Commands\GenerateDocumentation;
 use Knuckles\Scribe\Scribe;
 use Knuckles\Scribe\Tests\BaseLaravelTest;
 use Knuckles\Scribe\Tests\Fixtures\TestController;
@@ -125,6 +126,24 @@ class BehavioursTest extends BaseLaravelTest
         Scribe::afterGenerating(fn() => null);
     }
 
+    /** @test */
+    public function calls_bootstrap_hook()
+    {
+        $commandInstance = null;
+
+        Scribe::bootstrap(function (GenerateDocumentation $command) use (&$commandInstance){
+            $commandInstance = $command;
+        });
+
+        RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
+
+        $this->generate();
+
+        $this->assertTrue($commandInstance instanceof GenerateDocumentation);
+
+        Scribe::bootstrap(fn() => null);
+    }
+
     /** @test */
     public function skips_methods_and_classes_with_hidefromapidocumentation_tag()
     {