Peter Ragheb 2 лет назад
Родитель
Сommit
2046ca8b5f

+ 10 - 0
src/Commands/GenerateDocumentation.php

@@ -40,6 +40,8 @@ class GenerateDocumentation extends Command
 
     public function handle(RouteMatcherInterface $routeMatcher, GroupedEndpointsFactory $groupedEndpointsFactory): void
     {
+        $this->runBeforeGeneratingHook();
+
         $this->bootstrap();
 
         if (!empty($this->docConfig->get("default_group"))) {
@@ -86,6 +88,14 @@ class GenerateDocumentation extends Command
         return $this->docConfig;
     }
 
+    protected function runBeforeGeneratingHook()
+    {
+        if (is_callable(Globals::$__beforeGenerating)) {
+            c::info("Running `beforeGenerating()` hook...");
+            call_user_func_array(Globals::$__beforeGenerating, []);
+        }
+    }
+
     public function bootstrap(): void
     {
         // The --verbose option is included with all Artisan commands.

+ 10 - 0
src/Scribe.php

@@ -21,6 +21,16 @@ class Scribe
         Globals::$__beforeResponseCall = $callable;
     }
 
+    /**
+     * Specify a callback that will be executed just before the generate command is executed
+     *
+     * @param callable(): mixed $callable
+     */
+    public static function beforeGenerating(callable $callable)
+    {
+        Globals::$__beforeGenerating = $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 $__beforeGenerating;
+
     public static $__afterGenerating;
 
     public static $__instantiateFormRequestUsing;

+ 18 - 0
tests/GenerateDocumentation/BehavioursTest.php

@@ -125,6 +125,24 @@ class BehavioursTest extends BaseLaravelTest
         Scribe::afterGenerating(fn() => null);
     }
 
+    /** @test */
+    public function calls_beforeGenerating_hook()
+    {
+        $called = false;
+
+        Scribe::beforeGenerating(function () use (&$called){
+            $called = true;
+        });
+
+        RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
+
+        $this->generate();
+
+        $this->assertTrue($called);
+
+        Scribe::beforeGenerating(fn() => null);
+    }
+
     /** @test */
     public function skips_methods_and_classes_with_hidefromapidocumentation_tag()
     {