Skip to content

Commit

Permalink
Add afterGenerating() hook
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Oct 26, 2021
1 parent 90808bd commit 61a4821
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/Scribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,38 @@
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 changes: 2 additions & 0 deletions src/Tools/Globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class Globals
public static bool $shouldBeVerbose = false;

public static $beforeResponseCall;

public static $afterGenerating;
}
39 changes: 38 additions & 1 deletion src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;

Expand All @@ -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)
{
Expand All @@ -44,6 +56,8 @@ public function writeDocs(array $groupedEndpoints)
$this->writePostmanCollection($groupedEndpoints);

$this->writeOpenAPISpec($groupedEndpoints);

$this->runAfterGeneratingHook();
}

protected function writePostmanCollection(array $groups): void
Expand All @@ -61,6 +75,7 @@ protected function writePostmanCollection(array $groups): void
}

c::success("Wrote Postman collection to: {$collectionPath}");
$this->generatedFiles['postman'] = realpath($collectionPath);
}
}

Expand All @@ -79,6 +94,7 @@ protected function writeOpenAPISpec(array $parsedRoutes): void
}

c::success("Wrote OpenAPI specification to: {$specPath}");
$this->generatedFiles['openapi'] = realpath($specPath);
}
}

Expand Down Expand Up @@ -167,7 +183,28 @@ public function writeHtmlDocs(array $groupedEndpoints): void
$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]);
}
}

}

0 comments on commit 61a4821

Please sign in to comment.