From a4978d5fd9c5b6e0cdb63699c10ca3fe54948c6f Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 19 Jan 2024 17:08:45 +0000 Subject: [PATCH 1/3] Flushes once after `OperationTerminated` --- config/octane.php | 2 ++ src/Listeners/FlushOnce.php | 21 +++++++++++++++ tests/Listeners/FlushOnceTest.php | 43 +++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/Listeners/FlushOnce.php create mode 100644 tests/Listeners/FlushOnceTest.php diff --git a/config/octane.php b/config/octane.php index 8b578aef6..326f80964 100644 --- a/config/octane.php +++ b/config/octane.php @@ -15,6 +15,7 @@ use Laravel\Octane\Listeners\DisconnectFromDatabases; use Laravel\Octane\Listeners\EnsureUploadedFilesAreValid; use Laravel\Octane\Listeners\EnsureUploadedFilesCanBeMoved; +use Laravel\Octane\Listeners\FlushOnce; use Laravel\Octane\Listeners\FlushTemporaryContainerInstances; use Laravel\Octane\Listeners\FlushUploadedFiles; use Laravel\Octane\Listeners\ReportException; @@ -101,6 +102,7 @@ ], OperationTerminated::class => [ + FlushOnce::class, FlushTemporaryContainerInstances::class, // DisconnectFromDatabases::class, // CollectGarbage::class, diff --git a/src/Listeners/FlushOnce.php b/src/Listeners/FlushOnce.php new file mode 100644 index 000000000..245f5ce83 --- /dev/null +++ b/src/Listeners/FlushOnce.php @@ -0,0 +1,21 @@ +markTestSkipped('Once is only supported in Laravel 11+'); + } + + [$app, $worker] = $this->createOctaneContext([ + Request::create('/', 'GET'), + Request::create('/', 'GET'), + Request::create('/', 'GET'), + ]); + + $results = []; + + $app['router']->middleware('web')->get('/', function () use (&$results) { + $results[] = my_rand(); + }); + + $worker->run(); + + $this->assertTrue($results[0] !== $results[1]); + $this->assertTrue($results[0] !== $results[2]); + $this->assertTrue($results[1] !== $results[2]); + } +} + +function my_rand() +{ + return once(fn () => rand(1, PHP_INT_MAX)); +} + From 0a47806b442c9d0d18ff1195ae958cfcabaeb2fe Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 19 Jan 2024 17:08:54 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- tests/Listeners/FlushOnceTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Listeners/FlushOnceTest.php b/tests/Listeners/FlushOnceTest.php index a0f5ea0f8..f72668552 100644 --- a/tests/Listeners/FlushOnceTest.php +++ b/tests/Listeners/FlushOnceTest.php @@ -2,11 +2,9 @@ namespace Laravel\Octane\Listeners; -use Illuminate\Foundation\Application; use Illuminate\Http\Request; use Illuminate\Support\Once; use Laravel\Octane\Tests\TestCase; -use ReflectionClass; class FlushOnceTest extends TestCase { @@ -40,4 +38,3 @@ function my_rand() { return once(fn () => rand(1, PHP_INT_MAX)); } - From ec239359934cbd56ee3765dbc17f8bf71b23a171 Mon Sep 17 00:00:00 2001 From: taylorotwell Date: Fri, 19 Jan 2024 17:09:14 +0000 Subject: [PATCH 3/3] Fix code styling --- src/Listeners/FlushOnce.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Listeners/FlushOnce.php b/src/Listeners/FlushOnce.php index 245f5ce83..3a0af709c 100644 --- a/src/Listeners/FlushOnce.php +++ b/src/Listeners/FlushOnce.php @@ -10,7 +10,6 @@ class FlushOnce * Handle the event. * * @param mixed $event - * @return void */ public function handle($event): void {