From fbde6dd69ad92a7d7f20a238c55066c61c05dcc2 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 9 Nov 2021 16:01:28 +0000 Subject: [PATCH] [1.x] Fixes memory leak on Translator implementation (#416) * Fixes memory leak on translator keys * Apply fixes from StyleCI * Bumps framework dependency Co-authored-by: Taylor Otwell --- composer.json | 2 +- .../ProvidesDefaultConfigurationOptions.php | 1 + src/Listeners/FlushTranslatorCache.php | 27 +++++++++++++++ tests/Listeners/FlushTranslatorCacheTest.php | 33 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/Listeners/FlushTranslatorCache.php create mode 100644 tests/Listeners/FlushTranslatorCacheTest.php diff --git a/composer.json b/composer.json index 81f593083..2b75789c2 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ], "require": { "php": "^8.0", - "laravel/framework": "^8.62", + "laravel/framework": "^8.70", "laminas/laminas-diactoros": "^2.5", "laravel/serializable-closure": "^1.0", "symfony/psr-http-message-bridge": "^2.0" diff --git a/src/Concerns/ProvidesDefaultConfigurationOptions.php b/src/Concerns/ProvidesDefaultConfigurationOptions.php index a0c75fec9..7cefb37ea 100644 --- a/src/Concerns/ProvidesDefaultConfigurationOptions.php +++ b/src/Concerns/ProvidesDefaultConfigurationOptions.php @@ -48,6 +48,7 @@ public static function prepareApplicationForNextOperation(): array \Laravel\Octane\Listeners\FlushDatabaseQueryLog::class, \Laravel\Octane\Listeners\FlushLogContext::class, \Laravel\Octane\Listeners\FlushArrayCache::class, + \Laravel\Octane\Listeners\FlushTranslatorCache::class, // First-Party Packages... \Laravel\Octane\Listeners\PrepareInertiaForNextOperation::class, diff --git a/src/Listeners/FlushTranslatorCache.php b/src/Listeners/FlushTranslatorCache.php new file mode 100644 index 000000000..db0c14f95 --- /dev/null +++ b/src/Listeners/FlushTranslatorCache.php @@ -0,0 +1,27 @@ +sandbox->resolved('translator')) { + return; + } + + $translator = $event->sandbox->make('translator'); + + if ($translator instanceof NamespacedItemResolver) { + $translator->flushParsedKeys(); + } + } +} diff --git a/tests/Listeners/FlushTranslatorCacheTest.php b/tests/Listeners/FlushTranslatorCacheTest.php new file mode 100644 index 000000000..9e12b15e4 --- /dev/null +++ b/tests/Listeners/FlushTranslatorCacheTest.php @@ -0,0 +1,33 @@ +createOctaneContext([ + Request::create('/test-translator', 'GET'), + Request::create('/test-translator', 'GET'), + ]); + + $app['router']->middleware('web')->get('/test-cache', function () { + Validator::make($data, [ + 'name' => 'string|max:50', + ])->validate(); + }); + + $translator = $app['translator']; + + $app['translator'] = tap(Mockery::mock($translator), function ($translator) { + $translator->shouldReceive('flushParsedKeys')->twice(); + }); + + $worker->run(); + } +}