Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add bootstrap hook #576

Merged
merged 6 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Commands/GenerateDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public function getDocConfig(): DocumentationConfig
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.
Expand All @@ -110,6 +117,8 @@ public function bootstrap(): void
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
Expand Down
11 changes: 11 additions & 0 deletions src/Scribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,6 +22,16 @@ public static function beforeResponseCall(callable $callable)
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:
Expand Down
2 changes: 2 additions & 0 deletions src/Tools/Globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Globals

public static $__beforeResponseCall;

public static $__bootstrap;

public static $__afterGenerating;

public static $__instantiateFormRequestUsing;
Expand Down
19 changes: 19 additions & 0 deletions tests/GenerateDocumentation/BehavioursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,6 +126,24 @@ public function calls_afterGenerating_hook()
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()
{
Expand Down