Kaynağa Gözat

Merge pull request #576 from Peterragheb/feature/add-before-generating-hook

Feature: Add bootstrap hook
Shalvah 2 yıl önce
ebeveyn
işleme
25b68ccd86

+ 9 - 0
src/Commands/GenerateDocumentation.php

@@ -84,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.
@@ -108,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

+ 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;

+ 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()
     {