ソースを参照

Add afterGenerating() hook

shalvah 3 年 前
コミット
61a4821e10
3 ファイル変更68 行追加1 行削除
  1. 28 0
      src/Scribe.php
  2. 2 0
      src/Tools/Globals.php
  3. 38 1
      src/Writing/Writer.php

+ 28 - 0
src/Scribe.php

@@ -9,10 +9,38 @@ use Symfony\Component\HttpFoundation\Request;
 class Scribe
 {
     /**
+     * Specify a callback that will be executed just before a response call is made
+     * (after configuring the environment and starting a transaction).
+     *
      * @param callable(Request, ExtractedEndpointData): mixed $callable
      */
     public static function beforeResponseCall(callable $callable)
     {
         Globals::$beforeResponseCall = $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:
+     * [
+     *   'postman' => '/absolute/path/to/postman/collection',
+     *   'openapi' => '/absolute/path/to/openapi/spec',
+     *    // If you're using `laravel` type, `html` will be null, and vice versa for `blade`.
+     *   'html' => '/absolute/path/to/index.html/',
+     *   'blade' => '/absolute/path/to/blade/view',
+     *    // These are paths to asset folders
+     *   'assets' => [
+     *     'js' => '/path/to/js/assets/folder',
+     *     'css' => '/path/to/css/assets/folder',
+     *     'images' => '/path/to/images/assets/folder',
+     *   ]
+     * ]
+     *
+     * If you disabled `postman` or `openapi`, their values will be null.
+     *
+     * @param callable(array<string, string>): mixed $callable
+     */
+    public static function afterGenerating(callable $callable)
+    {
+        Globals::$afterGenerating = $callable;
+    }
 }

+ 2 - 0
src/Tools/Globals.php

@@ -9,4 +9,6 @@ class Globals
     public static bool $shouldBeVerbose = false;
 
     public static $beforeResponseCall;
+
+    public static $afterGenerating;
 }

+ 38 - 1
src/Writing/Writer.php

@@ -5,6 +5,7 @@ namespace Knuckles\Scribe\Writing;
 use Illuminate\Support\Facades\Storage;
 use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
 use Knuckles\Scribe\Tools\DocumentationConfig;
+use Knuckles\Scribe\Tools\Globals;
 use Knuckles\Scribe\Tools\Utils;
 use Symfony\Component\Yaml\Yaml;
 
@@ -19,6 +20,17 @@ class Writer
     private string $staticTypeOutputPath;
 
     private string $laravelTypeOutputPath = 'resources/views/scribe';
+    protected array $generatedFiles = [
+        'postman' => null,
+        'openapi' => null,
+        'html' => null,
+        'blade' => null,
+        'assets' => [
+            'js' => null,
+            'css' => null,
+            'images' => null,
+        ]
+    ];
 
     public function __construct(DocumentationConfig $config = null)
     {
@@ -44,6 +56,8 @@ class Writer
         $this->writePostmanCollection($groupedEndpoints);
 
         $this->writeOpenAPISpec($groupedEndpoints);
+
+        $this->runAfterGeneratingHook();
     }
 
     protected function writePostmanCollection(array $groups): void
@@ -61,6 +75,7 @@ class Writer
             }
 
             c::success("Wrote Postman collection to: {$collectionPath}");
+            $this->generatedFiles['postman'] = realpath($collectionPath);
         }
     }
 
@@ -79,6 +94,7 @@ class Writer
             }
 
             c::success("Wrote OpenAPI specification to: {$specPath}");
+            $this->generatedFiles['openapi'] = realpath($specPath);
         }
     }
 
@@ -167,7 +183,28 @@ class Writer
             $this->performFinalTasksForLaravelType();
         }
 
-        c::success("Wrote HTML docs to: " . rtrim($this->isStatic ? $this->staticTypeOutputPath : $this->laravelTypeOutputPath, '/').'/');
+        if ($this->isStatic) {
+            $outputPath = rtrim($this->staticTypeOutputPath, '/') . '/';
+            c::success("Wrote HTML docs to: $outputPath");
+            $this->generatedFiles['html'] = realpath("{$outputPath}index.html");
+            $assetsOutputPath = $outputPath;
+        } else {
+            $outputPath = rtrim($this->laravelTypeOutputPath, '/') . '/';
+            c::success("Wrote Blade docs to: $outputPath");
+            $this->generatedFiles['blade'] = realpath("{$outputPath}index.blade.php");
+            $assetsOutputPath = app()->get('path.public')."/vendor/scribe/";
+        }
+        $this->generatedFiles['assets']['js'] = realpath("{$assetsOutputPath}css");
+        $this->generatedFiles['assets']['css'] = realpath("{$assetsOutputPath}js");
+        $this->generatedFiles['assets']['images'] = realpath("{$assetsOutputPath}images");
+    }
+
+    protected function runAfterGeneratingHook()
+    {
+        if (is_callable(Globals::$afterGenerating)) {
+            c::info("Running `afterGenerating()` hook...");
+            call_user_func_array(Globals::$afterGenerating, [$this->generatedFiles]);
+        }
     }
 
 }