Skip to content

Commit

Permalink
feat: add after response hook
Browse files Browse the repository at this point in the history
  • Loading branch information
max13fr authored May 6, 2024
1 parent 0fd9abb commit f0bceb0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Extracting/Strategies/Responses/ResponseCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace Knuckles\Scribe\Extracting\Strategies\Responses;

use Illuminate\Support\Facades\Config;
use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Dingo\Api\Dispatcher;
use Dingo\Api\Routing\Route as DingoRoute;
use Exception;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\UploadedFile;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Knuckles\Scribe\Extracting\DatabaseTransactionHelpers;
use Knuckles\Scribe\Extracting\ParamHelpers;
use Knuckles\Scribe\Extracting\Strategies\Strategy;
Expand Down Expand Up @@ -89,6 +90,9 @@ public function makeResponseCall(ExtractedEndpointData $endpointData, array $set

try {
$response = $this->makeApiCall($request, $endpointData->route);

$this->runPostRequestHook($request, $endpointData, $response);

$response = [
[
'status' => $response->getStatusCode(),
Expand Down Expand Up @@ -170,6 +174,13 @@ protected function runPreRequestHook(Request $request, ExtractedEndpointData $en
}
}

protected function runPostRequestHook(Request $request, ExtractedEndpointData $endpointData, JsonResponse $jsonResponse): void
{
if (is_callable(Globals::$__afterResponseCall)) {
call_user_func_array(Globals::$__afterResponseCall, [$request, $endpointData, $jsonResponse]);
}
}

private function setLaravelConfigs(array $config)
{
if (empty($config)) {
Expand Down
12 changes: 12 additions & 0 deletions src/Scribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Knuckles\Scribe;

use Illuminate\Http\JsonResponse;
use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Knuckles\Scribe\Commands\GenerateDocumentation;
use Knuckles\Scribe\Tools\Globals;
Expand All @@ -22,6 +23,17 @@ public static function beforeResponseCall(callable $callable)
Globals::$__beforeResponseCall = $callable;
}

/**
* Specify a callback that will be executed just after a response call is done
* (allowing to modify the response).
*
* @param callable(Request, ExtractedEndpointData, JSONResponse): mixed $callable
*/
public static function afterResponseCall(callable $callable)
{
Globals::$__afterResponseCall = $callable;
}

/**
* Specify a callback that will be executed just before the generate command is executed
*
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 $__afterResponseCall;

public static $__bootstrap;

public static $__afterGenerating;
Expand Down

0 comments on commit f0bceb0

Please sign in to comment.