Skip to content

Commit

Permalink
Added beforeRequest and afterRequest events
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Jan 31, 2024
1 parent 0d4f260 commit 7fa9811
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/AfterburnerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace mako\afterburner;

use Closure;
use mako\application\web\Application;

/**
Expand All @@ -17,5 +18,5 @@ interface AfterburnerInterface
/**
* Runs the application.
*/
public static function run(Application $application, mixed ...$options): void;
public static function run(Application $application, ?Closure $beforeRequest = null, ?Closure $afterRequest, mixed ...$options): void;
}
15 changes: 14 additions & 1 deletion src/FrankenPHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace mako\afterburner;

use Closure;
use mako\application\Application as BaseApplication;
use mako\application\CurrentApplication;
use mako\application\web\Application;
Expand All @@ -26,7 +27,7 @@ class FrankenPHP implements AfterburnerInterface
/**
* {@inheritDoc}
*/
public static function run(Application $application, mixed ...$options): void
public static function run(Application $application, ?Closure $beforeRequest = null, ?Closure $afterRequest, mixed ...$options): void
{
$classesToKeep = $application->getContainer()->getInstanceClassNames();

Expand All @@ -44,6 +45,12 @@ public static function run(Application $application, mixed ...$options): void

CurrentApplication::set($currentApplication);

// Run the before request closure and stop processing requests if it returns FALSE.

if ($beforeRequest !== null && $currentApplication->getContainer()->call($beforeRequest) === false) {
break;
}

// Handle the request.

$success = frankenphp_handle_request(static function () use ($currentApplication) {
Expand All @@ -59,6 +66,12 @@ public static function run(Application $application, mixed ...$options): void
}
});

// Run the after request closure and stop processing requests if it returns FALSE.

if ($afterRequest !== null && $currentApplication->getContainer()->call($afterRequest) === false) {
break;
}

// Reset the container to the default state and collect garbage.

$classesToRemove = array_diff($application->getContainer()->getInstanceClassNames(), $classesToKeep);
Expand Down

0 comments on commit 7fa9811

Please sign in to comment.